使用Maven来构建应用程序,可以非常方便地管理应用相关的资源。众所周知,应用程序中涉及到的一些依赖关系,如Java应用程序依赖jar文件,如果只是手动找到相应的资源,可能需要花费一些时间。而且,即使已经积累了库文件,在未来应用程序升级以后,还要考虑到依赖库文件的升级情况,再次搜索收集。
还有一个问题,对应用程序依赖文件的管理是个非常复杂工作,占用存储空间不说,还可能因为应用之间的版本问题导致依赖冲突。使用Maven的pom模型来构建应用程序,可以更加有效地的管理,而且配置内容非常清晰(有时多了,可能pom文件显得有点臃肿)。
下面将常用的Maven配置,整理如下,以备参考。首先,整理一个简单的目录,作为快速查询之用:
- 设置字符集
- 拷贝src/main/resources/资源文件
- 编译代码
- 、编译打包成jar文件
- 构建测试用例配置
- 输出依赖jar文件到指定目录
- 配置指定的repository
- 将应用及其依赖jar文件打成一个jar文件
具体配置的详细内容,如下所示:
1、设置字符集
1 | < properties > |
2 | < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > |
3 | </ properties > |
在需要设置字符集的地方,引用${project.build.sourceEncoding}即可。
2、拷贝src/main/resources/资源文件
01 | < build > |
02 | < pluginManagement > |
03 | < plugins > |
04 | < plugin > |
05 | < groupId >org.apache.maven.plugins</ groupId > |
06 | < artifactId >maven-resources-plugin</ artifactId > |
07 | < version >2.5</ version > |
08 | < executions > |
09 | < execution > |
10 | < id >copy-resources</ id > |
11 | < phase >package</ phase > |
12 | < goals > |
13 | < goal >copy-resources</ goal > |
14 | </ goals > |
15 | < configuration > |
16 | < encoding >${project.build.sourceEncoding}</ encoding > |
17 | < outputDirectory >${project.build.directory}</ outputDirectory > |
18 | < resources > |
19 | < resource > |
20 | < directory >src/main/resources/</ directory > |
21 | < includes > |
22 | < include >*.properties</ include > |
23 | < include >*.xml</ include > |
24 | </ includes > |
25 | < filtering >true</ filtering > |
26 | </ resource > |
27 | </ resources > |
28 | </ configuration > |
29 | </ execution > |
30 | </ executions > |
31 | </ plugin > |
32 | </ plugins > |
33 | </ pluginManagement > |
34 | </ build > |
3、编译代码
01 | < build > |
02 | < pluginManagement > |
03 | < plugins > |
04 | < plugin > |
05 | < groupId >org.apache.maven.plugins</ groupId > |
06 | < artifactId >maven-compiler-plugin</ artifactId > |
07 | < version >2.5</ version > |
08 | < configuration > |
09 | < source >1.7</ source > |
10 | < target >1.7</ target > |
11 | </ configuration > |
12 | </ plugin > |
13 | </ plugins > |
14 | </ pluginManagement > |
15 | </ build > |
可以指定源代码编译级别。
4、编译打包成jar文件
01 | < build > |
02 | < pluginManagement > |
03 | < plugins > |
04 | < plugin > |
05 | < groupId >org.apache.maven.plugins</ groupId > |
06 | < artifactId >maven-jar-plugin</ artifactId > |
07 | < executions > |
08 | < execution > |
09 | < phase >package</ phase > |
10 | < goals > |
11 | < goal >jar</ goal > |
12 | </ goals > |
13 | < configuration > |
14 | < classifier >without-configs</ classifier > |
15 | < excludes > |
16 | < exclude >*.properties</ exclude > |
17 | < exclude >*.xml</ exclude > |
18 | </ excludes > |
19 | </ configuration > |
20 | </ execution > |
21 | </ executions > |
22 | </ plugin > |
23 | </ pluginManagement > |
24 | </ build > |
可以指定打包后jar文件的文件名后缀,同时可以设置是否将配置文件也打包到jar文件中。
5、构建测试用例配置
01 | < build > |
02 | < pluginManagement > |
03 | < plugins > |
04 | < plugin > |
05 | < groupId >org.apache.maven.plugins</ groupId > |
06 | < artifactId >maven-surefire-plugin</ artifactId > |
07 | < version >2.9</ version > |
08 | < configuration > |
09 | < skip >true</ skip > |
10 | < testFailureIgnore >true</ testFailureIgnore > |
11 | </ configuration > |
12 | </ plugin > |
13 | </ plugins > |
14 | </ pluginManagement > |
15 | </ build > |
构建应用时,可以配置是否执行测试用例代码,也可以配置如果测试用例未通过是否忽略。
6、输出依赖jar文件到指定目录
01 | < build > |
02 | < pluginManagement > |
03 | < plugins > |
04 | < plugin > |
05 | < groupId >org.eclipse.m2e</ groupId > |
06 | < artifactId >lifecycle-mapping</ artifactId > |
07 | < version >1.0.0</ version > |
08 | < configuration > |
09 | < lifecycleMappingMetadata > |
10 | < pluginExecutions > |
11 | < pluginExecution > |
12 | < pluginExecutionFilter > |
13 | < groupId >org.apache.maven.plugins</ groupId > |
14 | < artifactId >maven-dependency-plugin</ artifactId > |
15 | < versionRange >[2.0,)</ versionRange > |
16 | < goals > |
17 | < goal >copy-dependencies</ goal > |
18 | < goal >unpack</ goal > |
19 | </ goals > |
20 | </ pluginExecutionFilter > |
21 | < action > |
22 | < ignore /> |
23 | </ action > |
24 | </ pluginExecution > |
25 | </ pluginExecutions > |
26 | </ lifecycleMappingMetadata > |
27 | </ configuration > |
28 | </ plugin > |
29 | </ plugins > |
30 | </ pluginManagement > |
31 | < plugins > |
32 | < plugin > |
33 | < groupId >org.apache.maven.plugins</ groupId > |
34 | < artifactId >maven-dependency-plugin</ artifactId > |
35 | < version >2.8</ version > |
36 | < executions > |
37 | < execution > |
38 | < id >copy-dependencies</ id > |
39 | < phase >package</ phase > |
40 | < goals > |
41 | < goal >copy-dependencies</ goal > |
42 | </ goals > |
43 | < configuration > |
44 | < outputDirectory >${project.build.directory}/lib</ outputDirectory > |
45 | < overWriteReleases >false</ overWriteReleases > |
46 | < overWriteSnapshots >false</ overWriteSnapshots > |
47 | < overWriteIfNewer >true</ overWriteIfNewer > |
48 | </ configuration > |
49 | </ execution > |
50 | </ executions > |
51 | </ plugin > |
52 | </ plugins > |
53 | </ build > |
上面,和pluginManagement并列的plugins元素中配置的是拷贝依赖jar文件到target/lib目录下面,如果在Eclipse中出现maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e错误,上面pluginManagement元素中的配置,可以解决这个错误提示。
7、配置指定的repository
1 | < repositories > |
2 | < repository > |
3 | < id >cloudera</ id > |
5 | </ repository > |
6 | </ repositories > |
如果我们需要要的一些依赖jar文件在maven中央repository中没有,可以在pom文件中配置特定的repository,一般需要配置id和url。
8、将应用及其依赖jar文件打成一个jar文件
01 | < build > |
02 | < plugins > |
03 | < plugin > |
04 | < artifactId >maven-assembly-plugin</ artifactId > |
05 | < configuration > |
06 | < archive > |
07 | < manifest > |
08 | < mainClass >org.shirdrn.solr.cloud.index.hadoop.SolrCloudIndexer</ mainClass > |
09 | </ manifest > |
10 | </ archive > |
11 | < descriptorRefs > |
12 | < descriptorRef >jar-with-dependencies</ descriptorRef > |
13 | </ descriptorRefs > |
14 | </ configuration > |
15 | < executions > |
16 | < execution > |
17 | < id >make-assembly</ id > |
18 | < phase >package</ phase > |
19 | < goals > |
20 | < goal >single</ goal > |
21 | </ goals > |
22 | </ execution > |
23 | </ executions > |
24 | </ plugin > |
25 | </ plugins > |
26 | </ build > |