Package the project into a Maven Archetype (single-module project scaffolding)

official tutorial

1 Create a project, single module, the project structure is as follows

insert image description here

工程规范: The groupId of the project must be consistent with the top-level package name.

Otherwise, the created template needs to be manually modified in many places, and if it is consistent, it can be replaced directly ${groupId}.
insert image description here

2 pom.xml add maven-archetype-plugin plug-in

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-archetype-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
            	<propertyFile>archetype.properties</propertyFile>
            </configuration>
        </plugin>
    </plugins>
</build>

3 Add Maven environment variable MAVEN_OPTS

-Xms256m -Xmx512m -Dfile.encoding=UTF-8

4 Added archetype.properties

When we use the maven-archetype-plugin plugin to generate an archetype template project based on the current project, the plugin will package all the files under the current project into the jar package by default.

In fact, we hope to filter some non-essential file directories during build, such as .idea directory, README.md, etc. At this time, we only need to configure a file parameter during build, and define attributes in it to filter archetype.propertiesfiles excludePatternor Table of contents.

excludePatternsParameters can specify multiple filter conditions, separated by commas.

However, it should be noted that if you want to filter the entire directory, such as .ideathe directory, .idea/*you need to configure it as: instead of just filling in idea. This .gitignoreconfiguration is still different.

# 打包过滤文件
excludePatterns=.idea/*,.git,*.iml,.idea/

5 Add .gitignore

Rename .gitignoreit to __gitignore__, and archetype.propertiesadd the following to

gitignore=.gitignore

6 Build archetypes

Enter the project directory and run the following command

mvn archetype:create-from-project --settings D:\DevelopTools\apache-maven-3.6.3\conf\settings.xml

After the build is complete, you will see this archetype in the target

insert image description here

7 Install archetype to local warehouse

Enter the directory where the created Archetype is located:target\generated-sources\archetype

run the following command

mvn install

8 Using a custom archetype

Enter a new directory and execute the following command

mvn archetype:generate -DarchetypeCatalog=local

Choose 3 here, which is the archetype just generated

insert image description here

Fill in one by one groupId ,artifactId,version,package, then press Enter and enter Y to create a new project

insert image description here

Created

insert image description here

9 Open the project and verify

The new project is the same as the original project

insert image description here

Now start and verify

insert image description here

Enter http://localhost:8080/hello, you can see success

insert image description here

10 File property replacement

Find an archetype file in the target\generated-sources directory of the project, which is the skeleton file. There is one in it archetype-metadata.xml, this file is the basic configuration.
insert image description here
If Archetype variables are used in configuration files or elsewhere, such as in application.ymlspring.application.name

spring:
  application:
    name: ${
    
    artifactId}

In this way, it needs to be added in the place where archetype-metadata.xml configures yml filtered="true". If this attribute is added, the attribute variable will be replaced. as follows

<fileSet encoding="UTF-8" filtered="true">
  <directory>src/main/resources</directory>
  <includes>
    <include>**/*.yml</include>
  </includes>
</fileSet>

After adding, the generated new project will be replaced with the corresponding artifactId

spring:
  application:
    name: user-center

Guess you like

Origin blog.csdn.net/q283614346/article/details/126692547