我知道pom类型如果是war的话,在使用mvn package 的命令就能自动将项目依赖的jar包打到web-inf 下的lib文件夹中。但是,如果pom类型为jar的话,当你调用mvn package命令,执行过程中不会将依赖的第三方包提取出来。
以前,我的做法是,等到项目要上线的时候将pom类型改成war,然后执行一下mvn package命令,这样先把所以依赖到的包提取出来,然后再把pom类型改成jar,这样虽然能完成任务,但是,总感觉这样的做法比较拙劣。
所以最近寻找了一下有没有更好的办法,其实很简单,就是使用 maven的 assembly插件,在pom.xml 添加如下插件:
- <project>
- [...]
- <build>
- [...]
- <plugins>
- <plugin>
- <!-- NOTE: We don't need a groupId specification because the group is
- org.apache.maven.plugins ...which is assumed by default.
- -->
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-5</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- [...]
- </project>
然后在命令行上输入:
mvn assembly:assembly
你会在${project}/target 文件夹下发现新生成的 {artifactId}-jar-with-dependencies.jar 这个文件
在上面的这个 命令执行的过程中,maven会将jar包所依赖的包导出,并且解压(unpackage),一并放在
这个{artifactId}-jar-with-dependencies.jar 包中,这样对于程序的部署人员来说很方便,哪怕你的项目依赖了再多的第三方包,在部署的时候都会合并到一个assembly中。
但是问题又来了,在部署的过程中我们往往还是希望,将各个第三方包单独部署,比如ibatis归ibatis包,spring包归spring包,这样以后单独为某个第三方包升级也比较方便。
其实也有解决办法:
之前使用
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
这个jar-with-dependencies是assembly预先写好的一个,组装描述引用(assembly descriptor)我们来看一下这个定义这个组装描述(assemly descriptor)的xml文件
- <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
其实只要将上面这个xml文件中的
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
改成:
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
在 main/assembly 下创建 src.xml文件,将刚才修改过的内用写入文件中,内容为:
- <assembly
- xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
将之前pom.xml 中的plugin改成如下:
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptors>
- <descriptor>src/main/assembly/src.xml</descriptor>
- </descriptors>
- </configuration>
- </plugin>
再执行
mvn assembly:assembly
这样在target文件夹中就会看见新创建出来的{artifactId}-jar-with-dependencies.jar 这个jar包
里面会将项目所依赖的所有第三方包全部打包在里面,如下图:
转载自:http://blog.youkuaiyun.com/mozhenghua/article/details/5671133