天天看点

Maven之多模块打包成一个jar包及assembly

一、多模块打包

Maven之多模块打包成一个jar包及assembly

<?xml version="1.0" encoding="utf-8"?>  

<project xmlns="http://maven.apache.org/pom/4.0.0"  

         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"  

         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  

    <parent>  

        <artifactid>whatsmars-parent</artifactid>  

        <groupid>com.itlong</groupid>  

        <version>1.0-snapshot</version>  

    </parent>  

    <modelversion>4.0.0</modelversion>  

    <artifactid>earth</artifactid>  

    <packaging>jar</packaging>  

    <name>${project.artifactid}</name>  

    <description>the all in one project of whatsmars-earth</description>  

    <properties>  

        <skip_maven_deploy>false</skip_maven_deploy>  

    </properties>  

    <dependencies>  

        <dependency>  

            <groupid>com.itlong</groupid>  

            <artifactid>whatsmars-common</artifactid>  

            <version>${project.parent.version}</version>  

        </dependency>  

            <artifactid>whatsmars-earth-domain</artifactid>  

            <artifactid>whatsmars-earth-dao</artifactid>  

            <artifactid>whatsmars-earth-service</artifactid>  

    </dependencies>  

    <build>  

        <plugins>  

            <plugin>  

                <artifactid>maven-source-plugin</artifactid>  

                <executions>  

                    <execution>  

                        <id>attach-sources</id>  

                        <phase>none</phase>  

                    </execution>  

                </executions>  

            </plugin>  

                <artifactid>maven-javadoc-plugin</artifactid>  

                        <id>attach-javadoc</id>  

                        <phase>deploy</phase>  

                        <goals>  

                            <goal>jar</goal>  

                        </goals>  

                <configuration>  

                    <show>public</show>  

                    <charset>utf-8</charset>  

                    <encoding>utf-8</encoding>  

                    <docencoding>utf-8</docencoding>  

                    <excludepackagenames>com.itlong.com.*</excludepackagenames>  

                    <links>  

                        <link>http://docs.oracle.com/javase/6/docs/api</link>  

                    </links>  

                </configuration>  

                <groupid>org.apache.maven.plugins</groupid>  

                <artifactid>maven-shade-plugin</artifactid>  

                <version>1.4</version>  

                        <phase>package</phase>  

                            <goal>shade</goal>  

                        <configuration>  

                            <createsourcesjar>false</createsourcesjar>  

                            <promotetransitivedependencies>true</promotetransitivedependencies>  

                            <artifactset>  

                                <includes>  

<!-- 这里只是随便拿了几个模块来作为例子,实际上如下模块是不应该打包在一起的 -->  

                                    <include>com.itlong:whatsmars-common</include>  

                                    <include>com.itlong:whatsmars-earth-domain</include>  

                                    <include>com.itlong:whatsmars-earth-dao</include>  

                                    <include>com.itlong:whatsmars-earth-service</include>  

                                </includes>  

                            </artifactset>  

                            <transformers>  

                            </transformers>  

                        </configuration>  

        </plugins>  

    </build>  

</project>  

 二、assembly

你是否想要创建一个包含脚本、配置文件以及所有运行时所依赖的元素(jar)assembly插件能帮你构建一个完整的发布包。

目前assembly插件支持如下格式的归档文件:

zip

tar.gz

tar.bz2

jar

dir

war

and any other format that the archivemanager has been configured for

maven 2上使用assembly的简单步骤:

从预定义描述符里选择一个或者自己编写一个assembly描述符号。

工程的pom.xml里配置assembly插件。

在工程根目录下运行”mvn assembly:assembly”命令 。

什么是assembly?

“assembly”是把一组文件、目录、依赖元素组装成一个归档文件. 比如, 假设一个 maven project定义了一个jar artifact,它包含控制台应用程序和swing应用程序 。这样一个工程可以定义两套包含描述符,一套给给控制台应用,另一套给swing应用程序,它们包含各自的脚本、目录和依赖。

the maven assembly plugin

maven 2.0的assembly插件目的是提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。

使用任何一个预定义的描述符你可以轻松的构建一个发布包。这些描述符能处理一些常用的操作,如:把依赖的元素的归档到一个jar文件. 当然, 你可以自定义描述符来更灵活的控制依赖,模块,文件的归档方式。

maven-assembly-plugin : 是maven中针对打包任务而提供的标准插件

(1)、在pom.xml 文件里面的配置说明

Maven之多模块打包成一个jar包及assembly

<plugin>  

    <artifactid>maven-assembly-plugin</artifactid>  

    <executions>  <!--执行器 mvn assembly:assembly-->  

        <execution>  

            <id>make-zip</id><!--名字任意 -->    

        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->    

        <goals>    

           <goal>single</goal><!-- 只运行一次 -->    

        </goals>    

            <configuration>  

                     <descriptors> <!--描述文件路径-->  

                          <descriptor>src/main/resources/zip.xml</descriptor>  

                    </descriptors>  

            </configuration>  

        </execution>  

    </executions>  

 </plugin>  

(2)、zip.xml 文件配置如下

Maven之多模块打包成一个jar包及assembly

<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>release</id>  

    <formats>  

        <format>zip</format>  

    </formats>  

    <filesets>  

        <fileset>  

            <directory>${project.basedir}\src\main\config</directory>  

            <!-- 过滤 -->  

            <excludes>  

                <exclude>*.xml</exclude>  

            </excludes>  

            <outputdirectory>\</outputdirectory>  

        </fileset>  

    </filesets>  

    <dependencysets>  

        <dependencyset>  

            <useprojectartifact>true</useprojectartifact>  

            <outputdirectory>lib</outputdirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->  

            <scope>runtime</scope>  

        </dependencyset>  

    </dependencysets>  

</assembly>  

 (3)、zip.xml 格式属性说明

打包的文件格式

可以有:tar.zip war zip

<formats>

 <format>zip</format>

</formats>

需要打包的路径

<directory>${project.basedir}</directory>

打包后输出的路径

<outputdirectory>/</outputdirectory>

打包需要包含的文件

 <excludes>

        <exclude>junit:junit</exclude>

        <exclude>commons-lang:commons-lang</exclude>

        <exclude>commons-logging:commons-logging</exclude>

</excludes>

当前项目构件是否包含在这个依赖集合里。

<useprojectartifact>true</useprojectartifact>

依赖包打包到目录下

<dependencysets>

  <dependencyset>

   <outputdirectory>lib</outputdirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->

   <useprojectartifact>true</useprojectartifact>

   <scope>runtime</scope>

  </dependencyset>

</dependencysets>

原文链接:[http://wely.iteye.com/blog/2304825]