Quickly create project templates based on existing projects
The magical effect of mvn Archetype
background
In the development process, especially when using Maven as a package management tool, there is always a headache when creating new modules.
Common scenarios
Open source projects downloaded from the Internet (or large-scale projects of the company) need to build a new module or system, and this open source project may have many levels. At this time, manual creation is likely to produce errors. And Maven依赖等
error, sometimes very subtle, not easy to find.
1. Selection原始模型
Here is a demo as an example. This demo is nested layer by layer (Ali Cola4.0's demo). If you create a new one 领域模块
, you have to create 6 modules, which is very dangerous and very boring.
2. Build原始模型
1. Select the template you need to create
For example, in the demo, we will directly 原始模型
create the entire demo , so that in the future, we can also directly use this architecture structure to create a project.
2. Generate原始模型
Enter the pom.xml
directory of the demo parent project (demo-web-parent) and execute (no need to modify)
mvn archetype:create-from-project
3. Send 原始模型
to local warehouse
The ones just generated above 原始模型
need to be sent to the local Maven warehouse before they can be used.
Enter the generated target\generated-sources\archetype
directory and execute
mvn clean install
4. Check whether the push is successful
Open the Maven dependency directory ~\.m2\repository
, you can see the file and archetype-catalog.xml
see the following similar record, indicating that it has been created.
<?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>com.alibaba.cola.demo.web</groupId>
<artifactId>demo-web-parent-archetype</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description>demo-web-parent-archetype</description>
</archetype>
</archetypes>
</archetype-catalog>
3. Use to 原始模型
create a new project\module
1. Created with IDEA
- File->New->Project/Module, see the following interface, enter
archetype-catalog.xml
what you saw in the file just now三维坐标
. The fourth item is optional.
- Just fill in according to the actual situation.
- The third step, after all, is more important, there are many options, of course, you can leave it blank.
key | Instance | Description |
---|---|---|
groupId | com.tianjingle.wang | Project name of the new project |
artifactId | demotianjingle | The name of the project to be created |
version | 1.0.0-SNAPSHOT | The version number of the project to be created |
package | com.tianjingle.wang | The base package name of the project to be created |
archetypeGroupId | com.example | ./m2 in the xml |
archetypeArtifactId | demo-archetype | ./m2 in the xml |
archetypeVersion | 0.0.1-SNAPSHOT | ./m2 in the xml |
archetypeCatalog | local | Pick a template from a local warehouse |
interactiveMode | false |
2. Use mvn command to create
Field description, as in the table above
mvn archetype:generate -DgroupId=ltzDemo -DartifactId=com.ltz -Dversion=1.0.0-SNAPSHOT -Dpackage=com.ltz.demo -DarchetypeGroupId=com.alibaba.cola.demo.web -DarchetypeArtifactId=demo-web-parent-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -B -DarchetypeCatalog=local -DinteractiveMode=false