Creating Custom Archetype for maven projects

santosh
3 min readFeb 29, 2020

--

While working on enterprises projects. Quite often we need to have our own archetype to create custom maven projects with our internal dependencies

This speed up our development

To add common code or framework dependencies

Below is step wise guide to achieve this

Steps 1:

Maven has already provided one new archetype maven-archetype-archetype to create new archetypes.

Use below command to create a maven project named custom-archetype with group com.custom and version 1.0

mvn archetype:generate -B -DarchetypeArtifactId=maven-archetype-archetype -DgroupId=com.custom -DartifactId=custom-archetype -Dversion=1.0

Understanding each command

  1. archetype:generate : To create a new project based on an Archetype, you need to call. For detail refer here -> link
  2. archetypeArtifactId :An archetype to generate a sample archetype project.
  3. groupId : uniquely identifies your project across all projects. A group ID should follow Java’s package name rules. This means it starts with a reversed domain name you control. For example,org.apache.maven, org.apache.commons
  4. version : iIf you distribute it, then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, …).

Step 2:

Import the above project in IDE (eclipse, intellijIdea, netbeans etc). It looks like below

We need to do couple of changes to make it work

  1. Change the file name archetype.xml to archetype-metadata.xml

Also we need to modify the content of this xml as per our requirement, below is the sample content of archetype-metadata.xml

The requiredProperties section will declare all the properties that required along with their default values while generating the project from this archetype.

The fileSets will declare the files that will be placed in the final project that will be generated.

Additionally we can update the file App.java (at location /src/main/resources/archetype/resources/src/main/java/App.java) to be generated under custom package as per input we can do below changes to file at location

package $package;
/**
* Hello world!*/public class App{public static void main( String[] args ){
System.out.println( "Hello World!" );
}
}

Same goes for the test file.

Step3:

We also need to change the pom.xml file under archetype-resources folder to accept the runtime GroupId, ArtifactId, Version coordinates . We need to change it to placeholder in order to accept runtime values, for the project we will generate with this archetype.

The pom.xml under archetype-resources should look 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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>${artifactId}</name>
<dependencies>
<!-- sample guava dependencies now this will come in all project created with this archetype-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Step4:

Now we are all set to install our custom archetype via below command

mvn install

Step 5.

Create maven project using this archetype

mvn archetype:generate -DarchetypeCatalog=local -DarchetypeArtifactId=custom-archetype -DarchetypeGroupId=com.custom.archetype -DarchetypeVersion=1.0

Maven will start interactive mode and ask for all required properties regarding new maven project. Properties with default values are skipped but the default value can be overridden if you do not confirm the configuration in the last step.

Custom archetypes inputs for example:

Once done we can verify the maven project being created as per our need.

Reference

  1. https://maven.apache.org/guides/introduction/introduction-to-archetypes.html
  2. http://maven.apache.org/guides/mini/guide-creating-archetypes.html

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

santosh
santosh

No responses yet

Write a response