DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From Repetition to Reusability: How Maven Archetypes Save Time
  • A Maven Archetype for Jakarta EE 10 Applications
  • Creating a Maven Archetype From an Existing Project
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Trending

  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  1. DZone
  2. Coding
  3. Java
  4. How to Create a Custom Maven Archetype

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.

By 
Shaamik Mitraa user avatar
Shaamik Mitraa
·
Mar. 21, 17 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
41.4K Views

Join the DZone community and get the full member experience.

Join For Free

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>  

   

Archetype (information science) Apache Maven

Published at DZone with permission of Shaamik Mitraa. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From Repetition to Reusability: How Maven Archetypes Save Time
  • A Maven Archetype for Jakarta EE 10 Applications
  • Creating a Maven Archetype From an Existing Project
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook