【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

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