![]()
Basically, each company’s project has a set of templates. Just open a new project and just copy a copy, change the package name and some configurations to initialize a new project. This method is a bit cumbersome, is there any way to initialize the project from a template project through a command or even a visual way. Maven actually provides this ability, which is the generation project Archetype

Integrated project template plugin

First, you need to integrate the Maven archetype plugin in the template project:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-archetype-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>

Generate template from project

Only three steps are required to generate a template from a project.

Generate template prototype file

The Maven plugin allows us to generate a template project from an existing project. Execute the mvn archetype:create-from-project project root directory to generate a project template for the project, and the generated project prototype is saved under the path target/generated-sources/archetype .

But there is a problem. Some files that are not related to the project template source code will also be added to the template, such as the .iml file in IDEA and the files in the .idea directory. These "junk" files need to be ignored when generating the template.

maven-archetype-plugin provides an attribute configuration to help us realize this ability. .properties file in the root directory of the original project (or another directory you like):

# 原型的坐标 必须声明
archetype.groupId=cn.felord
# 最好按照约定以-archetype结尾
archetype.artifactId=template-archetype
archetype.version=1.0
# 需要忽略的文件夹和文件类型 英文逗号隔开
excludePatterns=**/.idea/**,**/*.iml

At this time, you need to specify the configuration file to execute the generated command:

mvn archetype:create-from-project -Darchetype.properties=./archetype.properties
Note: The path is the relative path between the configuration file and the root directory.

The generated prototype ignores those useless files. Attentive students will find that the coordinates pom.xml

<groupId>cn.felord</groupId>
<artifactId>template-archetype</artifactId>
<version>1.0</version>
<packaging>maven-archetype</packaging>

<name>template-archetype</name>

Install the generated prototype

We need to jump to target/generated-sources/archetype to execute mvn install

安装原型库到本地

In our local maven repository directory (usually ~/.m2 ), the current prototype will be registered in archetype-catalog.xml :

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>cn.felord</groupId>
      <artifactId>template-archetype</artifactId>
      <version>1.0</version>
      <description>spring security learning demo</description>
    </archetype>
  </archetypes>
</archetype-catalog>

In subsequent use, maven will retrieve our local template from here.

Use prototypes to generate projects

Next, to verify the effect, the project needs to be generated from the local prototype:

mvn archetype:generate -DarchetypeCatalog=local

创建项目步骤都在这里

Then a new project was born.

Share template

The template sharing is based on the Maven remote warehouse. First, you need to configure a remote warehouse setting.xml deploy permissions. Then execute target/generated-sources/archetype mvn deploy . There are two ways to use remote prototype templates.

IDEA adds Maven archetype template

Click New Project and then select Maven , check Create from archetype click Add Archetype pop up a dialog box:

IDEA添加原型模板

Then the template is added to the template list, select the added template and click to use it.

Command Line

Just like using a local prototype to create a project, here is a set of commands to execute:

mvn archetype:generate \
 -DgroupId=cn.felord.demo    \
 -DartifactId=demo-project   \
 -Dversion=1.0.0       \
 -Dpackage=cn.felord.demo   \
 -DarchetypeGroupId=cn.felord  \
 -DarchetypeArtifactId=template-archetype  \
 -DarchetypeVersion=1.0  \
 -DinteractiveMode=false    
If you want to generate through interactive commands, try removing the DinteractiveMode

Don't forget to configure the following remote warehouse to setting.xml ;

  <repository>
    <id>archetype</id>
     <!-- 仓库地址 -->  
    <url>https://repository.domain.com/path/to/repo/</url>
  </repository>
  
  <!-- 仓库的认证信息 -->
  <server>
    <id>archetype</id>
    <username>repousername</username>
    <password>xxx.felord.cn</password>
  </server>

Project template generation is very important in project research and development. It can keep our projects consistent and facilitate subsequent transfers to microservices or maintenance and upgrades.

Follow the public account: Felordcn for more information

personal blog: https://felord.cn


码农小胖哥
3.8k 声望8k 粉丝