DEV Community

Thilanka
Thilanka

Posted on

2

Creating custom archetypes in Maven

Image descriptionAs 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:

mvn archetype:generate \
-DgroupId=[your project's group id] \
-DartifactId=[your project's artifact id] \
-DarchetypeArtifactId=maven-archetype-archetype
Enter fullscreen mode Exit fullscreen mode

This will create following default directory structure for creating archetypes.

archetype
|-- pom.xml
`-- src
    `-- main
        `-- resources
            |-- META-INF
            |   `-- maven
            |       `--archetype.xml
            `-- archetype-resources
                |-- pom.xml
                `-- src
                    |-- main
                    |   `-- java
                    |       `-- App.java
                    `-- test
                        `-- java
                            `-- AppTest.java
Enter fullscreen mode Exit fullscreen mode

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:

<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>
Enter fullscreen mode Exit fullscreen mode

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.

mvn install
Enter fullscreen mode Exit fullscreen mode

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:

mvn clean install -U
Enter fullscreen mode Exit fullscreen mode

-U means force update of dependencies.

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

mvn archetype:generate -DarchetypeGroupId=com.my-template
 -DarchetypeArtifactId=template-name -DarchetypeVersion=1.0
 -DgroupId=proj.groupid -DartifactId=projectid
Enter fullscreen mode Exit fullscreen mode

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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more