java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > maven assembly打包

maven 使用assembly 进行打包的方法

作者: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打包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文