Computing Magazine

Create Your Own Maven Archetype

Posted on the 17 November 2015 by Skiabox @skiabox
Create your own maven archetype

[vc_row][vc_column][vc_column_text]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 and
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 :

[/vc_column_text][/vc_column][/vc_row][vc_row el_class="minimum_image_height1″][vc_column][cq_vc_tabs tabstitle="Step 1,Step 2,Step 3,Step 4,Step 5″ tabsicon="fa-caret-right,fa-caret-right,fa-caret-right,fa-caret-right,fa-caret-right" rotatetabs="15″]


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)

[/vc_column_text][/vc_column][/vc_row][vc_row el_class="minimum_image_height2″][vc_column][cq_vc_tabs tabstitle="Before 1,Before 2,After" tabsicon="fa-chevron-right,fa-chevron-right,fa-chevron-right" rotatetabs="15″][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 :[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][code language="" gutter="false"]mvn clean install

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] - 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] - 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] ------------------------
[INFO] Total time: 4.567 s
[INFO] Finished at: 2015-11-10T15:03:43+02:00
[INFO] Final Memory: 17M/168M
[INFO] ------------------------[/code][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

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:

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][code language="" gutter="false"]mvn archetype:create-from-project

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)

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

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.

11 directories, 4 files

Back to Featured Articles on Logo Paperblog