您的位置:首页 > 其它

maven-使用assembly自定义打包

2015-04-14 22:26 411 查看
用maven管理项目引用依赖很方便,但是打包的时候如果是web包还好,会直接把依赖的jar包打到lib目录中,如果是jar包的话,依赖默认是不打入进去的

这样如果运行环境中没有依赖的jar包,就麻烦了

assembly是maven提供的自定义打包插件,要把哪些东西打入jar包,打入什么位置,都可以自定义

这里备忘一个常用的配置,以便以后复制粘贴

pom.xml

<!-- 打包配置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.cmcc.dataprocess.runtime.MRTopology</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}-release</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


src/main/assembly/assembly.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>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.basedir}\target\classes</directory>
<excludes>
<exclude>*.*</exclude>
</excludes>
<outputDirectory>\</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}\src\main\resources</directory>
<outputDirectory>\</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}\src\main\bin</directory>
<outputDirectory>\bin</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}\README.txt</source>
<outputDirectory>\</outputDirectory>
</file>
</files>
</assembly>


注:1.在source或者directory中可以使用绝对路径也可以使用相对路径,比如${project.basedir}\README.TXT也可以使用README.txt,但是在outputDirectory中只能使用相对路径,如果使用绝对路径,会生成类似E:这样的整个盘符文件夹;

在pom.xml中可以使用/来表示目录分隔符,但是在assembly.xml文件中如果在Windows下编译打包,要使用\来表示,特别是outputDirectory。

关于maven更多的打包插件,可以参见:http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package

其中我觉得生产javadoc和source插件在大项目中也值得一用

maven assembly插件的详细配置参数:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

如果是不使用maven的java项目,可以在eclipse中使用比较流行的fatjar插件

还有一点就是许多框架都是默认加载项目目录下的lib目录的,所以一般打包的话可以打在项目目录的lib目录下面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: