文章目錄
- Pre
- 概述
- Jar 打包方式_maven-jar-plugin
- Overview
- 使用
- 官方文檔
- Shade 打包方式_maven-shade-plugin
- Overview
- 使用
- 將部分jar包添加或排除
- 將依賴jar包內部資源添加或排除
- 自動將所有不使用的類排除
- 將依賴的類重命名并打包進來 (隔離方案)
- 修改包的后綴名
- 官方文檔
- Assembly 打包方式_maven-assembly-plugin
- Overview
- 使用
- 官方文檔

Pre
Maven - 統一構建規范:Maven 插件管理最佳實踐
概述
Maven 提供了多種打包方式,其中常見的包括三種:jar、shade、assembly。下面是它們的詳細比較:
-
Jar 打包方式:
- 描述: 這是最常見的打包方式,它創建一個標準的Java JAR文件。
- 優點: 簡單直接,適用于大多數簡單項目。
- 缺點: 不能包含項目的依賴,如果項目有外部依賴,用戶必須手動將它們添加到類路徑中。
-
Shade 打包方式:
- 描述: Maven Shade插件允許創建一個可執行的JAR文件,其中包含所有依賴。
- 優點: 生成一個獨立的可執行JAR,無需用戶手動添加依賴。
- 缺點: 可能會導致JAR文件較大,不適合所有項目。
-
Assembly 打包方式:
- 描述: Maven Assembly插件提供了一種更靈活的打包方式,允許創建各種自定義分發包。
- 優點: 可以根據項目的需要創建定制的分發包,非常靈活。
- 缺點: 配置相對復雜,適用于需要高度定制化的項目。
總結 :
- Jar方式適用于簡單項目,但對于有依賴的項目需要手動處理依賴 ; 默認的打包方式,用來打普通的project JAR包;。
- Shade方式生成可執行JAR,但可能導致文件較大; 用來打可執行jar包,也就是所謂的fat JAR包。
- Assembly方式最靈活,可以根據項目需求創建定制分發包 ; 自定義的打包結構,也可以定制依賴項等。
Jar 打包方式_maven-jar-plugin
Overview
使用maven-jar-plugin
插件, 默認的打包方式,用來打普通的project JAR包 .
使用
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.1.0</version><configuration><archive><manifest><!-- 指定入口函數 --><mainClass>類的全路徑名稱</mainClass><!-- 是否添加依賴的jar路徑配置 --><addClasspath>true</addClasspath><!-- 依賴的jar包存放位置,和生成的jar放在同一級目錄下 --><classpathPrefix>lib/</classpathPrefix></manifest></archive><!-- 不打包com.artisan.excludes下面的所有類 --><excludes>com/artisan/excludes/*</excludes></configuration></plugin></plugins>
</build>
上面配置使用這個 jar包的時候就需要在它同一級的創建一個lib目錄來存放。 可以使用includes或excludes選擇的打包某些內容
官方文檔
https://maven.apache.org/shared/maven-archiver/examples/classpath.html
Shade 打包方式_maven-shade-plugin
Overview
插件:使用maven-shade-plugin插件
maven-shade-plugin提供了兩大基本功能:
- 將依賴的jar包打包到當前jar包(常規打包是不會將所依賴jar包打進來的);
- 對依賴的jar包進行重命名(用于類的隔離);
使用
maven-shade-plugin 只存在一個goal shade:shade,需要將其綁定到 phase package 上
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><configuration><!-- put your configurations here --></configuration><executions><execution><phase>package</phase><goals><goal>shade</goal></goals></execution></executions></plugin></plugins>
</build>
然后在當前pom文件所在路徑下,執行打包命令:mvn clean package
將部分jar包添加或排除
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><excludes><exclude>jmock:*</exclude><exclude>*:xml-apis</exclude><exclude>org.apache.maven:lib:tests</exclude><exclude>log4j:log4j:jar:</exclude></excludes><includes><include>junit:junit</include></includes></artifactSet></configuration></execution></executions></plugin></plugins></build>...
</project>
- jar包以
groupId:artifactId[[:type]:classifier]
的形式表示 - 1.3版本后插件支持通配符
‘*’ and ‘?’
將依賴jar包內部資源添加或排除
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><filters><filter><artifact>junit:junit</artifact><includes><include>junit/framework/**</include><include>org/junit/**</include></includes><excludes><exclude>org/junit/experimental/**</exclude><exclude>org/junit/runners/**</exclude></excludes></filter><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></plugins></build>...
</project>
自動將所有不使用的類排除
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><minimizeJar>true</minimizeJar></configuration></execution></executions></plugin></plugins></build>...
</project>
將依賴的類重命名并打包進來 (隔離方案)
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><relocations><relocation><pattern>org.codehaus.plexus.util</pattern><shadedPattern>org.shaded.plexus.util</shadedPattern><excludes><exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude><exclude>org.codehaus.plexus.util.xml.pull.*</exclude></excludes></relocation></relocations></configuration></execution></executions></plugin></plugins></build>...
</project>
將“org.codehaus.plexus.util
”重命名為“org.shaded.plexus.util
”,原始jar包中的“org.codehaus.plexus.util.xml.Xpp3Dom
”和“org.codehaus.plexus.util.xml.pull
”不會被重命名到目的包中;
修改包的后綴名
默認會生成一個Jar包和一個以 “-shaded”為結尾的uber-jar包,可以通過配置來指定uber-jar的后綴名。
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><shadedArtifactAttached>true</shadedArtifactAttached><shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense --></configuration></execution></executions></plugin></plugins></build>...
</project>
Invalid signature file digest for Manifest main attributes
原因:有些jar包生成時,會 使用jarsigner生成文件簽名(完成性校驗),分為兩個文件存放在META-INF目錄下:
a signature file, with a .SF extension;
a signature block file, with a .DSA, .RSA, or .EC extension;
需要在生成 uber-jar時,將這些排除掉,不再進行完成性校驗,如下所示:
<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>
https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html
官方文檔
https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html
Assembly 打包方式_maven-assembly-plugin
Overview
使用maven-assembly-plugin
插件 。
日常使用比較多的是maven-assembly-plugin
插件
例如:大數據項目中往往有很多shell腳本、SQL腳本、.properties及.xml配置項等,采用assembly插件可以讓輸出的結構清晰而標準化
使用
首先在pom文件添加以下內容:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>${maven-assembly-plugin.version}<version><executions><execution><id>make-assembly</id><!-- 綁定到package生命周期 --><phase>package</phase><goals><!-- 只運行一次 --><goal>single</goal></goals></execution></executions><configuration><!-- 配置描述符文件 --><descriptor>src/main/assembly/assembly.xml</descriptor><!-- 也可以使用Maven預配置的描述符<descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs> --></configuration></plugin></plugins>
</build>
然后編寫描述符文件:
<assembly><id>assembly</id><formats><format>tar.gz</format></formats><includeBaseDirectory>true</includeBaseDirectory><fileSets><fileSet><directory>src/main/bin</directory><includes><include>*.sh</include></includes><outputDirectory>bin</outputDirectory><fileMode>0755</fileMode></fileSet><fileSet><directory>src/main/conf</directory><outputDirectory>conf</outputDirectory></fileSet><fileSet><directory>src/main/sql</directory><includes><include>*.sql</include></includes><outputDirectory>sql</outputDirectory></fileSet><fileSet><directory>target/classes/</directory><includes><include>*.properties</include><include>*.xml</include><include>*.txt</include></includes><outputDirectory>conf</outputDirectory></fileSet></fileSets><files><file><source>target/${project.artifactId}-${project.version}.jar</source><outputDirectory>.</outputDirectory></file></files><dependencySets><dependencySet><unpack>false</unpack><scope>runtime</scope><outputDirectory>lib</outputDirectory></dependencySet></dependencySets>
</assembly>
字段 | 解析 |
---|---|
formats | 是assembly插件支持的打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同時定義多個formats。 |
id | 是添加到打包文件名的標識符,用來做后綴。比如說,如果按上面的配置,生成的文件就是artifactId-{artifactId}-artifactId-{version}-assembly.tar.gz |
fileSets/fileSet | 用來設置一組文件在打包時的屬性。 |
directory | 源目錄的路徑。 |
includes/excludes | 設定包含或排除哪些文件,支持通配符。 |
fileMode | 指定該目錄下的文件屬性,采用Unix八進制描述法,默認值是064。 |
outputDirectory | 生成目錄的路徑。 |
files/file | 與fileSets大致相同,不過是指定單個文件,并且還可以通過destName屬性來設置與源文件不同的名稱。 |
dependencySets/dependencySet | 用來設置工程依賴文件在打包時的屬性,也與fileSets大致相同。 |
dependencySet-unpack | 布爾值,false表示將依賴以原來的JAR形式打包,true則表示將依賴解成*.class文件的目錄結構打包。 |
dependencySet-scope | 表示符合哪個作用范圍的依賴會被打包進去。compile與provided都不用管,一般是寫runtime。 |
按照以上配置打包好后,將.tar.gz文件上傳到服務器,解壓之后就會得到bin、conf、lib等規范化的目錄結構,十分方便。
官方文檔
https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html