一卓的博客

怕什么真理无穷,
进一寸有一寸的欢喜。

0%

maven 订制自己的项目模板

Maven 可以根据已有的项目模板来快速构建项目,避免重复的搭建相同的项目开发环境。

首先确保本地正确安装了 JDKMaven

自定义 archetype

1、首先需要创建一个 Maven 项目,或者使用现有 Maven 项目,在项目根目录下执行以下命令

1
mvn archetype:create-from-project

2、执行成功后,可以看到在项目多了一个 target 文件夹,目录如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
target
|-- generated-sources
|-- archetype
|-- src
|-- main
|-- resources
|-- archetype-resources
|-- ....
|-- META-INF
|-- maven
|-- archetype-metadata.xml
|-- target
...
|-- pom.xml

如果想生成干净的项目骨架,可进入 src 目录下删除不需要的文件及文件夹,并打开 target\generated-sources\archetype\src\main\resources\META-INF\maven 目录下的 archetype-metadata.xml 文件,将不需要的 fileSet 标签删除,文件内容大概长这个样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd" name="archetype-demo"
xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>

3、进入 archetype 目录下,执行以下命令

1
mvn clean install

项目将会被安装到你所使用的 mavensettings.xml 中所指定的仓库下,自定义的 archetype 生成成功

使用自定义 archetype 生成项目:

1、进入想将项目生成的目录位置, 打开 cmd 窗口,输入以下命令

1
mvn archetype:generate -DarchetypeCatalog=local

2、根据提示输入 groupIdartifactIdversionpackage , 回车,会再次提示确认,输入 Y 回车

大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Choose archetype:
1: local -> com.github.archetype:archetype-demo-archetype (archetype-d
emo-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): : 1
Define value for property 'groupId': com.baidu.io
Define value for property 'artifactId': test-archetype
Define value for property 'version' 1.0-SNAPSHOT: : 0.0.1-SNAPSHOT
Define value for property 'package' com.baidu.io: :
Confirm properties configuration:
groupId: com.baidu.io
artifactId: test-archetype
version: 0.0.1-SNAPSHOT
package: com.baidu.io
Y: : Y

3、成功生成项目。

请作者喝杯咖啡吧