Maven maven-jar-plugin

发布时间 2023-12-25 21:03:39作者: l_v_y_forever
 

Maven maven-jar-plugin

转载自:https://www.jianshu.com/p/d44f713b1ec9

作用:打包(jar)插件,设定 MAINFEST .MF文件的参数,比如指定运行的Main class、将依赖的jar包加入classpath中等等,首先我们明确一点的是maven 插件功能:compile、package、deploy...都是在${project.build.directory }/classes 文件路径下,当然测试是在test-classes下,我的如图:

 

D:\java\work\ia-parent\ia-service-es\target\classes
 
我的项目构建路径.jpg
知道了打包所在的路径,下面就来分析一下maven-jar-plugin的功能,直接上配置详情:

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

 

 

执行打包后,jar包内部结构如图:
 
执行插件后.jpg

 

MANIFEST.MF 文件内容如下
 
图解.jpg


具体详情标签参照下文,其实也是直接从官网copy而已:

archive(档案)

ElementDescriptionTypeDefault
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(清单)

ElementDescriptionTypeDefault
addClasspath 是否创建Class-Path清单条目 boolean false
addDefaultImplementationEntries manifest 将会包含以下内容:1.Implementation-Title:${project.name} 2.Implementation-Version:${project.version} 3.Implementation-Vendor-Id: ${project.groupId} 4.Implementation-Vendor: ${project.organization.name} 5.Implementation-URL:${project.url} boolean false
addDefaultSpecificationEntries manifest 将会包含以下内容:1.Specification-Title: ${project.name} 2.Specification-Version: ${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

ElementDescriptionType
name The name of the section String
manifestEntries A list of key/value pairs to add to the manifest(添加到manifest的一组键值对) Map
manifestSection属性一般按默认设置即可。

pom.properties content

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

  1. version=${project.version}
  2. groupId=${project.groupId}
  3. artifactId=${project.artifactId}

参考文档:
http://maven.apache.org/shared/maven-archiver/index.html