• Maven之多模块打包成一个jar包及assembly(转)


    assembly例子https://github.com/javahongxi/whatsmars/tree/master/whatsmars-spring

    一、多模块打包

    Xml代码  
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    5.     <parent>  
    6.         <artifactId>whatsmars-parent</artifactId>  
    7.         <groupId>com.itlong</groupId>  
    8.         <version>1.0-SNAPSHOT</version>  
    9.     </parent>  
    10.     <modelVersion>4.0.0</modelVersion>  
    11.   
    12.     <artifactId>earth</artifactId>  
    13.     <packaging>jar</packaging>  
    14.     <name>${project.artifactId}</name>  
    15.     <description>The all in one project of whatsmars-earth</description>  
    16.     <properties>  
    17.         <skip_maven_deploy>false</skip_maven_deploy>  
    18.     </properties>  
    19.   
    20.     <dependencies>  
    21.         <dependency>  
    22.             <groupId>com.itlong</groupId>  
    23.             <artifactId>whatsmars-common</artifactId>  
    24.             <version>${project.parent.version}</version>  
    25.         </dependency>  
    26.         <dependency>  
    27.             <groupId>com.itlong</groupId>  
    28.             <artifactId>whatsmars-earth-domain</artifactId>  
    29.             <version>${project.parent.version}</version>  
    30.         </dependency>  
    31.         <dependency>  
    32.             <groupId>com.itlong</groupId>  
    33.             <artifactId>whatsmars-earth-dao</artifactId>  
    34.             <version>${project.parent.version}</version>  
    35.         </dependency>  
    36.         <dependency>  
    37.             <groupId>com.itlong</groupId>  
    38.             <artifactId>whatsmars-earth-service</artifactId>  
    39.             <version>${project.parent.version}</version>  
    40.         </dependency>  
    41.     </dependencies>  
    42.   
    43.     <build>  
    44.         <plugins>  
    45.             <plugin>  
    46.                 <artifactId>maven-source-plugin</artifactId>  
    47.                 <executions>  
    48.                     <execution>  
    49.                         <id>attach-sources</id>  
    50.                         <phase>none</phase>  
    51.                     </execution>  
    52.                 </executions>  
    53.             </plugin>  
    54.             <plugin>  
    55.                 <artifactId>maven-javadoc-plugin</artifactId>  
    56.                 <executions>  
    57.                     <execution>  
    58.                         <id>attach-javadoc</id>  
    59.                         <phase>deploy</phase>  
    60.                         <goals>  
    61.                             <goal>jar</goal>  
    62.                         </goals>  
    63.                     </execution>  
    64.                 </executions>  
    65.                 <configuration>  
    66.                     <show>public</show>  
    67.                     <charset>UTF-8</charset>  
    68.                     <encoding>UTF-8</encoding>  
    69.                     <docencoding>UTF-8</docencoding>  
    70.                     <excludePackageNames>com.itlong.com.*</excludePackageNames>  
    71.                     <links>  
    72.                         <link>http://docs.oracle.com/javase/6/docs/api</link>  
    73.                     </links>  
    74.                 </configuration>  
    75.             </plugin>  
    76.             <plugin>  
    77.                 <groupId>org.apache.maven.plugins</groupId>  
    78.                 <artifactId>maven-shade-plugin</artifactId>  
    79.                 <version>1.4</version>  
    80.                 <executions>  
    81.                     <execution>  
    82.                         <phase>package</phase>  
    83.                         <goals>  
    84.                             <goal>shade</goal>  
    85.                         </goals>  
    86.                         <configuration>  
    87.                             <createSourcesJar>false</createSourcesJar>  
    88.                             <promoteTransitiveDependencies>true</promoteTransitiveDependencies>  
    89.                             <artifactSet>  
    90.                                 <includes>  
    91. <!-- 这里只是随便拿了几个模块来作为例子,实际上如下模块是不应该打包在一起的 -->  
    92.                                     <include>com.itlong:whatsmars-common</include>  
    93.                                     <include>com.itlong:whatsmars-earth-domain</include>  
    94.                                     <include>com.itlong:whatsmars-earth-dao</include>  
    95.                                     <include>com.itlong:whatsmars-earth-service</include>  
    96.                                 </includes>  
    97.                             </artifactSet>  
    98.                             <transformers>  
    99.   
    100.                             </transformers>  
    101.                         </configuration>  
    102.                     </execution>  
    103.                 </executions>  
    104.             </plugin>  
    105.         </plugins>  
    106.     </build>  
    107.   
    108. </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}srcmainconfig</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>

  • 相关阅读:
    hdu 1698
    ACM起步要点总结(转哈工大)
    RANSAC
    Android 颜色渲染(十) ComposeShader组合渲染
    java过滤html标签函数
    Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
    简单的三方登录SDK示例,Android Activity之间数据的传递
    搜索引擎日志分析
    继续过中等难度的题目
    包圈圈的题目
  • 原文地址:https://www.cnblogs.com/yasepix/p/9633793.html
Copyright © 2020-2023  润新知