太いJarファイルを作成する– Maven Shadeプラグイン
このチュートリアルでは、Maven Shade Pluginを使用して、依存関係のJarとともにJarを単一の実行可能なJarファイル(fatJarまたはuberJar)に作成する方法を示します。
Note
Maven Shadeプラグインは、assembly
pluginと比較した場合、同じクラス名の競合を回避するためにclass
relocating機能を提供するため、fat / uberjarを作成するための優れたプラグインです。クラスパス。
1. Javaプロジェクトを確認する
以前のJavaプロジェクト(dateutils)が再利用されます。次のフォルダ構造を参照してください。
Note
このプロジェクトには単一の依存関係があります–joda-time.jar
2. Pom.xml
以下のコメントを読んでください。
pom.xml
4.0.0 com.example.core.utils dateUtils jar 1.0-SNAPSHOT dateUtils http://maven.apache.org 1.7 2.5 4.11 junit junit ${junit.version} test joda-time joda-time ${jodatime.version} dateutils org.apache.maven.plugins maven-eclipse-plugin 2.9 true false org.apache.maven.plugins maven-compiler-plugin 2.3.2 ${jdk.version} ${jdk.version} org.apache.maven.plugins maven-shade-plugin 2.3 package shade com.example.core.utils.App
3. パッケージ化
最終的なJarを生成するには、単にパッケージ化します。
$ mvn package [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ dateUtils --- [INFO] Building jar: /Users/example/dateUtils/target/dateutils.jar [INFO] [INFO] --- maven-shade-plugin:2.3:shade (default) @ dateUtils --- [INFO] Including joda-time:joda-time:jar:2.5 in the shaded jar. [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing /Users/example/dateUtils/target/dateutils.jar with /Users/example/dateUtils/target/dateUtils-1.0-SNAPSHOT-shaded.jar [INFO] Dependency-reduced POM written at: /Users/example/dateUtils/dependency-reduced-pom.xml [INFO] Dependency-reduced POM written at: /Users/example/dateUtils/dependency-reduced-pom.xml [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.594s [INFO] Finished at: Tue Oct 21 15:24:28 MYT 2014 [INFO] Final Memory: 17M/42M [INFO] ------------------------------------------------------------------------
2つのjarファイルがtarget
フォルダーに作成されます。
-
dateutils.jar – Project and dependency classes in a single jar, this is what you want.
-
original-dateutils.jar – Only your project classes
P.S The generated dependency-reduced-pom.xml
is for reference only, just ignore it.
4. レビューする
dateutils.jar
の内容を一覧表示します
$ jar tf target/dateutils.jar META-INF/MANIFEST.MF META-INF/ com/ com/example/ com/example/core/ com/example/core/utils/ com/example/core/utils/App.class META-INF/maven/ META-INF/maven/com.example.core.utils/ META-INF/maven/com.example.core.utils/dateUtils/ META-INF/maven/com.example.core.utils/dateUtils/pom.xml META-INF/maven/com.example.core.utils/dateUtils/pom.properties META-INF/LICENSE.txt META-INF/NOTICE.txt org/ org/joda/ org/joda/time/ org/joda/time/base/ org/joda/time/base/AbstractDateTime.class org/joda/time/base/AbstractDuration.class //... org/joda/time/Weeks.class org/joda/time/YearMonth$Property.class org/joda/time/YearMonth.class org/joda/time/YearMonthDay$Property.class org/joda/time/YearMonthDay.class org/joda/time/Years.class META-INF/maven/joda-time/ META-INF/maven/joda-time/joda-time/ META-INF/maven/joda-time/joda-time/pom.xml META-INF/maven/joda-time/joda-time/pom.properties
MANIFEST.MF
Manifest-Version: 1.0 Build-Jdk: 1.7.0_05 Built-By: example Created-By: Apache Maven 3.1.1 Main-Class: com.example.core.utils.App Archiver-Version: Plexus Archiver
それを実行します
$ java -jar target/dateutils.jar 2014-10-21
ソースコードをダウンロード
ダウンロード–dateUtils-maven-shade-plugin.zip(10 KB)