天天看點

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]