Monday, March 31, 2014

Create Apache Maven archetype from scratch

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.

You have two options to create your own archetype.

  • ->Create archetype from scratch
  • ->Create archetype from existing project

I have browsed internet to find a clue about creating maven archetype from  scratch.I couldn't find many resources for it but I found many resources for creating archetype from existing project.

In this post I will discuss how to create own maven archetype, host it in remote repository and use it to generate project

Before start creating the archetype you have to determine the source files and resources which should be there in the project once it will be crated using your archetype.

1. There is a standard file structure for maven archetype . You have to create root folder, sub folders and few files inside it (Diagram 1.0 shows the file structure inside a maven archetype)

Explanation: 

Our root folder is "archetype". Inside that we have "src" folder which include the resources to be included in our archetype and "pom.xml" file. There is "main" folder inside "src" folder. It includes "resources" folder. "archetype-resources" folder inside it includes all resources to be included in the archetype project. "META-INF" includes "maven" folder which includes "archetype-metadata.xml" file. It has the configuration details for manipulating files when generating a project. You have to add "pom.xml" file for your desired project into the "archetype-resources" folder and all source scripts and test scripts in to the "src" folder inside it. All the resource file/folders to be included to generated projects should go to resources folder inside "src" folder as shown in the diagram. "archetype-resources" folder has the exact file structure to go to generated project using your archetype.

Now lets have a close look
  • archetype/pom.xml

Note: Green colored components are optional. You have to include it when you are going to deploy your archetype to remote repository.

Explanation:

There are some important attribute you should have to configured in this pom.xml file
  1. groupId - Group ID of your  archetype
  2. artifactId - This should be unique
  3. version - Different versions of same artifact (archetype) can be available. So this attribute would be used to distinguish them.
Above three attributes altogether used to call your archetype.Let's see how the things working under the hood. When you try to generate project using your archetype (using mvn  archetype:generate command - this will be explained later in this tutorial) , the archetype plugin will look at the a xml file called catalog (local or remote) where knowledge about archetypes is stored. Catalog distinguish archetypes using above three attributes.
  • archetype/src/main/resources/archetype-resources

This folder contains the exact file structure of the resultant project. You can make simple maven project ( contains pom.xml, src, test, resources folders ) and copy-paste it in to here. You can include all the resource files and test scripts into this structure.
  •  archetype/src/main/resources/META-INF/maven/archetype-metadata.xml

The metadata about an archetype is stored in the archetype-metadata.xml file located in the META-INF/maven directory of its jar file. This is a reference for the Archetype descriptor used to describe archetypes's metadata.

Note: Green colored component is optional. Here we have defined 3 attributes. Users are forced to give those 3 attributes when creating projects using this archetype. Custom maven parameters can be achieved using this method.

2. After create the file structure you have to build the project. Go to the project root folder "archetype" and build the project using

command.Then you can find "target" folder inside the project root folder which includes "jar" file. The name of the jar file is <artifactId>+<version>.jar. (artifactId and version are given as your parent pom.xml file)

3. Now your archetype has been added to your local catalog. If you want to add your archetype to a remote repository (remote maven nexus repo) you have to deploy your archetype. Execute following maven command giving appropriate configuration details.


4. If you have successfully deployed your archetype into a remote repository (or local repository ) you can use that archetype to generate projects. Goto to a desired location and execute following maven command to generate project.

Explanation:

->Remote repository details
DarchetypeRepository - remote repository where the archetype is deployed

->Archetype details
DarchetypeGroupId - group id of the archetype
DarchetypeVersion - version of the archetype to be used
DarchetypeArtifactId - artifact id of the archetype to be used

->New project details
DartifactId - artifact id of project to be created
DgroupId - group id of the project to be created
Dpackage - package name in the project to be created

If you have given above parameters correctly it will generate a new project to you location.


No comments:

Post a Comment