background
Generally, when creating a new project, you select maven or spring initializr to generate the project, and then go through a series of modifications, configurations and adding some styles of your own project.
As microservices are becoming more and more popular, a project needs to create many new microservice projects. Each time a new project is created, it needs to spend a lot of time to modify and configure, so as to achieve the consistency of all project structures and styles. If the colleagues of the new project are not very familiar with the project style, some project styles will be inconsistent.
maven provides a way to solve these problems, that is, to create a scaffold for your own project. Here is how to create and use a scaffold project.
Step 1: create a basic project
Create a project (scaffolding will be generated from this project, and all projects generated from this scaffolding are the format and style of this project)
For example:
-
groupId: com.yyds.oa
-
artifactId: oa-archetype
-
version: 0.0.1-SNAPSHOT
-
package: com.yyds.oa.archetype
be careful:
It is best to create a startup class with a name that can be used by all projects, such as StartApplication
The second step is to generate a prototype (scaffold) through the basic project
-
Add archetype. In the project root directory Properties, reference maven official documents , the following configurations are for reference only
# Specify which files or directories are not packaged into archetype excludePatterns=archetype.properties,*.iml,.idea/,.idea/libraries,logs/,build.sh # Setting variables (because. gitignore cannot be packaged directly into archetype and cannot be generated into new projects through archetype) gitignore=.gitignore # Customize archetype related configuration (if it is not set, the default will be used, that is, the current project configuration is basic + archetype) archetype.groupId=com.yyds.oa.framework archetype.artifactId=oa-framework-archetype # If no version is specified, the current project will be followed #archetype.version=0.0.1-SNAPSHOT
-
Copy project Gitignore file and rename it to__ gitignore__
__gitignore__
-
Check the configuration of the project. After confirmation, execute the command under the project root directory:
mvn archetype:create-from-project -Darchetype.properties=archetype.properties
-
After executing the script, target / generated sources / archetype will be generated in the project root directory (the output directory cannot be modified, refer to Maven archetype plugin source code)
-
Copy the archetype and rename it OA framework archetype
-
Open with development tools
The scaffold will be generated here
After the first two steps are completed, you can refer to: https://github.com/zhouxiaofu/create-maven-archetype/tree/main/oa-archetype
Step 3 variables and packaging
variable
The project configuration generated by the prototype will be consistent with the basic project, and some user-defined configurations are required, such as application The contents of YML are as follows
spring: application: name: oa-archetype server: port: 8080
The project generated through the prototype will be the same as this. You need to customize some configurations each time you generate, such as spring application. name,server.port and other configurations, or some configurations in the code
Variables can be used at this time. After modification:
spring: application: name: ${serverName} server: port: ${serverPort}
Open resources / meta-inf / Maven / archetype metadata XML, most of the configurations when generating the project are under this file, which can be modified as needed.
Since the serverPort variable is not a required variable when generating the project, we need to set it as required
Add under XML archetype descriptor - > requiredproperties
<requiredProperty key="serverPort"> <!-- You can also add defaultValue,It becomes non mandatory --> <!-- <defaultValue>8080</defaultValue>--> </requiredProperty> <requiredProperty key="serverName"> <!-- server name Default to artifactId --> <defaultValue>${artifactId}</defaultValue> </requiredProperty>
Note: for files that need to use variables, check whether the fileSet tag has filtered = "true", and if not, add it.
Before modification:
<fileSet encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> </includes> </fileSet>
After modification:
<fileSet encoding="UTF-8" filtered="true"> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> </includes> </fileSet>
At the same time, test / resources / projects / basic / archetype Properties add the default value of requiredProperty (used as a test during packaging, otherwise packaging will report an error)
serverPort=8080 serverName=basic
Refer to: https://github.com/zhouxiaofu/create-maven-archetype/tree/main/oa-framework-archetype
pack
After modification, package
-
There are maven private servers:
mvn deploy
-
No maven private server:
mvn install
The fourth step is to generate the project through the prototype
Execute in the directory where the project needs to be generated
mvn archetype:generate -DgroupId=com.yyds.oa -DartifactId=oa-user -Dversion=0.0.1-SNAPSHOT -Dpackage=com.yyds.oa.archetype -DserverPort=8081 -DarchetypeGroupId=com.yyds.oa.framework -DarchetypeArtifactId=oa-framework-archetype -DarchetypeVersion=0.0.1-SNAPSHOT
Parameter introduction:
- groupId, artifactId, version, package, serverPort project configuration to be generated
- archetypeGroupId, archetypeArtifactId, archetypeVersion specify the prototype of the build project
Secondary confirmation is required during execution. If you don't want secondary confirmation, you can set the parameter interactiveMode to false (interactiveMode is a required parameter and the default is true)
-DinteractiveMode=false
Interactive mode is recommended
archetype:generate official document
You can also configure maven archetype through an editor such as IDEA, and then generate a project
To view the latest documents: https://github.com/zhouxiaofu/create-maven-archetype