A Maven Custom Archetype
Tags: java, opensource, productivity
Published: 2019-08-10 17:36:14

###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**
<archetype-descriptor name="myOwn-archetype">
 
    <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>
 
</archetype-descriptor>

<b>*requiredProperties*</b> 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.

<b>*fileSets*</b> 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 <b>client-name</b> is required property and i can used this property in any file of filtered fileSets directory.

<b>*modules*</b> tag helps you to have a <b>multi-module</b> project template.

for other information, please have a look at the [Apache docs](https://maven.apache.org/guides/mini/guide-creating-archetypes.html) 


###4. Make it Available
![Nexus Repo](https://docs.releng.linuxfoundation.org/en/latest/_images/nexus2-ui.png)
After a setup of custom archetype you need to make this available for other co-employees. So to do this we have a <b>*distributionManagement*</b> 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](https://maven.apache.org/pom.html#Distribution_Management)

###5. A shortcut way for creating Archetype
![Shortcut-image](https://cdn.shopify.com/s/files/1/2780/3536/files/chaim-billingham-header_1024x1024.jpg?v=1519303740)
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](https://twitter.com/IamJAveed59)