Maven Custom Archetype and PlaceHolder

Maven Custom Archetype and PlaceHolder


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  following steps need to be followed

  1. Create a maven Project using Eclipse which is used as your Custom archetype.
  2. I named it customArchitype.
  3. It’s pom.xml look like following
<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 META-INF/maven under src/main/resources.
  2. Now project structure look  like src/main/resources/META-INF/maven
  3. After that create a file called archetype.xml under aforesaid folder.

This file will look like following
archetype.xml
<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 about the project structure it creates. So here you can customize the Project Structure which is fit in your organization.

I will discuss each tag I used in this XML in a tabular format.



Tage name
Description
<id>customArchitype</id>
TThis is the tag which points your custom archetype project when you do archetype: generate it will find archetype project based on that. So it will be the artifact id of the pom.xml in my case customArchitype project. The artifact is customArchitype. Always use your custom archetype project’s artifact ID.
<source>src/main/java/customArchitype/CustomExample.java</source>
This tells about the src folder hierarchy which will be generated. To do that we have to create a folder call  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 call  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 package statement I wrote
package ${groupId}.customArchitype;


Because when we give group Id in archetype.generate command our sample project create that package and place the files under the same so I want to dynamically replace package value, Hence I wrote a placeholder ${groupId} which actually replace the value of group id which we will pass when invoking archetype: generate command.

7. Now The last step is to create a template of pom.xml which will be created when the project is created by archetype: generate command.

Also, that pom.xml will be placed under archetype-resources.




Template 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/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 generated pom will not replace group id and artifact id given in archetype: generate command.

Overall Structure:

Maven custom Archetype directory structure



Test:

Now if you run 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

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>     

This new project group id will replace all placeholders.