PrefaceIt's time to develop a new project , Or create a new project , What do I do ? The boss said according to xxx Project structure create a new project .
official account :liuzhihangs, Record the skills in work study 、 Development and source notes ; From time to time to share some of the life experience . You are welcome to guide !
There are often new projects to be created at work , There are three common ways
CC Dafa New projects , Then find the previous various tool classes , Copy and paste in , You may not be able to run at this time , And then do all kinds of debugging .
CD Dafa Copy the old project , Then change module name , Rely on names , Delete old code , Of course, it doesn't have to be able to run , At this point, all kinds of debugging will be carried out .
Of course , It's certainly not the use of these two methods , Let's introduce a more concise way , Use maven archetype Generate project template , One click project creation .
Action!!!
What is? Archetype ?
In short ,Archetype yes Maven Project template toolbox .
An archetype is defined as an original pattern or model from which all other things of the same kind are made.
Prototypes are defined as original styles or models , You can make all the other projects of the same kind .
Official explanation , Simple and clear , It's using existing projects , Generate a template . In the future, you can use this template to quickly generate projects with the same structure . Useful in team development .
It's the official explanation , Put up the address :http://maven.apache.org/arche...
Use IDEA As a demonstration tool , Step by step introduction .
Prepare template project
Here is still a template project , Such as this :
This is a lot of module project , A simple demo
- Used nacos As a registry ;
- fegin As a calling tool ;
- There is a universal check token Tool class ;
- Let's assume that there are also some public configurations of companies (MQ, Link monitoring , Unified log, etc ).
Of course, this project can run . The newly created project is also the template .
Get into the subject
Use command
archetype:generate
This is based on the current project , You can also use the official website to specify parameters separately
- mvn archetype:generate Then, step by step, follow the prompts and enter ;
- One time input , among
DarchetypeGroupId
、DarchetypeArtifactId
、DarchetypeVersion
For the generated Archetype Organizational version of the project .
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
Advanced usage mvn clean archetype:create-from-project
The execution diagram is as follows :
Go back to IDEA Check out the project , stay target/generated-sources
In the catalog archetype
The generated project template .
The structure is as shown in the figure :
main/resources/archetype-resources: Project template , Generate a new project , It is based on this code to generate .
- .idea It's useless , Delete the .
- __rootArtifactId__xxx Each part of the project module
- main/resources/META-INF/maven/archetype-metadata.xml: Template project metadata configuration .
You can put archetype Torture out , This is a separate engineering template , After the torture , Use IDEA open .
So let's start with archetype What's in it .
archetype Template project introduction
Use IDEA Open and find , Or a Maven project .
archetype-resources
open pom file , You can see inside ${groupId}
、 ${artifactId}
、${version}
Use the placeholder to specify the organization version , These are what you specify when you create a new project .
archetype-metadata.xml
archetype-metadata.xml It is configured for metadata .
- fileSet: Used to generate files in some projects . If the file or directory name contains
__property__
Pattern , Replace it with the corresponding attribute value .
attribute | type | describe |
---|---|---|
filtered | boolean | Filter the corpus , Copy the specified file directly without modification . The default value is :false. |
packaged | boolean | Package files , The specified file will be in package Attribute is generated in the directory structure before the / Copy . They can be non packaged , This means that the selected file will be generated without the prefix / Copy . The default value is :false. |
encoding | String | The encoding used when filtering content . |
fileSet Contains the following elements :
Elements | type | describe |
---|---|---|
directory | String | The directory where the project files are generated |
includes/include* | List<String> | Include files |
excludes/exclude* | List<String> | Exclude files |
Because you don't need to build a project .idea *.iml file , So delete :
- module How many projects are to be generated module
attribute | type | describe |
---|---|---|
id | String | The module's artifactId. |
dir | String | The module's directory. |
name | String | The module's name. |
Elements | type | describe |
---|---|---|
fileSets/fileSet* | List<FileSet> | file |
modules/module* | List<ModuleDescriptor> | modular |
You can see that it is your own project template .
__rootArtifactId__-controller
At the time of generation , According to the incoming artifactId Generate the specified module name .
Use
- clean install
- IDEA Add Archetype
- Choose to use Archetype Generate a new project
- Fill in the name of the newly generated project, etc
- Generate a new project
Expand
Q: How to customize the package path ?
A: have access to requiredProperties
Custom parameters . By passing in custom parameters , To generate custom package paths .
For example, it is found that the package paths of newly generated projects are com.liuzhihang.archetype
, That's not gonna work , Each project has its own package path . Just make the following changes :
- take
requiredProperties
Add to the project , Then add a new variablemiddlePackage
.
<requiredProperties>
<!-- Use archetype The input parameters must be required when -->
<requiredProperty key="groupId">
<!-- Default values can be set , Use archetype Default values will be used -->
<defaultValue>com.liuzhihang</defaultValue>
</requiredProperty>
<requiredProperty key="package">
<defaultValue>com.liuzhihang</defaultValue>
</requiredProperty>
<requiredProperty key="middlePackage">
<defaultValue>${rootArtifactId}</defaultValue>
</requiredProperty>
</requiredProperties>
- Modify the file name of the template
If the file or directory name contains __property__
Pattern , Replace it with the corresponding attribute value . Not yet here , Because the generated package name has not been changed .
- Modify the package path of internal files . Include .java 、 .xml 、 .properties etc. .
- again clean install
notes : You may report an error at this time , Need to be in src/test/resources/projects/basic/archetype.properties
Add below middlePackage=basic
Try again .
Pay attention to the specification of middlePackage
attribute .
Q: I want to customize Application How to get your name ?
A: Also use requiredProperties
Custom parameters .
<requiredProperty key="appName">
</requiredProperty>
Of course, you can have a generic name .
Q: How to use the other partners ?
A: Certainly deploy It's private service , stay pom Add the following configuration , Specify your own company's private service .deploy , In this way, you can use it happily with your partner .
<!-- Remote warehouse -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://liuzhihang.com:xxxx/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://liuzhihang.com:xxxx/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Q: How can I get from IDEA Delete Archetype ?
A: So easy to use, how willing to delete Well ? Just find the following path
liuzhihang % > pwd
/Users/liuzhihang/Library/Caches/JetBrains/IntelliJIdea2020.1/Maven/Indices
There's a UserArchetypes.xml
, open , Delete the inside archetype Just go .