Skip to content

Commit 11dfe08

Browse files
committed
添加项目模板
0 parents  commit 11dfe08

File tree

11 files changed

+671
-0
lines changed

11 files changed

+671
-0
lines changed

.gitignore

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Eclipse project files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# IntelliJ IDEA project files and directories
7+
*.iml
8+
*.ipr
9+
*.iws
10+
.idea/
11+
12+
# Geany project file
13+
.geany
14+
15+
# KDevelop project file and directory
16+
.kdev4/
17+
*.kdev4
18+
19+
# Build targets
20+
/target
21+
*/target
22+
23+
# Report directories
24+
/reports
25+
*/reports
26+
27+
# Mac-specific directory that no other operating system needs.
28+
.DS_Store
29+
30+
# JVM crash logs
31+
hs_err_pid*.log
32+
33+
dependency-reduced-pom.xml
34+
35+
*/.unison.*
36+
37+
# exclude mainframer files
38+
mainframer
39+
.mainframer
40+
41+
# exclude docker-sync stuff
42+
.docker-sync
43+
*/.docker-sync

README.md

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Maven通用工程模板方案
2+
3+
> 本文旨在介绍使用`maven archetype`进行项目模板管理的方案,同时会介绍一些常用于质量检测的`maven`插件。
4+
5+
工作中常常会遇到这么一种情况:一个产品线或工作组内,会创建数十个甚至上百个结构高度相似,各自承担不同业务的应用工程项目。
6+
7+
比如一个常见的基于`spring-boot`的应用工程模块结构可能如下
8+
9+
```
10+
-root
11+
|- doc 项目文档与阅读资料
12+
|- facade 接口与模型发布包
13+
|- app 实际服务进程代码
14+
|- checkstyle 项目风格检测工具
15+
|- pom.xml 项目依赖配置与版本管理配置
16+
```
17+
18+
以如上工程结构为例,`pom.xml``checkstyle`通常是高度一致的;
19+
20+
有时我们需要在研发团队要统一一个相对标准的项目构建结构(以及流水线方案), 比起让各个项目负责人在项目伊始复制粘贴的方案,
21+
使用`archetype jar`进行模板项目的管理和创建则更为友好。
22+
23+
![workflow](doc/archetype-workflow.png)
24+
25+
## 1. `archetype`概述
26+
27+
简单地说,`archetype`是一个maven的模板工具,`archetype`定义了一组统配模型,用于构建大量同质的项目结构。
28+
`Archetype`可以帮助我们去创建一个供其他研发人员使用的`maven`项目模板,甚至允许我们和依赖包一样,在仓库中对模板进行版本管理和发布。
29+
30+
使用`maven-archetype-plugin`,我们能够将一个已有的项目转化为`archetype project`
31+
32+
![archetype](doc/archetype-overview.png)
33+
34+
35+
通过生成的`archetype project`进行构建,就能生成`archetype jar`制品;
36+
37+
由于`archetype project`也是一个`maven`项目,因此和其他的`dependency jar`制品一样,
38+
我们也可以通过`install``deploy`等指令在本地安装或者发布至远端。
39+
40+
## 2. 转化模板工程
41+
42+
我们以一个简单的`springboot`模板项目为例, 可以在github上找到该[项目地址](https://github.com/my-helloworld/template-springboot)
43+
44+
该模板结构如下
45+
46+
```
47+
-root
48+
|-app SpringBoot应用
49+
|-checkstyle 项目风格检测工具
50+
|-doc 项目文档与阅读资料
51+
|-pom.xml 项目依赖配置与版本管理配置
52+
```
53+
54+
项目结构本身很简单,唯一定制的部分是构建过程要求做一些常见的静态检测,如`checkstyle``findbugs`
55+
56+
接下来我们需要将这个项目转化为一个`archetype project`;
57+
58+
> 指定`archetype`构建配置
59+
60+
在项目的根目录下`pom.xml`声明配置:
61+
62+
```xml
63+
<build>
64+
<extensions>
65+
<extension>
66+
<groupId>org.apache.maven.archetype</groupId>
67+
<artifactId>archetype-packaging</artifactId>
68+
<version>${ext.archetype.packaging.version}</version>
69+
</extension>
70+
</extensions>
71+
</build>
72+
```
73+
74+
> 构建`archetype project`
75+
76+
在项目根目录下执行生成archetype
77+
78+
```
79+
mvn archetype:create-from-project
80+
```
81+
82+
该指令会在`${project.build.directory}/generated-sources/archetype`下生成模板工程`archetype project`
83+
84+
```
85+
target/generated-sources/archetype
86+
|-- pom.xml
87+
`-- src
88+
|-- main
89+
| `-- resources
90+
| |-- archetype-resources
91+
| | |-- pom.xml
92+
| | `-- src
93+
| | |-- main
94+
| | | `-- java
95+
| | | `-- App.java
96+
| | `-- test
97+
| | `-- java
98+
| | `-- AppTest.java
99+
| `-- META-INF
100+
| `-- maven
101+
| `-- archetype-metadata.xml
102+
`-- test
103+
`-- resources
104+
`-- projects
105+
`-- it-basic
106+
|-- archetype.properties
107+
`-- goal.txt
108+
```
109+
110+
## 3 本地安装与使用
111+
112+
在第一步中,我们生成了`artifact project`,为了使用这个模板,我们需要在本地进行安装
113+
114+
> 安装本地仓库
115+
116+
和所有的源码发布流程一样,archetype生成了一个支持发布的pom配置, 因此只需要执行`install`即可
117+
118+
```
119+
cd target/generated-sources/archetype
120+
mvn install
121+
```
122+
123+
安装完毕后,能在本地的`~/.m2/repository/archetype-catalog.xml`中找到我们安装的`archetype`信息
124+
125+
```xml
126+
<?xml version="1.0" encoding="UTF-8"?>
127+
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0
128+
http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
129+
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
130+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
131+
<archetypes>
132+
<archetype>
133+
<groupId>com.chpengzh</groupId>
134+
<artifactId>template-springboot-archetype</artifactId>
135+
<version>latest</version>
136+
<description>template-springboot-archetype</description>
137+
</archetype>
138+
</archetypes>
139+
</archetype-catalog>
140+
141+
```
142+
143+
> 使用本地模板创建项目
144+
145+
使用交互式命令行创建就能快速创建一个新的项目
146+
147+
```
148+
mvn archetype:generate -DarchetypeCatalog=local
149+
...
150+
Choose archetype:
151+
1: local -> com.chpengzh:template-springboot-archetype (template-springboot-archetype)
152+
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
153+
Define value for property 'groupId': com.alipay.sofa
154+
Define value for property 'artifactId': a-new-project
155+
Define value for property 'version' 1.0-SNAPSHOT: :
156+
Define value for property 'package' com.alipay.sofa: :
157+
Confirm properties configuration:
158+
groupId: com.alipay.sofa
159+
artifactId: a-new-project
160+
version: 1.0-SNAPSHOT
161+
package: com.alipay.sofa
162+
Y: : y
163+
...
164+
```
165+
166+
在shell场景,我们也可以使用预设环境变量的方式避免交互
167+
168+
```
169+
yes | mvn archetype:generate \
170+
-DarchetypeCatalog=local \
171+
-DarchetypeGroupId=com.chpengzh \
172+
-DarchetypeArtifactId=template-springboot-archetype \
173+
-DarchetypeVersion=latest \
174+
-DgroupId=com.alipay.sofa \
175+
-DartifactId=a-new-project \
176+
-Dversion=1.0
177+
```
178+
179+
## 4. 发布与使用
180+
181+
和安装过程一致,要发布一个`archetype jar`的过程只需要执行`mvn deploy`即可;
182+
183+
关于发布的具体流程,请参阅[Deploy Plugin](https://maven.apache.org/plugins/maven-deploy-plugin/),本章节不详细讲解;
184+
185+
> 发布archetype
186+
187+
修改`target/generated-sources/archetype/pom.xml`
188+
189+
```xml
190+
<distributionManagement>
191+
<repository>
192+
<id>archetype-release</id>
193+
<name>Archetype Releases</name>
194+
<url>http://xxx</url>
195+
</repository>
196+
<snapshotRepository>
197+
<id>archetype-snapshot</id>
198+
<name>Archetype Snapshots</name>
199+
<url>http://xxx</url>
200+
</snapshotRepository>
201+
</distributionManagement>
202+
```
203+
204+
`~/.m2/settings.xml`中添加授权信息(略)
205+
206+
发布
207+
208+
```
209+
cd target/generated-sources/archetype
210+
mvn deploy
211+
```
212+
213+
> 使用发布的模板
214+
215+
交互过程同本地创建
216+
217+
```
218+
mvn archetype:generate \
219+
-DarchetypeCatalog=http://username:password@xxx/nexus/content/repositories/myapps/archetype-catalog.xml
220+
```
221+
222+
## 阅读材料
223+
224+
- [Apache Maven Project: maven-deploy-plugin](https://maven.apache.org/plugins/maven-deploy-plugin/)
225+
- [Apache Maven Project: introduction-to-archetypes](https://maven.apache.org/guides/introduction/introduction-to-archetypes.html)
226+
- [创建自定义的archetype](https://xtuhcy.iteye.com/blog/1815061)

app/pom.xml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>template-springboot</artifactId>
7+
<groupId>com.chpengzh</groupId>
8+
<version>latest</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>app</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
</dependencies>
20+
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<artifactId>maven-checkstyle-plugin</artifactId>
25+
</plugin>
26+
<plugin>
27+
<groupId>org.codehaus.mojo</groupId>
28+
<artifactId>findbugs-maven-plugin</artifactId>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-maven-plugin</artifactId>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author chen.pengzhi ([email protected])
8+
*/
9+
@SpringBootApplication
10+
public class Application {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(Application.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=9090
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.application.name=example
2+
server.port=8080

checkstyle/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>template-springboot</artifactId>
9+
<groupId>com.chpengzh</groupId>
10+
<version>latest</version>
11+
</parent>
12+
13+
<artifactId>checkstyle</artifactId>
14+
</project>

0 commit comments

Comments
 (0)