development setup

Introduction To Maven Archetype

Basics of Maven archetypes, creating maven archetype for an existing maven project and generating a maven project from custom archetype.

Maven archetypes are helpful to generate templates for Maven projects with the help of the maven command. Maven supports various pre-built archetype project templates that we can use to create simple java projects, web projects, etc.

We can also create our customized project templates with the help of the Maven archetypes. With this, we can set standardized project structures and best practices that are specific to an organization.

Table of Contents

Using the available archetype to generate maven project

There are multiple project templates available on the maven repository that we can use to create a maven project.

Executing the below command lists all available maven archetypes projects on the console.

mvn archetype:generate

We can also choose the archetype and other input parameters (group id, artifact id, etc.) to generate the maven project.

We can also choose the non-interactive mode by providing the exact archetype project type along with other required input parameters, as shown below.

mvn archetype:generate -DgroupId=com.asbnotebook -DartifactId=test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The above command generates a maven project with the structure defined in the maven-archetype-quickstart archetype.

We are also passing the GroupId and artifactId parameters.

The archetypeArtifactId parameter specifies the name of the archetype that we are using for the maven project generation.

The interactiveMode parameter with the value set to false disables the interactive mode and generates the project using the supplied input parameters.

The below image shows the project generated by the above command.

maven-archetype-quickstart-project-example

Custom maven archetypes

We can create custom archetype projects with predefined project structures. This is very useful in scenarios where we want to standardize a project structure across the organization.

We can use these archetypes to create projects with these defined templates that contain organization-level best practices.

Once created, we can publish the archetype to the organization’s Maven repository and utilize it across the organization.

Generating archetype project for existing maven project

Let’s create a custom archetype project from an existing maven project.

Navigate inside the project directory and run the below command to generate the files required for creating the archetype project.

mvn archetype:create-from-project

The generated source files are placed under the project’s /target/generated-sources/archetype directory.

We can copy the generated directories and files from the above folder and create a new maven archetype. We can create a new folder and copy the archetype files and folders inside the created folder.

Finally, we can run the command mvn install to register the archetype into our local repository.

Archetype project structure

The below image shows the typical folder structure of an archetype project.

Advertisements
maven archetype project.

Below is the pom.xml content of a simple archetype project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.asbnotebook</groupId>
  <artifactId>test-project-archetype</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>maven-archetype</packaging>

  <name>test-project-archetype</name>

  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.2.1</version>
      </extension>
    </extensions>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.2.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <url>http://maven.apache.org</url>
</project>

The build extension archetype-packaging provides the required maven life cycles to register the project as an archetype project.

The archetype-metadata.xml file contains the archetype resources. The generated maven project includes these resources. This file is located under the directory /src/main/resources/META_INF/maven/.

The below content shows an example configuration.

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd" name="test-project"
    xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.properties</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/test/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>  
  </fileSets>
</archetype-descriptor>

The project-specific resources are available under the /src/main/resources/archetype-resources/ directory of the archetype project.

We can also use the mentioned folder to place our packages, resources, etc.

Generating the project using the archetype

Once the archetype is added to the local repository, we can now use the archetype to generate new projects.

Let’s create a new project by running the below maven command.

mvn archetype:generate -DarchetypeCatalog=local

The command is executed in interactive mode. We can then choose the available archetypes by selecting the archetype from the listed archetypes.

The archetypeCatalog option with the value local instructs the maven to use only archetypes available in the local maven repository.

The below image shows the console output of the creation of the maven archetype.

maven archetype creation

Finally, the below image shows the generated maven project structure with the default directories and files.

custom maven archetype project template

We can also observe the project structure with the predefined resources and java files.

Finally, we can generate a maven project with non-interactive mode.

The below maven command shows how we can use the interactiveMode property with value false to disable interactive mode.

mvn archetype:generate -DgroupId=com.asb -DartifactId=my-template-project -DarchetypeArtifactId=test-project-archetype -DarchetypeVersion=0.0.1 -DarchetypeCatalog=local -DinteractiveMode=false

Conclusion

In this article, we learned what are maven archetype projects and also how to use them to create projects by leveraging the available archetypes.

We also learned how to generate archetype resources for a project using maven command.

We then created a custom maven archetype project using the generated archetype resources.

Finally, we also learned how to generate custom projects with the help of our custom archetype project.

Leave a Reply