Maven Archetype use - custom project template

Maven Archetype can be understood as the template of Maven project. I believe you have used it more or less. When we first started to learn how to create a project with maven, we often used regular Maven project templates, such as Maven Archetype QuickStart and Maven Archetype webapp. You only need to provide basic elements (such as groupId,artifactId and version), and the project template can generate the basic structure and POM files of the project.

In the development of daily work, it is very common to build a project by hand. Every time we start a new project, we need to configure various projects. In fact, the infrastructure of the project is basically the same, so in general, companies will customize their own project templates for development. With the customized project templates, we can quickly build our projects.

1, Understanding and use of Maven archetype plugin

Note: the following is based on Maven 3

1. Some steps of using archetype

Maven uses the archetype command mvn archetype:generate

Enter this command in the console and you can see that the Archetype plug-in will output an Archetype list for users to choose from. The default is the Maven Archetype QuickStart template. Press enter with Maven Archetype QuickStart. The next step is to provide some basic parameters, mainly including:

  • groupId: groupId of the project you want to create
  • Artifact ID: the artifact ID of the project you want to create
  • Version: the version of the project you want to create
  • Package: the default package name of the project you want to create

The above parameters are the most common. When we customize the Archetype, we can also configure additional configuration parameters.

After you fill in the configuration as prompted, the Archetype plug-in generates the skeleton of the project.

2. Use archetype for batch processing

Sometimes users find it troublesome to use Archetype interactively. They want to input instructions at one time to generate project architecture without being destroyed interactively. You can use the - B option of the mvn command to require Maven Archetype plugin to run in batch mode. However, in this way, the user will display the Archetype coordinate information to be used in the declaration, as well as the groupId, artifactId, version, package and other information of the project to be created. If the Archetype is customized, the user will also enter his own additional configuration parameters. For example:

mvn archetype:generate -B \
-DgroupId=com.yibaiwu.demo \
-DartifactId=demo \ 
-Dversion=1.0.0-SNAPSHOT \
-Dpackage=com.yibaiwu.demo \ 
-DarchetypeArtifactId=yibaiwu-archetype-plugin \
-DarchetypeGroupId=com.alias \
-DarchetypeVersion=1.0-SNAPSHOT \
  • archetypeArtifactId: groupId of Archetype
  • archetypeGroupId: the artificid of Archetype
  • archetypeVersion: version of Archetype
3. Write Archetype

Here is an example of a custom Archetype.

As shown in the figure above, a typical Archetype Maven project mainly includes these parts:

  • pom.xm: POM of Archetype itself
  • src/main/resources/achetype-resource/pom.xml: POM prototype of the project generated based on the Archetype
  • src/main/resources/META-INF/maven/achetype-metadata.xml: description file for Archetype.
  • Src / main / resources / Archetype resources / * *: other content that needs to be included in Archetype. There are some sample codes and configuration files that we write by ourselves.

Like other Maven projects, the province of Archetype project also needs pom.xml file, which contains coordinate information of Archetype, so that Maven can locate and use it.

<?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.alias</groupId>
    <artifactId>yibaiwu-archetype-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

Next, configure the project skeleton information contained in Archetype. Generally, when writing Archetype, predefine the directory structure and files to be included, and use configurable attribute declaration instead of hard coding where necessary. For example, our project coordinate information is generally configurable. Here is a simple POM prototype:

<?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>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.0.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Next is the core part of Archetype, archetype-metadata.xml descriptor file, which is located in META-INF/maven / subdirectory of Archetype project resource directory. Its main functions are: first, declare which directories and files should be included in Archetype; second, which attribute parameters this Archetype uses. Here is an Archetype descriptor file.

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="sample">
    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="false">
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
    <requiredProperties>
        <requiredProperty key="groupId">
            <defaultValue>com.yibaiwu</defaultValue>
        </requiredProperty>
    </requiredProperties>
</archetype-descriptor>

If the value of the package attribute of the fileSet is true and the value of directory is x, then the X directory under Archetype resources will be created in the generated project, and a package directory (and the package we entered) will be generated under the generated x directory. If the value of the packaged attribute of the fileSet is false, the contents of the X directory in the Archetype will be copied directly to the X directory of the project. In general, Java code needs to be placed in the package path, while project resource files do not. Therefore, the packaged property of the fileSet of the corresponding project resource file is false.

The filtered property ID of the fileSet replaces the property declaration with the parameter value, as shown in the following file.

package ${package};

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication(scanBasePackages = {"${package}"})
@MapperScan("${package}.repository")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

After the Archetype is written, use maven install to install it to the local warehouse, or package and upload it to the private server. Then the user can use the Archetype to generate a project by formulating the coordinates of the Archetype:

mvn archetype:generate \
-DgroupId=com.yibaiwu.demo -DartifactId=demo \ 
-Dversion=1.0.0-SNAPSHOT \
-Dpackage=com.yibaiwu.demo \ 
-DarchetypeArtifactId=yibaiwu-archetype-plugin \
-DarchetypeGroupId=com.alias \
-DarchetypeVersion=1.0-SNAPSHOT \

4. Configure archetype-metadata.xml to generate multi project module code

Sometimes, when building a project, we divide the project into different modules. Here, we modify the archetype-metadata.xml file to support the generation of multiple submodules. Reference article: Multi-Module Maven Archetypes

The trick to generating multiple submodules is to use the rootartifactid. When archetype generates an application, it will use the artifactId to replace it.

The example structure of multi module archetype is as follows:
example-archetype
├── pom.xml
└── src
    ├── main
       └── resources
           ├── META-INF
           │   └── maven
           │       └── archetype-metadata.xml
           └── archetype-resources
               ├── __rootArtifactId__-module1
               │   ├── pom.xml
               │   └── src
               │       ├── main
               │       │   ├── java
               │       │   └── resources
               │       └── test
               │           └── java
               │           └── resources
               ├── __rootArtifactId__-module2
               │   ├── pom.xml
               │   └── src
               │       ├── main
               │       │   ├── java
               │       │   └── resources
               │       └── test
               │           └── java
               │           └── resources
               └── pom.xml
Under the archetype resources directory, the parent pom file of the created project:
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>pom</packaging>

<modules>
    <module>${rootArtifactId}-module1</module>
    <module>${rootArtifactId}-module2</module>
</modules>
POM file for submodule 1:
<parent>
    <groupId>${groupId}</groupId>
    <artifactId>${rootArtifactId}</artifactId>
    <version>${version}</version>
</parent>

<artifactId>${artifactId}</artifactId>
<packaging>jar</packaging>
POM file for submodule 2
<parent>
    <groupId>${groupId}</groupId>
    <artifactId>${rootArtifactId}</artifactId>
    <version>${version}</version>
</parent>

<artifactId>${artifactId}</artifactId>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>${groupId}</groupId>
        <artifactId>${rootArtifactId}-module1</artifactId>
        <version>${version}</version>
    </dependency>
</dependencies>
archetype-metadata.xml file:
<archetype-descriptor name="${artifactId}">
  <modules>
    <module id="${rootArtifactId}-module1" dir="__rootArtifactId__-module1" name="${rootArtifactId}-module1">
        <fileSets>
            <fileSet filtered="true" packaged="true" encoding="UTF-8">
                <directory>src/main/java</directory>
            </fileSet>
            <fileSet encoding="UTF-8">
                <directory>src/main/resources</directory>
            </fileSet>
            <fileSet filtered="true" packaged="true" encoding="UTF-8">
                <directory>src/test/java</directory>
            </fileSet>
             <fileSet encoding="UTF-8">
                <directory>src/test/resources</directory>
             </fileSet>
        </fileSets>
    </module>
    <module id="${rootArtifactId}-module2" dir="__rootArtifactId__-module2" name="${rootArtifactId}-module2">
        <fileSets>
            <fileSet filtered="true" packaged="true" encoding="UTF-8">
                <directory>src/main/java</directory>
            </fileSet>
            <fileSet encoding="UTF-8">
                <directory>src/main/resources</directory>
            </fileSet>
            <fileSet filtered="true" packaged="true" encoding="UTF-8">
                <directory>src/test/java</directory>
            </fileSet>
             <fileSet encoding="UTF-8">
                <directory>src/test/resources</directory>
             </fileSet>
        </fileSets>
    </module>
  </modules>
</archetype-descriptor>
  • id: the artifact id of the project
  • dir: equivalent to the corresponding directory of the project source file in archetype resources
  • Name: the name of the module

After the modification, use the same method as above to install the project locally, and then execute the mvn archetype: generate command:

mvn archetype:generate \
-DgroupId=com.yibaiwu.demo \
-DartifactId=demo \ 
-Dversion=1.0.0-SNAPSHOT \
-Dpackage=com.yibaiwu.demo \ 
-DarchetypeArtifactId=yibaiwu-archetype-plugin \
-DarchetypeGroupId=com.alias \
-DarchetypeVersion=1.0-SNAPSHOT \
Published 23 original articles, won praise 5, visited 10000+
Private letter follow

Tags: Maven xml Java encoding

Posted on Wed, 15 Jan 2020 22:27:19 -0800 by shortysbest