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="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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.rhuanrocha</groupId>
  <artifactId>example-archetype</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>example-archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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:

src/main/resources/META-INF/maven

Below we have an example of this file.

<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
    <id>customArchitype</id>
    <sources>

        <source>src/main/java/resource/HelloWorldResource.java</source>

    </sources>
    <resources>
        <resource>src/main/webapp/WEB-INF/beans.xml</resource>
    </resources>

</archetype>

Now, I’ll create a following folder:

               src/main/resources/archetype-resource

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="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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
    <packaging>war</packaging>
    <name>Example Archetype</name>
    <url>https://rhuanrocha.net/</url>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <jakartaee>8.0</jakartaee>
        <junit>3.8.1</junit>

    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${jakartaee}</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
</project>

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;
import javax.ws.rs.*;

@Path("helloWorld")
public class JobResource {

    @Inject
    private JobBusiness jobBusiness;

    @GET
    public Response sayHelloWorld(){

        return Response
                .ok( "Hello World")
                .build();
    }    

}

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};

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("resources")
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:

WordPress.com Logo

You are commenting using your WordPress.com 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