maven 使用assembly 进行打包的方法

 更新时间:2020年09月04日 11:59:30   作者:zhongzunfa  
这篇文章主要介绍了maven 使用assembly 进行打包的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1. pom 中添加assembly 插件

要使用assembly 进项编译打包, 首先主要在pom 中的build中添加插件信息, 具体如图下所示:

<build>
  <finalName>${project.artifactId}</finalName>
  <sourceDirectory>src/main/java</sourceDirectory>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>
    </resource>
    <resource>
      <directory>${profile.dir}</directory>
      <filtering>true</filtering>
    </resource>
  </resources>

  <plugins>
    <!-- compiler插件参数设置,指定编码 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
      </configuration>
    </plugin>

    <!--  这个插件是关键  -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <!--  这个是assembly 所在位置 -->
        <descriptor>src/main/assembly/assembly.xml</descriptor>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build> 

2. 创建assembly文件夹和assembly.xml文件

创建assembly文件夹和assembly.xml文件, 这个样子创建主要是规范。 

在pom 中已经介绍assembly.xml 位置。

<!--  这个是assembly 所在位置 -->
<descriptor>src/main/assembly/assembly.xml</descriptor> 

创建assembly.xml 文件后添加如下内容:

<assembly>
  <formats>
    <!--支持 zip,tar,tar.gz,tar.bz2,jar,dir,war 等 -->
    <format>tar.gz</format>
    <format>zip</format>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>src/main/resources</directory>
      <outputDirectory>conf</outputDirectory>
      <fileMode>0644</fileMode>
    </fileSet>
    <fileSet>
      <directory>${profile.dir}</directory>
      <outputDirectory>conf</outputDirectory>
      <!-- 表示的是包含下面格式的资源文件 -->
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>
      <fileMode>0644</fileMode>
    </fileSet>
    <fileSet>
      <directory>src/main/assembly/bin</directory>
      <outputDirectory>bin</outputDirectory>
      <fileMode>0755</fileMode>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
    </dependencySet>
  </dependencySets>
</assembly> 

fileMode 官方解释:

Similar to a UNIX permission, sets the file mode of the files included. THIS IS AN OCTAL VALUE. Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1. For example, the value 0644 translates to User read-write, Group and Other

上述的三个fileSet 分别是将resource 下的资源打包到config 目录下, 将assembly下的bin 启动相关脚本打包到bin 目录下, 将maven项目依赖的所有jar 包, 打包到lib 中。 

具体结构如下图所示:

参考地址:
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

zip.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>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> 

例:

<assembly>
 <id>assembly</id>
 <formats>
 <format>tar.gz</format>
 </formats>
 <includeBaseDirectory>true</includeBaseDirectory>
 <fileSets>
 <fileSet>
  <directory>${project.build.directory}/resources</directory>
  <outputDirectory>resources</outputDirectory>
  <fileMode>0755</fileMode>
 </fileSet>
 <fileSet>
  <directory>${project.build.directory}/config</directory>
  <outputDirectory>config</outputDirectory>
  <fileMode>0644</fileMode>
 </fileSet>
 <fileSet>
  <directory>${project.build.directory}/bin</directory>
  <outputDirectory>bin</outputDirectory>
  <fileMode>0755</fileMode>
 </fileSet>
 </fileSets>
 <dependencySets>
 <dependencySet>
  <outputDirectory>lib</outputDirectory>
 </dependencySet>
 </dependencySets>
</assembly>

到此这篇关于maven 使用assembly 进行打包的方法的文章就介绍到这了,更多相关maven assembly打包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • spring是如何解析xml配置文件中的占位符

    spring是如何解析xml配置文件中的占位符

    这篇文章主要介绍了spring是如何解析xml配置文件中的占位符,帮助大家更好的理解和使用spring框架,感兴趣的朋友可以了解下
    2020-11-11
  • 浅谈Hibernate对象状态之间的神奇转换

    浅谈Hibernate对象状态之间的神奇转换

    这篇文章主要介绍了浅谈Hibernate对象状态之间的神奇转换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Spring Cache使用RedisCache案例解析

    Spring Cache使用RedisCache案例解析

    这篇文章主要介绍了Spring Cache使用RedisCache案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java二分法查找_动力节点Java学院整理

    Java二分法查找_动力节点Java学院整理

    这篇文章主要介绍了Java二分法查找的相关资料,需要的朋友可以参考下
    2017-04-04
  • jenkins+maven+svn自动部署和发布的详细图文教程

    jenkins+maven+svn自动部署和发布的详细图文教程

    Jenkins是一个开源的、可扩展的持续集成、交付、部署的基于web界面的平台。这篇文章主要介绍了jenkins+maven+svn自动部署和发布的详细图文教程,需要的朋友可以参考下
    2020-09-09
  • Java微信公众平台之自定义菜单

    Java微信公众平台之自定义菜单

    这篇文章主要为大家详细介绍了Java微信公众平台之自定义菜单,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • SSM框架实现分页和搜索分页的示例代码

    SSM框架实现分页和搜索分页的示例代码

    本篇文章主要介绍了SSM框架实现分页和搜索分页的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 使用Filter实现登录权限验证

    使用Filter实现登录权限验证

    这篇文章主要为大家详细介绍了使用Filter实现登录权限验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • zookeeper+Springboot实现服务器动态上下线监听教程详解

    zookeeper+Springboot实现服务器动态上下线监听教程详解

    这篇文章主要介绍了zookeeper+Springboot实现服务器动态上下线监听,主要介绍了什么是服务器动态上下线监听及为什么要实现对服务器上下线的监听,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • IntelliJ IDEA安装插件阿里巴巴Java开发手册(Alibaba Java Coding Guidelines)

    IntelliJ IDEA安装插件阿里巴巴Java开发手册(Alibaba Java Coding Guidelines

    这篇文章主要介绍了IntelliJ IDEA安装插件阿里巴巴Java开发手册(Alibaba Java Coding Guidelines),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05

最新评论