通用的三種打包
1.方式一: 最小化打包 maven-jar-plugin
用途: 可以用來發(fā)布maven倉(cāng)庫(kù) 或最小化共享
一般不包含第三方依賴,
可以結(jié)合maven-dependency-plugin插件把其他依賴也一起打包
示例:文章來源:http://www.zghlxwxcb.cn/news/detail-685090.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.note.swing.SpringViewApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
2. 方式二: 通用打包 maven-shade-plugin
用途: 普通工程打包為可執(zhí)行jar ,相當(dāng)于Fat Jar
shade打包一般夠用, 但是如果常用的配置項(xiàng)不能滿足需求時(shí),可以使用maven-assembly-plugin
示例:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<transformers>
<!--設(shè)置主函數(shù)-->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.note.swing.deploy.SingleAutoHelper</mainClass>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
3.方式三: 可自定義擴(kuò)展打包maven-assembly-plugin
用途: 可以自定義bin文件夾,嵌入一些bat文件或sh文件
相對(duì)于maven-shade-plugin比較復(fù)雜一些,但是對(duì)于特殊需要時(shí) ,可以考慮使用
示例
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<!--程序入口 -->
<mainClass>cn.note.swing.SpringViewApplicationcation</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>lib/*.jar</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<!--文件名后綴 -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors>
<!--assembly配置文件路徑,注意需要在項(xiàng)目中新建文件assembly/release.xml -->
<descriptor>${project.basedir}/assembly/release.xml</descriptor>
</descriptors>
</configuration>
</plugin>
常用的配置信息:
字段 | 解析 |
---|---|
formats | 是assembly插件支持的打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war??梢酝瑫r(shí)定義多個(gè)format |
id | 是添加到打包文件名的標(biāo)識(shí)符,用來做后綴。也就是說,如果按上面的配置,生成的文件就是artifactId ? {artifactId}-artifactId?{version}-assembly.tar.gz |
fileSets/fileSet | 用來設(shè)置一組文件在打包時(shí)的屬性 |
directory | 源目錄的路徑 |
includes/excludes | 設(shè)定包含或排除哪些文件,支持通配符 |
fileMode | 指定該目錄下的文件屬性,采用Unix八進(jìn)制描述法,默認(rèn)值是064 |
outputDirectory | 生成目錄的路徑 |
files/file | 與fileSets大致相同,不過是指定單個(gè)文件,并且還可以通過destName屬性來設(shè)置與源文件不同的名稱 |
dependencySets/dependencySet | 用來設(shè)置工程依賴文件在打包時(shí)的屬性,也與fileSets大致相同 |
dependencySet-unpack | 布爾值,false表示將依賴以原來的JAR形式打包,true則表示將依賴解成*.class文件的目錄結(jié)構(gòu)打包 |
dependencySet-scope | 表示符合哪個(gè)作用范圍的依賴會(huì)被打包進(jìn)去。compile與provided都不用管,一般是寫runtime |
特殊的spring-boot-maven-plugin打包方式
用途: 最常見應(yīng)用于spring-boot項(xiàng)目
spring-boot-maven-plugin 為基于maven-assembly-plugin的封裝,為了解決特殊的resources資源文件下的文件沖突問題. 如:spring.schemas和spring.handlers
示例:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>cn.note.helper.NoteHelperApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
參考文章:https://www.kongzid.com/archives/mvn1#fulu文章來源地址http://www.zghlxwxcb.cn/news/detail-685090.html
到了這里,關(guān)于maven 常用打包方式匯總的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!