DEV Community

Cover image for A Maven Custom Archetype
JAveedMeandad
JAveedMeandad

Posted on

A Maven Custom Archetype

1. What is Maven Archetype?

A maven archetype is a simple artifact and it contains your project related files and configuration. The main benefit of using archetypes is to standardize project development and to enable developers to easily follow best practices while bootstrapping their projects faster.

2. Why we require custom archetype?

It's Just Simple
When we are developing an application that comes up based some other projects and we required some files to get information and to access their functionality.

So many configuration files may consists in your dependent project like web.xml,pom.xml and etc..,

The pom files consists of all dependencies which your required from other projects.

Here your project template is fixed and you know what files you need and where you need place these files. At First you created it by your own but later it is a repeated work when you are developing new project with same project Template. So the maven custom archetype helps you to create fully automated,customized project template.

3. What consists inside of Archetypes?

An archetype is made up of one major configuration file called archetype-metadata.xml and its available in src/main/resources/META-INF/maven/ and there is a folder called src/main/resources/archetype-resources/ it contains all your project related files where you need make customization.

Even a archetype is created by a project so we also have a Root pom.xml in the base location of archetype project.

Maven Archetype Descriptor

<requiredProperties>
    <requiredProperty key="version">
        <defaultValue>1.0</defaultValue>
    </requiredProperty>
    <requiredProperty key="client-name"/>
</requiredProperties>

<fileSets>
    <fileSet filtered="true" packaged="true">
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.java</include>
        </includes>
    </fileSet>
     <fileSet filtered="true" packaged="true">
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include>
        </includes>
    </fileSet>
</fileSets>

<modules>
    <module name="service-module"></module>
    <module name="web-module"></module>
</modules>
Enter fullscreen mode Exit fullscreen mode

requiredProperties tag is used to maintain any configurable parameters that you need in your project. If its set as default value it will be used other wise it will prompted while creating project.

fileSets will be used to maintain folders. A filtered file means that placeholders will be substituted with provided values during the generation process. So in our project client-name is required property and i can used this property in any file of filtered fileSets directory.

modules tag helps you to have a multi-module project template.

for other information, please have a look at the Apache docs

4. Make it Available

Nexus Repo
After a setup of custom archetype you need to make this available for other co-employees. So to do this we have a distributionManagement tag in root pom.xml file where you can maintain your repository to deploy the generated archetype and finally it will be available to others also.

for details how to make a configuration for Nexus, Please check the site Distribution Management in Maven

5. A shortcut way for creating Archetype

Shortcut-image
As i told earlier that first time you created it by your own by using general method to create project using maven. You have your project template ready so it is very simple and only one command you need to use and that is a in-built maven plugin mvn archetype:create-from-project.

Go to your project template root folder and execute the above command maven automatically create a custom archetype and it will be available at the target location of base project template.

That's all for the post i will come back with another useful post soon. I would like to see your feedback and suggestions.

You can follow me on Twitter

Top comments (1)

Collapse
 
citronbrick profile image
CitronBrick

You can enable xml syntax highlighting for your archetype descriptor using

(triple backtick) xml