如果你的项目使用maven构建的话,当项目要上线,部署到服务器上去的时候或许会碰见这样的问题。问题就是,服务器上没有maven的环境,也就是说,项目所依赖到的那些仓库(repository)中的jar包你需要单独提取出来上传到服务器中去。

我知道pom类型如果是war的话,在使用mvn package 的命令就能自动将项目依赖的jar包打到web-inf 下的lib文件夹中。但是,如果pom类型为jar的话,当你调用mvn package命令,执行过程中不会将依赖的第三方包提取出来。

以前,我的做法是,等到项目要上线的时候将pom类型改成war,然后执行一下mvn package命令,这样先把所以依赖到的包提取出来,然后再把pom类型改成jar,这样虽然能完成任务,但是,总感觉这样的做法比较拙劣。

所以最近寻找了一下有没有更好的办法,其实很简单,就是使用 maven的 assembly插件,在pom.xml 添加如下插件:

1. <project>
2.   [...]  
3. <build>
4.     [...]  
5. <plugins>
6. <plugin>
7.         <!-- NOTE: We don't need a groupId specification because the group is  
8.              org.apache.maven.plugins ...which is assumed by default.  
9. >
10. <artifactId>maven-assembly-plugin</artifactId>
11. <version>2.2-beta-5</version>
12. <configuration>
13. <descriptorRefs>
14. <descriptorRef>jar-with-dependencies</descriptorRef>
15. </descriptorRefs>
16. </configuration>
17.         [...]  
18. </project>

然后在命令行上输入:

mvn assembly:assembly

你会在${project}/target 文件夹下发现新生成的 {artifactId}-jar-with-dependencies.jar 这个文件

在上面的这个 命令执行的过程中,maven会将jar包所依赖的包导出,并且解压(unpackage),一并放在

这个{artifactId}-jar-with-dependencies.jar 包中,这样对于程序的部署人员来说很方便,哪怕你的项目依赖了再多的第三方包,在部署的时候都会合并到一个assembly中。

但是问题又来了,在部署的过程中我们往往还是希望,将各个第三方包单独部署,比如ibatis归ibatis包,spring包归spring包,这样以后单独为某个第三方包升级也比较方便。

其实也有解决办法:

之前使用

1. <descriptorRefs>
2. <descriptorRef>jar-with-dependencies</descriptorRef>
3. </descriptorRefs>

这个jar-with-dependencies是assembly预先写好的一个,组装描述引用(assembly descriptor)我们来看一下这个定义这个组装描述(assemly descriptor)的xml文件

1. <assembly xmlns="http:///plugins/maven-assembly-plugin/assembly/1.1.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http:///plugins/maven-assembly-plugin/assembly/1.1.0 http:///xsd/assembly-1.1.0.xsd">
4. <id>jar-with-dependencies</id>
5. <formats>
6. <format>jar</format>
7. </formats>
8. <includeBaseDirectory>false</includeBaseDirectory>
9. <dependencySets>
10. <dependencySet>
11. <unpack>true</unpack>
12. <scope>runtime</scope>
13. </dependencySet>
14. </dependencySets>
15. <fileSets>
16. <fileSet>
17. <directory>${project.build.outputDirectory}</directory>
18. </fileSet>
19. </fileSets>
20. </assembly>

其实只要将上面这个xml文件中的

1. <dependencySet>
2. <unpack>true</unpack>
3. <scope>runtime</scope>
4. </dependencySet>

改成:

1. <dependencySet>
2. <unpack>false</unpack>
3. <scope>runtime</scope>
4. </dependencySet>

在 main/assembly 下创建 src.xml文件,将刚才修改过的内用写入文件中,内容为:

1. <assembly
2. xmlns="http:///plugins/maven-assembly-plugin/assembly/1.1.0"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http:///plugins/maven-assembly-plugin/assembly/1.1.0 http:///xsd/assembly-1.1.0.xsd">
5. <id>jar-with-dependencies</id>
6. <formats>
7. <format>jar</format>
8. </formats>
9. <includeBaseDirectory>false</includeBaseDirectory>
10. <dependencySets>
11. <dependencySet>
12. <unpack>false</unpack>
13. <scope>runtime</scope>
14. </dependencySet>
15. </dependencySets>
16. <fileSets>
17. <fileSet>
18. <directory>${project.build.outputDirectory}</directory>
19. </fileSet>
20. </fileSets>
21. </assembly>

将之前pom.xml 中的plugin改成如下:

1. <plugin>
2. <artifactId>maven-assembly-plugin</artifactId>
3. <configuration>
4. <descriptors>
5. <descriptor>src/main/assembly/src.xml</descriptor>
6. </descriptors>
7. </configuration>
8. </plugin>

再执行

mvn assembly:assembly
这样在target文件夹中就会看见新创建出来的{artifactId}-jar-with-dependencies.jar 这个jar包

里面会将项目所依赖的所有第三方包全部打包在里面,如下图:

Maven 插件打包pom maven assembly plugin打包外部jar_maven