Creating custom archetypes in Maven
Tags: maven, java, programming
Published: 2022-07-31 11:15:00



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erb3jp7eilqasu2nhfyp.png)As developers we embrace concepts like DRY (Don’t Re-invent Yourself). When we are creating similar things repetitively, we always prepare a template for that thing, so instead of starting a project from scratch, you can have a preconfigured directory structure. And in those directories you can place your template source files. Eg: struts.xml, spring config file, web.xml, tiles.xml etc..

## Creating your archetype

Run the following command:

```console
mvn archetype:generate \
-DgroupId=[your project's group id] \
-DartifactId=[your project's artifact id] \
-DarchetypeArtifactId=maven-archetype-archetype
```
This will create following default directory structure for creating archetypes.

```bash
archetype
|-- pom.xml
`-- src
    `-- main
        `-- resources
            |-- META-INF
            |   `-- maven
            |       `--archetype.xml
            `-- archetype-resources
                |-- pom.xml
                `-- src
                    |-- main
                    |   `-- java
                    |       `-- App.java
                    `-- test
                        `-- java
                            `-- AppTest.java
```
Think of maven-archetype-archetype as the maven’s template to create templates.

The generated archetype-descriptor is old and needs to be changed to use new features. To that end, rename the file `archetype.xml` to `archetype-metadata.xml`

Over write the following content to the file:

```xml
<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
   name="simple-web"> <!-- name must be same as the project archetypeId -->
         <fileSets>
         <fileSet filtered="true" encoding="UTF-8">
             <directory>src/main/web</directory>
         </fileSet>
         <fileSet filtered="true" encoding="UTF-8">
             <directory>src/main/resources</directory>
         </fileSet>
         <fileSet filtered="true" encoding="UTF-8">
             <directory>src/test/resources</directory>
         </fileSet>
         <fileSet filtered="true" packaged="false">
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.java</include>
             </includes>
         </fileSet>
         <fileSet filtered="true">
             <directory>src/test/java</directory>
             <includes>
                 <include>**/*.java</include>
             </includes>
         </fileSet>
     </fileSets>
 </archetype-descriptor>
```

The above maven archetype descriptor file tells maven to copy (or create if not exists) any source files reside in web, `src/main/java` and corresponding test source directories. The archetype-resources directory is where all your template code should go. When you finally copy all your template files run the following command in maven to install your newly created artefact.

```console 
mvn install
```

You might want to continuously improve your template over time. Your artefact get installed multiple times during the process, so wrong versions of archetype may be installed. To counter that you might want to clean archetype cache by issuing following command:

```console 
mvn clean install -U
```

-U means force update of dependencies.

So whenever you want to create a new project using your archetype run following command:

```console
mvn archetype:generate -DarchetypeGroupId=com.my-template
 -DarchetypeArtifactId=template-name -DarchetypeVersion=1.0
 -DgroupId=proj.groupid -DartifactId=projectid
```

So there you have it! This single line command can load all your template source files and create whole directory structure of your application.