fatのJarファイルを作成する – Maven Assembly Plugin

このチュートリアルでは、リンク付きのfat/uber jarを作成する方法を説明します:[Maven Assembly Plugin]。これは、依存関係のJarと一緒にJarファイルを1つの実行可能なJarファイルに作成することを意味します。
プラグイン]このようなテクニックを使って解決してください
class
移転する。
。リンク://maven/maven-create-a-fat-jar-file-one-jar-example/[Maven
one-jar plugin]を選択し、依存関係jarファイルをプロジェクトjarに直接追加し、カスタムクラスローダーでロードします。
1. Javaプロジェクトをレビューする
以前のJavaプロジェクト(リンク://maven/maven-create-a-fat-jar-file-one-jar-example/[dateutils])は再利用されます。

-
注意** このプロジェクトは単一の依存関係を持っています –
joda-time.jar
2. Pom.xml
コメントを読んで自明です。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4__0__0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.core.utils</groupId>
<artifactId>dateUtils</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dateUtils</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<jodatime.version>2.5</jodatime.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
</dependencies>
<build>
<finalName>dateutils</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.mkyong.core.utils.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.パッケージ
上記の “Maven Assembly Plugin”は、Mavenのパッケージングフェーズにバインドされ、最後のJarを生成します。
$ mvn package [INFO][INFO]--- maven-jar-plugin:2.4:jar (default-jar) @ dateUtils ---[INFO]Building jar:/Users/mkyong/dateUtils/target/dateutils.jar[INFO][INFO]--- maven-assembly-plugin:2.4.1:single (make-assembly) @ dateUtils ---[INFO]Building jar:/Users/mkyong/dateUtils/target/dateutils-jar-with-dependencies.jar[INFO]------------------------------------------------------------------------[INFO]BUILD SUCCESS[INFO]------------------------------------------------------------------------[INFO]Total time: 4.325s[INFO]Finished at: Tue Oct 21 13:44:41 MYT 2014[INFO]Final Memory: 17M/42M[INFO]------------------------------------------------------------------------
2つのjarファイルが `target`フォルダに作成されます。
-
dateutils.jar – プロジェクトクラスのみ
-
dateutils-jar-with-dependencies.jar – プロジェクトおよび依存クラス
単一のジャーで
4.レビュー
`dateutils-jar-with-dependencies.jar`の内容をリストしてください
$ jar tf target/dateutils-jar-with-dependencies.jar META-INF/META-INF/MANIFEST.MF org/org/joda/org/joda/time/org/joda/time/base/org/joda/time/chrono/org/joda/time/tz/ZoneInfoCompiler$DateTimeOfYear.class org/joda/time/tz/ZoneInfoCompiler$Rule.class org/joda/time/tz/ZoneInfoCompiler$RuleSet.class org/joda/time/tz/ZoneInfoCompiler$Zone.class org/joda/time/tz/ZoneInfoCompiler.class org/joda/time/tz/ZoneInfoProvider.class org/joda/time/UTCDateTimeZone.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/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 com/com/mkyong/com/mkyong/core/com/mkyong/core/utils/com/mkyong/core/utils/App.class META-INF/maven/com.mkyong.core.utils/META-INF/maven/com.mkyong.core.utils/dateUtils/META-INF/maven/com.mkyong.core.utils/dateUtils/pom.xml META-INF/maven/com.mkyong.core.utils/dateUtils/pom.properties
MANIFEST.MF
Manifest-Version: 1.0 Built-By: mkyong Build-Jdk: 1.7.0__05 Created-By: Apache Maven 3.1.1 Main-Class: com.mkyong.core.utils.App Archiver-Version: Plexus Archiver
それを実行します
$ java -jar target/dateutils-jar-with-dependencies.jar 2014-10-21
ソースコードをダウンロードする
ダウンロードする – リンク://wp-content/uploads/2014/10/dateUtils-maven-assembly-plugin.zip[dateUtils-maven-assembly-plugin.zip](7 KB)
参考文献
-
http://maven.apache.org/plugins/maven-assembly-plugin/
[Apache Maven
アセンブリプラグイン]。 link://maven/maven-create-a-fat-jar-file-one-jar-example/[Fatを作成する
JARファイル – Maven One-JARの例]。 link://maven/create-a-fat-jar-file-maven-shade-plugin/[Maven shade
プラグイン]。 link://maven/how-to-create-a-jar-file-with-maven/[jarを作成する方法
Mavenのファイル]