maven

Create your own maven archetype

Maven is a great build automation tool.
It describes how software is built and of course it describes its dependencies.
The only problem that arises from its use is the limited variety of its archetypes and the existence of old dependency versions inside the poms of these archetypes.
I am using a couple of web sites to find the latest versions of the dependencies I use like mvnrepository.com and search.maven.org.
The latter proved to be a little more updated so I use it more frequently.
The tool that I am using in this tutorial is IntelliJ Idea Ultimate (v15) but you can use any IDE you like since maven is an IDE-agnostic build automation tool.

Let’s create a classic maven quick start project by using the maven quickstart archetype :

    [tabitem]

    step 1

    Step 1


    [/tabitem]

    [tabitem]

    step 2

    Step 2


    [/tabitem]

    [tabitem]

    step 3

    Step 3


    [/tabitem]

    [tabitem]

    step 4

    Step 4


    [/tabitem]

    [tabitem]

    step 5

    Step 5


    [/tabitem]

    The problem now is that we have a pom.xml containing old versions of dependencies.
    We can easily check that by looking at the pom.xml file in our editor and by examining the Effective POM (right clink on pom.xml –> Maven –> Show Effective POM)

      [tabitem]
      pom1

      pom1


      [/tabitem]
      [tabitem]
      pom2

      pom2


      [/tabitem]
      [tabitem]
      pom3

      pom3


      [/tabitem]

      The next step is to go to the folder of our project and execute the following maven command (If you don’t have maven you can install it using Homebrew and the command ‘brew install maven’) :

      mvn clean install

      Then you’ll get the following result :

      mvn clean install

      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building com.skiabox.app10 1.0-SNAPSHOT
      [INFO] ————————————————————————
      [INFO]
      [INFO] — maven-clean-plugin:3.0.0:clean (default-clean) @ com.skiabox.app10 —
      [INFO] Deleting /Users/Administrator/IdeaProjects/NextGenerationProject1/target
      [INFO]
      [INFO] — maven-resources-plugin:2.7:resources (default-resources) @ com.skiabox.app10 —
      [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
      [INFO] skip non existing resourceDirectory /Users/Administrator/IdeaProjects/NextGenerationProject1/src/main/resources
      [INFO]
      [INFO] — maven-compiler-plugin:3.3:compile (default-compile) @ com.skiabox.app10 —
      [INFO] Changes detected – recompiling the module!
      [INFO] Compiling 1 source file to /Users/Administrator/IdeaProjects/NextGenerationProject1/target/classes
      [INFO]
      [INFO] — maven-resources-plugin:2.7:testResources (default-testResources) @ com.skiabox.app10 —
      [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
      [INFO] skip non existing resourceDirectory /Users/Administrator/IdeaProjects/NextGenerationProject1/src/test/resources
      [INFO]
      [INFO] — maven-compiler-plugin:3.3:testCompile (default-testCompile) @ com.skiabox.app10 —
      [INFO] Changes detected – recompiling the module!
      [INFO] Compiling 1 source file to /Users/Administrator/IdeaProjects/NextGenerationProject1/target/test-classes
      [INFO]
      [INFO] — maven-surefire-plugin:2.19:test (default-test) @ com.skiabox.app10 —

      ——————————————————-
      T E S T S
      ——————————————————-
      Running com.skiabox.apps.AppTest
      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec – in com.skiabox.apps.AppTest

      Results :

      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

      [INFO]
      [INFO] — maven-jar-plugin:2.6:jar (default-jar) @ com.skiabox.app10 —
      [INFO] Building jar: /Users/Administrator/IdeaProjects/NextGenerationProject1/target/com.skiabox.app10-1.0-SNAPSHOT.jar
      [INFO]
      [INFO] — maven-install-plugin:2.5.2:install (default-install) @ com.skiabox.app10 —
      [INFO] Installing /Users/Administrator/IdeaProjects/NextGenerationProject1/target/com.skiabox.app10-1.0-SNAPSHOT.jar to /Users/Administrator/.m2/repository/com/skiabox/apps/com.skiabox.app10/1.0-SNAPSHOT/com.skiabox.app10-1.0-SNAPSHOT.jar
      [INFO] Installing /Users/Administrator/IdeaProjects/NextGenerationProject1/pom.xml to /Users/Administrator/.m2/repository/com/skiabox/apps/com.skiabox.app10/1.0-SNAPSHOT/com.skiabox.app10-1.0-SNAPSHOT.pom
      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 4.567 s
      [INFO] Finished at: 2015-11-10T15:03:43+02:00
      [INFO] Final Memory: 17M/168M
      [INFO] ————————————————————————

      Our next move is to run in the same folder the following command:

      mvn archetype:create-from-project

      The result of this action is the following description in our console window:

      mvn archetype:create-from-project

      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building com.skiabox.app10 1.0-SNAPSHOT
      [INFO] ————————————————————————
      [INFO]
      [INFO] >>> maven-archetype-plugin:2.4:create-from-project (default-cli) > generate-sources @ com.skiabox.app10 >>>
      [INFO]
      [INFO] <<< maven-archetype-plugin:2.4:create-from-project (default-cli) < generate-sources @ com.skiabox.app10 <<<
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:create-from-project (default-cli) @ com.skiabox.app10 —
      [INFO] Setting default groupId: com.skiabox.apps
      [INFO] Setting default artifactId: com.skiabox.app10
      [INFO] Setting default version: 1.0-SNAPSHOT
      [INFO] Setting default package: com.skiabox.apps
      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building com.skiabox.app10-archetype 1.0-SNAPSHOT
      [INFO] ————————————————————————
      [INFO]
      [INFO] — maven-resources-plugin:2.7:resources (default-resources) @ com.skiabox.app10-archetype —
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 15 resources
      [INFO]
      [INFO] — maven-resources-plugin:2.7:testResources (default-testResources) @ com.skiabox.app10-archetype —
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 2 resources
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:jar (default-jar) @ com.skiabox.app10-archetype —
      [INFO] Building archetype jar: /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/com.skiabox.app10-archetype-1.0-SNAPSHOT
      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 2.103 s
      [INFO] Finished at: 2015-11-10T18:06:05+02:00
      [INFO] Final Memory: 12M/155M
      [INFO] ————————————————————————
      [INFO] Archetype project created in /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype
      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 6.371 s
      [INFO] Finished at: 2015-11-10T18:06:06+02:00
      [INFO] Final Memory: 15M/155M
      [INFO] ————————————————————————

      Now that we have created the archetype we type from the directory that the archetype resides (/target/generated-sources/archetype) the following command :

      
      mvn install
      
      

      The result of this action is that we installed this archetype inside our local maven repository (console output below)

      mvn install
      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building com.skiabox.app10-archetype 1.0-SNAPSHOT
      [INFO] ————————————————————————
      [INFO]
      [INFO] — maven-resources-plugin:2.7:resources (default-resources) @ com.skiabox.app10-archetype —
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 15 resources
      [INFO]
      [INFO] — maven-resources-plugin:2.7:testResources (default-testResources) @ com.skiabox.app10-archetype —
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 2 resources
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:jar (default-jar) @ com.skiabox.app10-archetype —
      [INFO] Building archetype jar: /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/com.skiabox.app10-archetype-1.0-SNAPSHOT
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:integration-test (default-integration-test) @ com.skiabox.app10-archetype —
      [INFO] Processing Archetype IT project: basic
      [INFO] —————————————————————————-
      [INFO] Using following parameters for creating project from Archetype: com.skiabox.app10-archetype:1.0-SNAPSHOT
      [INFO] —————————————————————————-
      [INFO] Parameter: groupId, Value: archetype.it
      [INFO] Parameter: artifactId, Value: basic
      [INFO] Parameter: version, Value: 0.1-SNAPSHOT
      [INFO] Parameter: package, Value: it.pkg
      [INFO] Parameter: packageInPathFormat, Value: it/pkg
      [INFO] Parameter: version, Value: 0.1-SNAPSHOT
      [INFO] Parameter: package, Value: it.pkg
      [INFO] Parameter: groupId, Value: archetype.it
      [INFO] Parameter: artifactId, Value: basic
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic/.idea/libraries/Maven__junit_junit_4_12.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic/.idea/copyright/profiles_settings.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic/.idea/dictionaries/Administrator.xml
      [INFO] project created from Archetype in dir: /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic
      [INFO]
      [INFO] — maven-install-plugin:2.5.2:install (default-install) @ com.skiabox.app10-archetype —
      [INFO] Installing /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/target/com.skiabox.app10-archetype-1.0-SNAPSHOT.jar to /Users/Administrator/.m2/repository/com/skiabox/apps/com.skiabox.app10-archetype/1.0-SNAPSHOT/com.skiabox.app10-archetype-1.0-SNAPSHOT.jar
      [INFO] Installing /Users/Administrator/IdeaProjects/NextGenerationProject1/target/generated-sources/archetype/pom.xml to /Users/Administrator/.m2/repository/com/skiabox/apps/com.skiabox.app10-archetype/1.0-SNAPSHOT/com.skiabox.app10-archetype-1.0-SNAPSHOT.pom
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:update-local-catalog (default-update-local-catalog) @ com.skiabox.app10-archetype —
      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 2.587 s
      [INFO] Finished at: 2015-11-17T01:08:59+02:00
      [INFO] Final Memory: 14M/220M
      [INFO] ————————————————————————

      Now it is very easy to use this new archetype.Just move to a new blank folder and use the new archetype by using the following command :

      mvn archetype:generate -DarchetypeCatalog=local
      

      Using this command we can choose between any of the local maven archetypes that we have previously created. As you see in the following image we pick the second archetype in the list, which is the archetype we’ve just created

      mvn archetype:generate -DarchetypeCatalog=local
      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building Maven Stub Project (No POM) 1
      [INFO] ————————————————————————
      [INFO]
      [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
      [INFO]
      [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
      [INFO]
      [INFO] — maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom —
      [INFO] Generating project in Interactive mode
      [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
      Choose archetype:
      1: local -> com.skiabox.apps:com.skiabox.app6-archetype (com.skiabox.app6-archetype)
      2: local -> com.skiabox.apps:com.skiabox.app10-archetype (com.skiabox.app10-archetype)
      Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 2
      Define value for property ‘groupId’: : com.skiabox.apps
      Define value for property ‘artifactId’: : com.skiabox.app15
      Define value for property ‘version’: 1.0-SNAPSHOT: :
      Define value for property ‘package’: com.skiabox.apps: :
      Confirm properties configuration:
      groupId: com.skiabox.apps
      artifactId: com.skiabox.app15
      version: 1.0-SNAPSHOT
      package: com.skiabox.apps
      Y: :
      [INFO] —————————————————————————-
      [INFO] Using following parameters for creating project from Archetype: com.skiabox.app10-archetype:1.0-SNAPSHOT
      [INFO] —————————————————————————-
      [INFO] Parameter: groupId, Value: com.skiabox.apps
      [INFO] Parameter: artifactId, Value: com.skiabox.app15
      [INFO] Parameter: version, Value: 1.0-SNAPSHOT
      [INFO] Parameter: package, Value: com.skiabox.apps
      [INFO] Parameter: packageInPathFormat, Value: com/skiabox/apps
      [INFO] Parameter: package, Value: com.skiabox.apps
      [INFO] Parameter: version, Value: 1.0-SNAPSHOT
      [INFO] Parameter: groupId, Value: com.skiabox.apps
      [INFO] Parameter: artifactId, Value: com.skiabox.app15
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/TempProject/com.skiabox.app15/.idea/libraries/Maven__junit_junit_4_12.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/TempProject/com.skiabox.app15/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/TempProject/com.skiabox.app15/.idea/copyright/profiles_settings.xml
      [WARNING] Don’t override file /Users/Administrator/IdeaProjects/TempProject/com.skiabox.app15/.idea/dictionaries/Administrator.xml
      [INFO] project created from Archetype in dir: /Users/Administrator/IdeaProjects/TempProject/com.skiabox.app15
      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 43.142 s
      [INFO] Finished at: 2015-11-17T01:29:53+02:00
      [INFO] Final Memory: 15M/220M
      [INFO] ————————————————————————

      The result is a complete maven project with updated dependencies.You can open this project directly from inside IntelliJ Idea by just opening its pom.xml file.
      Of course we can update our maven dependencies automatically (see this link) but it is not recommended to do so.
      In the following console screenshot you can see the result of a tree command (brew install tree) inside our new project that confirms that we have just created a full maven project.

      Administrator@Stavross-MacBook-Pro:~/IdeaProjects/TempProject/com.skiabox.app15$ tree
      .
      ├── NextGenerationProject1.iml
      ├── pom.xml
      └── src
      ├── main
      │   └── java
      │   └── com
      │   └── skiabox
      │   └── apps
      │   └── App.java
      └── test
      └── java
      └── com
      └── skiabox
      └── apps
      └── AppTest.java

      11 directories, 4 files

      %d bloggers like this: