maven-使用assembly自定义打包


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

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


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


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

pom.xml

 1 <!-- 打包配置 -->
 2     <build>
 3         <plugins>
 4             <plugin>
 5                 <groupId>org.apache.maven.plugins</groupId>
 6                 <artifactId>maven-assembly-plugin</artifactId>
 7                 <version>2.5.3</version>
 8                 <configuration>
 9                     <descriptors>
10                         <descriptor>src/main/assembly/assembly.xml</descriptor>
11                     </descriptors>
12                     <archive>
13                         <manifest>
14                             <mainClass>com.cmcc.dataprocess.runtime.MRTopology</mainClass>
15                         </manifest>
16                     </archive>
17                     <appendAssemblyId>false</appendAssemblyId>
18                     <finalName>${project.artifactId}-release</finalName>
19                 </configuration>
20                 <executions>
21                     <execution>
22                         <id>make-assembly</id>
23                         <phase>package</phase>
24                         <goals>
25                             <goal>single</goal>
26                         </goals>
27                     </execution>
28                 </executions>
29             </plugin>
30         </plugins>
31     </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目录下面


 

posted @ 2015-04-14 22:26  Daem0n  阅读(1626)  评论(0编辑  收藏  举报