自动化构建

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

阅读之前

通过阅读能收获到什么

  1. Maven快速入门
  2. 使用Maven快速搭建大型项目

快速入门

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ mvn archetype:create 
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=packageName
-DartifactId=projectName
-Dversion=0.01-SNAPSHOT

$ mvn archetype:create
-DarchetypeArtifactId=maven-archetype-webapp
-DgroupId=packageName
-DartifactId=webappName
-Dversion=0.01-SNAPSHOT

$ mvn compile // 编译源代码
$ mvn test-compile // 编译测试代码
$ mvn test // 运行测试
$ mvn site // 产生site
$ mvn package // 打包
$ mvn install // 在本地仓库中安装
$ mvn clean // 清理
$ mvn eclipse:eclipse // 生成eclipse项目
$ mvn idea:idea // 生成idea项目
$ mvn deploy // 上传至私有服务器
$ mvn dependency:list // 查看当前项目已被解析的依赖
$ mvn dependency:resolve // 打印出已解决依赖的列表
$ mvn dependency:tree // 打印整个依赖树
$ mvn jetty:run // 运行在jetty
$ mvn tomcat:run // 运行在tomcat

依赖范围

官方解释:

compile: This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project.
provided: This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime.
runtime: It is in the runtime and test classpaths, but not the compile classpath.
test: This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
system: This scope is similar to provided except that you have to provide the JAR which contains it explicitly.
import: This scope is only supported on a dependency of type pom in the section.

中文解释:

  • compile:默认,编译测试运行均有效
  • provided:编译和测试有效
  • runtime:测试和运行有效
  • test:测试有效
  • system:类似于provided,但system范围需要指定JAR
  • import:导入

依赖传递 排除指定依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.icebreaker.rs</groupId>
<artifactId>rs-core</artifactId>
<version>1.0.1-SNAPSHOT</version>


<exclusions>
<exclusion>
<groupId>com.icebreaker.rs</groupId>
<artifactId>rs-core-lib</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

最佳实践

实例结构

1
2
3
4
5
6
7
8
9
10
.
├── rs-parent
| └── pom.xml<2>
├── rs-apis
| ├── rs-api-app
| | └── pom.xml
| ├── rs-api-other
| | └── pom.xml
| └── pom.xml<3>
└── pom.xml<1>

实例详解

项目中maven配置文件,目标<1>所在位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.icebreaker.rs</groupId>
<artifactId>rs-server-build</artifactId>
<packaging>pom</packaging>
<version>1.0.1-SNAPSHOT</version>

<name>rs-server-build</name>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>rs-parent</module>
<module>rs-lib</module>
<module>rs-cores</module>
<module>rs-apis</module>
<module>rs-tasks</module>
</modules>
</profile>
<profile>
<id>api</id>
<modules>
<module>rs-parent</module>
<module>rs-lib</module>
<module>rs-cores</module>
<module>rs-apis</module>
</modules>
</profile>
</profiles>
</project>

独立模块rs-parent中maven配置文件,目标<2>所在位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.icebreaker.rs</groupId>
<artifactId>rs-parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<commons-lang3.version>3.5</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-fileupload.version>1.3.2</commons-fileupload.version>

<spring-boot.version>1.5.3.RELEASE</spring-boot.version>

<maven-clean-plugin.version>2.6.1</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-assembly-plugin.version>2.6</maven-assembly-plugin.version>
<maven-surefire-plugin-version>2.19.1</maven-surefire-plugin-version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven-clean-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

独立模块rs-apis中maven配置文件,目标<3>所在位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>rs-parent</artifactId>
<groupId>com.icebreaker.rs</groupId>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../rs-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>rs-api-parent</artifactId>
<packaging>pom</packaging>

<modules>
<module>rs-api-common</module>
<module>rs-api-app</module>
</modules>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>

总结

文章主要总结了Maven的基本用法,结合实际案例,说明生产环境如何通过Maven组织大型项目结构