【MAVEN】打包插件

1. maven-jar-plugin

作用:打包(jar)插件,設定 MAINFEST .MF文件的參數,比如指定運行的Main class、將依賴的jar包加入classpath中等等,首先我們明確一點的是maven 插件功能:compile、package、deploy…都是在${project.build.directory }/classes 文件路徑下,當然測試是在test-classes下

MANIFEST.MF文件詳解

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>                     <!-- 存檔 -->
          <addMavenDescriptor/>       <!-- 添加maven 描述 -->
          <compress/>                 <!-- 壓縮 -->
          <forced/> 
          <index/>
          <manifest>                            <!-- 配置清單(MANIFEST)-->
            <addClasspath/>                         <!-- 添加到classpath 開關 -->
            <addDefaultImplementationEntries/> 
            <addDefaultSpecificationEntries/>
            <addExtensions/>
            <classpathLayoutType/>
            <classpathMavenRepositoryLayout/>
            <classpathPrefix/>                      <!-- classpath 前綴 -->
            <customClasspathLayout/>
            <mainClass/>                            <!-- 程序主函數入口 -->
            <packageName/>                          <!-- 打包名稱 -->
            <useUniqueVersions/>                    <!-- 使用唯一版本 -->
          </manifest>
          <manifestEntries>                     <!-- 配置清單(MANIFEST)屬性 -->                       
            <key>value</key>
          </manifestEntries>
          <manifestFile/>                       <!-- MANIFEST 文件位置 -->
          <manifestSections>
            <manifestSection>
              <name/>
              <manifestEntries>
                <key>value</key>
              </manifestEntries>
            <manifestSection/>
          </manifestSections>
          <pomPropertiesFile/>
        </archive>
         
        <excludes>                          <!-- 過濾掉不希望包含在jar中的文件  --> 
            <exclude/>
        </excludes>  
        
        <includes>                          <!-- 添加文件到jar中的文件  --> 
            <include/>
        </includes>
    </configuration>  
</plugin>

最小化配置

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>  
        <archive>  
            <addMavenDescriptor>false</addMavenDescriptor>  
            <manifest>  
                <addClasspath>true</addClasspath>  
                <classpathPrefix>lib/</classpathPrefix>  
                <mainClass>com.meix.boot.Application</mainClass>  
            </manifest>
            <manifestEntries>  
                <Class-Path>./</Class-Path>  
            </manifestEntries> 
        </archive>  
         <!-- 過濾掉不希望包含在jar中的文件  -->  
        <excludes>  
            <exclude>*.xml</exclude>  
            <exclude>spring/**</exclude>  
            <exclude>config/**</exclude>  
        </excludes> 
        <!-- 這裏不做舉例了 -->
        <includes>
            <include></include>
        </includes>         
    </configuration>  
</plugin>

具體詳情標籤參照下文,其實也是直接從官網copy而已:

archive(檔案):

Element Description Type Default
addMavenDescriptor 創建的存檔是否包含以下兩個Maven文件:1、pom文件,位於 META-INF / maven / ${groupId} / ${artifactId}/pom.xml 2、pom.properties文件,位於META-INF / maven / ${groupId} / ${artifactId} /pom.properties boolean true
compress 是否壓縮 boolean true
forced 是否強制重建存檔 boolean true
index 創建的存檔是否包含 INDEX.LIST文件 boolean false
manifest
manifestEntries 要添加到清單的鍵/值對的列表 Map
manifestFile 提供自己的清單文件 File
manifestSections
pomPropertiesFile 使用此選項覆蓋自動創建的 pom.properties文件(僅當addMavenDescriptor設置爲true時) File

archive 一般按默認設置即可

manifest(清單):

Element Description Type Default
addClasspath 是否創建Class-Path清單條目 boolean false
addDefaultImplementationEntries manifest 將會包含以下內容:1.Implementation-Title:project.name2.ImplementationVersion:{project.name} 2.Implementation-Version:{project.version} 3.Implementation-Vendor-Id: ${project.groupId} 4.Implementation-Vendor: project.organization.name5.ImplementationURL:{project.organization.name} 5.Implementation-URL:{project.url} boolean false
addDefaultSpecificationEntries manifest 將會包含以下內容:1.Specification-Title: ${project.name} 2.Specification-Version: project.artifact.selectedVersion.majorVersion3.{project.artifact.selectedVersion.majorVersion} 3.{project.artifact.selectedVersion.minorVersion} Specification-Vendor: ${project.organization.name} boolean false
addExtensions 是否創建Extension-List清單條目 boolean false
classpathLayoutType 在創建的Class-Path中格式化條目時要使用的佈局類型(3個值simple、repository、custom) String simple
classpathMavenRepositoryLayout
classpathPrefix Class-Path前綴 String “”
customClasspathLayout
mainClass The Main-Class manifest entry(主程序入口) String
packageName The Main-Class manifest entr
useUniqueVersions

manifest主要關注的屬性有:addClasspath、classpathPrefix、mainClass

manifestSection 默認設置即可

Element Description Type
name The name of the section String
manifestEntries A list of key/value pairs to add to the manifest(添加到manifest的一組鍵值對) Map
pom.properties content

The auto created pom.properties file will contain the following content:

version=${project.version}
groupId=${project.groupId}
artifactId=${project.artifactId}

2. maven-assembly-plugin

3. maven-shade-plugin

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章