Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create a Custom Maven Archetype

DZone's Guide to

How to Create a Custom Maven Archetype

Creating a Maven archetype can be a good way to streamline beginnings of new projects in your company. Check out the guide on how to do it in practice.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

In this article, I will show you how to create your own Archetype in Maven so it can fit your company coding structure template. To do this, follow these steps:

  1. Create a Maven project using Eclipse to be used as your custom archetype.
  2. I named it customArchitype.
  3. Its pom.xml looks like:
<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>com.example</groupId>
    <artifactId>customArchitype</artifactId>
    <version>1.0.Example</version>
</project>
  1. Then create a folder structure like META-INF/maven under src/main/resources.
  2. Now, your project structure should look like: src/main/resources/META-INF/maven
  3. After that, create a file called archetype.xml under the aforesaid folder. This file will look like the following:
<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/customArchitype/CustomExample.java</source>
    </sources>
    <testSources>
        <source>src/test/java/customArchitype/CustomExampleTest.java</source>
    </testSources>
</archetype>


This archetype.xml is the most important part, which tells us about the project structure it creates. So here, you can customize the project structure to fit into your organization.

I will discuss each tag I used in this XML below:
Tag name
Description
<id>customArchitype</id>
This is the tag that points your custom archetype project when you do an archetype:generate. It will find the archetype project based on that, so it will be the artifact id of the pom.xml — in my case, customArchitype. The artifact is customArchitype. Always use your custom archetype project’s artifact ID.
<source>src/main/java/customArchitype/CustomExample.java</source>
This tells us about the src folder hierarchy that will be generated. To do that, we have to create a folder called archetype-resources under
src/main/resources and then create a Java class called CustomExample.java maintaining the hierarchy written under the <source> tag
<testSources>
   <source>src/test/java/customArchitype/CustomExampleTest.java</source>
 </testSources>
This tells the test folder hierarchy which will be generated. To do that, we have to create a folder called archetype-resources under
src/main/resources and then create a Java class called CustomExampleTest.java maintaining the hierarchy written under the <source> tag


CustomExample.java file:

package ${groupId}.customArchitype;


public class CustomExample {
    public void printName(String name) {
        System.out.println("Hello" + name);
    }
}


CustomExampleTest.java file:

package ${groupId}.customArchitype;


public class CustomExampleTest {
    public static void main(String[] args) {
        CustomExample example = new CustomExample();
        example.printName("Shamik");
    }
}

Placeholder

Please note that in the package statement I wrote package ${groupId}.customArchitype;  because when we give the group Id in our archetype.generate command, our sample project creates that package and places the files under that, so I want to dynamically replace the package value. Hence I wrote a placeholder, ${groupId}, which actually replaces the value of the group id, which we will pass when invoking the archetype:generate command.

Now, the last step is to create a template of the pom.xml file, which will be created when the project is created by the archetype: generate command. Also, that pom.xml will be placed under archetype-resources.
<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>jar</packaging>
    <name>Custom Archetype</name>
    <url>http://javaonfly.blogspot.in/</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


Please note that in the groupId and artifactId tag, I used the placeholder for the same reason stated above. But this is an important thing to do, unless the generated POM will not replace the group id and artifact id given in the archetype: generate command.

Overall Structure

Test

Now if you run the following command:
mvn archetype:generate -DarchetypeGroupId=com.example -DarchetypeArtifactId=customArchitype
    -DarchetypeVersion=1.0.Example -DgroupId=com.example -DartifactId=newArchitype  


It will create a new project called newArchitype.

Here's a generic version of this command:

mvn archetype:generate -DarchetypeGroupId=<custom-archetype group id>
    -DarchetypeArtifactId=<custom-archetype artifactid>
    -DarchetypeVersion=<custom-archetype version>
    -DgroupId=<new project Group Id>
    -DartifactId=<new project artifact Id>  

   

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
maven archetype ,maven placeholders ,java ,customization ,tutorial

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}