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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • A Maven Archetype for Jakarta EE 10 Applications
  • Creating a Maven Archetype From an Existing Project
  • Using Python Libraries in Java
  • DGS GraphQL and Spring Boot

Trending

  • Designing a Java Connector for Software Integrations
  • AI’s Role in Everyday Development
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Agile and Quality Engineering: A Holistic Perspective
  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 
Shamik Mitra user avatar
Shamik Mitra
·
Mar. 21, 17 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
41.0K 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 Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Maven Archetype for Jakarta EE 10 Applications
  • Creating a Maven Archetype From an Existing Project
  • Using Python Libraries in Java
  • DGS GraphQL and Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!