Creating a Custom Maven Archetype

In this post I will cover to you how to create a custom maven archetype. In our example we’ll create an archetype to WAR project. To this post we’ll assume that you already install Java and Maven in your machine.

Create a custom maven archetype is a good practices to:

  • Promote a pattern of architecture to projects.
  • Turn easy the task to create new projects

In an enterprise environment, we may want to create a pattern of architecture to projects. To this, a maven archetype may help us to do that, because maven archetype permit us create a project skeleton with all dependencies, packages and configurations defined, and the developer can use this skeleton in your projects as base, with a easy way.

To facilitate us, we’ll create a new maven project and then will modify this project to creating our archetype. To create a new maven project run the following command:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=net.rhuanrocha -DartifactId=example-archetype

After running this command, a new project will be created with the following pom.xml.

<project xmlns="" xmlns:xsi=""

Now, we need create a file called archetype.xml. This file describe the structure of the project generated by this archetype. This file need be created in the following folder:


Below we have an example of this file.

<archetype xmlns="" xmlns:xsi=""




Now, I’ll create a following folder:


This folder will have the skeleton used when a new project is create using this archetype. Inside this folder, we’ll have the classes, packages and configurations that will be generated by archetype. With this, inside this folder we’ll put the pom.xml and the src folder with all classes and configuration files. Below, we have a example of pom.xml inside archetype-resource.

<project xmlns="" xmlns:xsi=""
    <name>Example Archetype</name>



Note that the pom.xml uses the parameter ${groupId}, ${artifactId} and ${version} that are parameters sent when the command running to create a project using this archetype. The command to generate new project using this archetype will be covered ahead.

Now, I’ll create the class HelloWorldResource. This class will be inserted on the package that its name begin with value sent on the ${groupId} parameter. Below, we have the example:

package ${groupId}.resource;

import javax.inject.Inject;

public class JobResource {

    private JobBusiness jobBusiness;

    public Response sayHelloWorld(){

        return Response
                .ok( "Hello World")


Note that the parameter ${groupId} is used to define the package name.

Now I’ll create the JAXSConfiguration class. Below we have a example:

package ${groupId};


public class JAXRSConfiguration extends Application {


Now, to install this archetype on your local repository, run the following command:

mvn clean install

Now, to create a new project using the custom maven archetype created, run the following command:

mvn archetype:generate -DarchetypeGroupId=net.rhuanrocha -DartifactId=example-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=<group id> -DartifactId=<name of your artifact>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s