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>
        <dependency>
            <groupId>com.itlong</groupId>
            <artifactId>whatsmars-earth-domain</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.itlong</groupId>
            <artifactId>whatsmars-earth-dao</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.itlong</groupId>
            <artifactId>whatsmars-earth-service</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <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>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <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>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

 二、assembly

 

你是否想要創建一個包含腳本、配置文件以及所有運行時所依賴的元素(jar)Assembly插件能幫你構建一個完整的發布包。

Assembly插件會生成 “assemblies”, 此特性等同於的Maven 1 distribution plug-in.。該插件不僅支持創建二進制歸檔文件,也支持創建源碼歸檔文件。這些assemblies定義在一個assembly描述符文件里。你可以選擇自定義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 Descriptor Format.

什么是Assembly?

“assembly”是把一組文件、目錄、依賴元素組裝成一個歸檔文件. 比如, 假設一個 Maven project定義了一個JAR artifact,它包含控制台應用程序和Swing應用程序 。這樣一個工程可以定義兩套包含描述符,一套給給控制台應用,另一套給Swing應用程序,它們包含各自的腳本、目錄和依賴。

Assembly Plugin的描述符可以定義任何一個文件或者目錄歸檔方式。舉個例子,如果的你的Maven 2工程包含”src/main/bin”這個目錄,你可以指示Assembly插件復制“src/main/bin”目錄下所有的文件到bin目錄里(歸檔文件里的目錄),並且可以修改它們的權限屬性(UNIX mode)。見 assembly descriptor.

The Maven Assembly Plugin

Maven 2.0的Assembly插件目的是提供一個把工程依賴元素、模塊、網站文檔等其他文件存放到單個歸檔文件里。

使用任何一個預定義的描述符你可以輕松的構建一個發布包。這些描述符能處理一些常用的操作,如:把依賴的元素的歸檔到一個jar文件. 當然, 你可以自定義描述符來更靈活的控制依賴,模塊,文件的歸檔方式。

 

 

maven-assembly-plugin : 是maven中針對打包任務而提供的標准插件

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

 

Java代碼   收藏代碼
  1. <plugin>  
  2.     <artifactId>maven-assembly-plugin</artifactId>  
  3.     <executions>  <!--執行器 mvn assembly:assembly-->  
  4.         <execution>  
  5.             <id>make-zip</id><!--名字任意 -->    
  6.         <phase>package</phase><!-- 綁定到package生命周期階段上 -->    
  7.         <goals>    
  8.            <goal>single</goal><!-- 只運行一次 -->    
  9.         </goals>    
  10.             <configuration>  
  11.                      <descriptors> <!--描述文件路徑-->  
  12.                           <descriptor>src/main/resources/zip.xml</descriptor>  
  13.                     </descriptors>  
  14.             </configuration>  
  15.         </execution>  
  16.     </executions>  
  17.  </plugin>  

 

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

 

Xml代碼   收藏代碼
  1. <assembly  
  2.     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">  
  5.     <id>release</id>  
  6.     <formats>  
  7.         <format>zip</format>  
  8.     </formats>  
  9.     <fileSets>  
  10.         <fileSet>  
  11.             <directory>${project.basedir}\src\main\config</directory>  
  12.             <!-- 過濾 -->  
  13.             <excludes>  
  14.                 <exclude>*.xml</exclude>  
  15.             </excludes>  
  16.             <outputDirectory>\</outputDirectory>  
  17.         </fileSet>  
  18.     </fileSets>  
  19.       
  20.     <dependencySets>  
  21.         <dependencySet>  
  22.             <useProjectArtifact>true</useProjectArtifact>  
  23.             <outputDirectory>lib</outputDirectory><!-- 將scope為runtime的依賴包打包到lib目錄下。 -->  
  24.             <scope>runtime</scope>  
  25.         </dependencySet>  
  26.     </dependencySets>  
  27. </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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM