Basically every company project has a set of templates, just start a new project directly copy a change package name and some configuration to initialize a new project. This approach is a bit cumbersome, there is no command or even visual way to initialize the project from a template project. Maven actually provides this capability, which is based on prototypes (Archetype).

Integration project template plug-in

The first step is to integrate the Maven prototype plug-in into your template project:

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

Generate templates from your project

It only takes three steps to generate a template from your project.

Generate template prototype files

The Maven plugin allows us to generate a template project from an existing project. Run the MVN archetype:create-from-project command in the project root directory to generate a project template. The generated project prototype is saved in target/generated-sources/archetype.

However, there is a problem with this. Some files that have nothing to do with the source code of the project template will also be added to the template, such as. Iml files in IDEA and. IDEA files. These “junk” files need to be ignored during template generation.

Maven-archetype-plugin provides an attribute configuration to help us achieve this capability. Write a.properties file in the root directory of your original project (or whatever you prefer) :

The coordinates of the prototype must be declared
archetype.groupId=cn.felord
Archetype is the best way to end this archetype
archetype.artifactId=template-archetype
archetype.version=1.0
The folder and file types to be ignored are separated by commas
excludePatterns=**/.idea/**,**/*.iml
Copy the code

This configuration file needs to be specified to execute the generated command:

mvn archetype:create-from-project -Darchetype.properties=./archetype.properties
Copy the code

Note: The path is the relative path between the configuration file and the root directory.

The resulting prototype ignores useless files. Careful students will notice that the pom. XML coordinates in the generated prototype become:

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

<name>template-archetype</name>
Copy the code

Install the generated prototype

We need to jump to target/generated-sources/archetype and run MVN install

In our local Maven repository directory (usually ~/.m2), the current archetype is registered in archetype-catalog.xml:


      
<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>
Copy the code

Maven will retrieve our local templates from here for subsequent use.

Build projects using prototypes

Next to verify the effect, generating a project from a local prototype requires:

mvn archetype:generate -DarchetypeCatalog=local
Copy the code

Then a new project was born.

Share the template

Template sharing is based on Maven remote repository. First you need to configure a remote repository in your setting.xml and have the deploy permission. Then execute MVN deploy under target/generated-sources/archetype. There are two ways to use remote prototype templates.

IDEA adds Maven prototype templates

Click New Project and select Maven, check Create from Archetype and click Add Archetype to pop up the dialog box:

The template is then added to the template list. Select the template and click Next to use it.

The command line

Just like creating a project with a native prototype, here’s a set of commands to execute:

DartifactId=demo-project \ -dversion =1.0.0 \ 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 = falseCopy the code

If you want interactive command generation, try removing the first three options and the last DinteractiveMode.

Don’t forget to configure the following remote repository to setting.xml;

  <repository>
    <id>archetype</id>
     <! -- Warehouse address -->  
    <url>https://repository.domain.com/path/to/repo/</url>
  </repository>
  
  <! -- Warehouse authentication information -->
  <server>
    <id>archetype</id>
    <username>repousername</username>
    <password>xxx.felord.cn</password>
  </server>
Copy the code

Project template generation is very important in project research and development, so that our project can maintain consistency, and it is also convenient for subsequent micro services or maintenance and upgrading.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn