Creating a new Maven Archetype and Publishing it to Nexus

This page will describe the process to create and upload a new archetype to the repository.

We will be creating an archetype of a basic spring MVC application that is pre-configured to make a test connection to a MySQL database. We will run this application using the built in servlet engine in Maven called jetty.

Before starting please review the following article to learn how to publish artifacts to remote repositories.


Create a Archetype Template

We start by creating an archetype from well…. an archetype…

Execute the following command

mvn archetype:create -DgroupId=test -DartifactId=test-archetype -DarchetypeArtifactId=maven-archetype-archetype

cd /test-archetype

Code Your Application

In this section we will take some time to code the project that we will install into this archetype. Since creating a Spring MVC application is beyond the scope of this page please navigate to the following page to see how this application was coded. We will move this application to this archetype so that it can be used as a starting point for new projects.

MySQL Test Connection using Spring MVC

Import files into the new archetype

At this time you can copy the files from the project you created using the above link into the src/main/resources/archetype-resources/

Before we can proceed we need to make some small changes to the project we just created and imported into this archetype.

These changes are necessary because when developers create project based on archetypes they are asked a few questions.

  • group id
  • artifact id
  • version

The group id is important typically its (com.yourcompanyname). Since developers can provide anything in this field you need to modify your template code to take this parameter and place your code into the right java packages.

Modify the project’s pom.xml

Before we can proceed we need to modify the template project’s pom.xml file. Replace the groupId artifactId and version tag’s with the following


The above step is necessary since the developers that are using your archetype to create their own project can use any group artifact and version. When the project generates maven needs to create a pom.xml file for the project with the values that were specified by the developer.

Modify the archetype descriptor

The archetype descriptor contains a list of files that are part of the archetype we are creating. It is located in the following directory.



Modify the source files

During the creation of a project maven asks the user for the groupId. Maven uses this to place the source files into packages that represent the user’s groupId. For this reason we need to modify each of the source packages and change them to look like this…

package ${package}.test;

So go ahead and modify the three java source files and spring-servlet.xml. replace the package statement with what you see above.

  • src/main/java/test/DBParam.java
  • src/main/java/test/HelloWorldController.java
  • src/main/java/test/TestDBConnectionController.java
  • src/main/webapp/WEB-INF/spring-servlet.xml

Relocate Source If Necessary

Also you may need to relocate the .java files so they represent the package statement. This means if you replaced the following with a ${package} statement then everything under that must be moved to the highlevel folder.

com.mycompany.mygroup.program -> ${package}.program

In the above example the program package and everything under it needs to be moved to the top level (/src/main/java) folder.

The xml file needs to be modified because we need to tell spring to scan a different package for controller beans.

        <context:component-scan base-package="${package}.test"/>

Installing the archetype into Nexus

Make sure the nexus repository is up and running.

Before we publish the archetype we need to define the location of the Nexus repository.

We do this in the archetype’s pom.xml file

After adding this section your pom.xml file should look like this.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <name>Archetype - test-archetype</name>


                <name>Internal Snapshots</name>
mvn deploy

Open up the Nexus repository and verify that the archetype has been added. If it has not please review the instructions above before continuing.

Running the Archetype

In this section we will generate a project from an archetype we just created above.

open up the command line and navigate to a clean working directory.

mvn archetype:create \
  -DarchetypeGroupId=test \
  -DarchetypeArtifactId=test-archetype \
  -DarchetypeVersion=1.0-SNAPSHOT \
  -DgroupId=yourgroup \

You can also use the new format to generate a project form an archetype:

mvn archetype:generate \
  -DarchetypeGroupId=test \
  -DarchetypeArtifactId=test-archetype \

Running your newly generated project

cd your-project
mvn jetty:run

At this point you can navigate to all the urls listed below:

Publishing your Archetype to a Website

Good News! You don’t need a Nexus server. Maven artifacts can be published to plain old web sites.

To test this I have uploaded the test-archetype to the following location and used the following command to generate a project based on the archetype.

mvn archetype:generate \
  -DarchetypeGroupId=test \
  -DarchetypeArtifactId=test-archetype \
  -DarchetypeVersion=1.0-SNAPSHOT \
  -DarchetypeRepository=http://your static site hostname/directory of your repository

Maven makes a Http connection to the above archetypeRepository and gets all the data necessary to create the project.


As you can see creating an archetype is not much difficult. You can use this technique to create templates of small projects you find yourself creating often. Archetypes can be posted not only to Nexus repositories but static html sites as well.

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

2 Responses to “Creating a new Maven Archetype and Publishing it to Nexus”

  1. 1 Kunal
    July 26, 2013 at 4:47 am

    Thanks for the static site info in the end.

    • January 15, 2014 at 1:08 pm

      I have done all of this. I can see it in nexus through the admin console, but can’t generate a project. Is there any chance you could post your settings.xml. I have a very basic one that was given to me, and not sure it is correct.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

November 2009
« Oct   Dec »

Blog Stats

  • 846,580 hits

%d bloggers like this: