1. 程式人生 > 程式設計 >maven 使用assembly 進行打包的方法

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 中。

具體結構如下圖所示:

maven 使用assembly 進行打包的方法

參考地址:
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打包內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!