Maven の Assembly Plugin を使って、fat jar(依存するライブラリも含む jar)を作成する方法を書いていきます。
※ fat jar は uber-jar と呼ばれることもあります。
1. 作成方法
pom.xml に、アセンブリープラグインの定義を追加します。
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>[Main-ClassのFQCN]</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
...
</project>
<execution> の定義によって、コマンド mvn package を実行したときに、ゴール assembly:single が実行されるようになります。
2. Fat Jar の作成例
サンプルプロジェクト sample-fatjar で、fat jar を作成して実行する方法を記載します。
2.1. pom.xml の作成
以下の pom.xml を作成します。
sample-fatjar/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>org.sample</groupId>
<artifactId>sample-fatjar</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.sample.fatjar.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
ライブラリ(依存関係)に jackson-databind を追加しています。
2.2. Mainクラスの作成
以下の Mainクラスを作成します。
sample-fatjar/src/main/java/org/sample/fatjar/Main.java
package org.sample.fatjar; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { static class Dog { public String name; public int age; public Dog(String name, int age) { this.name = name; this.age = age; } } public static void main(String[] args) throws JsonProcessingException { Dog dog = new Dog("むぎ", 2); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(dog)); } }
Jackson の ObjectMapper を使用しています。
2.3. ビルドの実行
ディレクトリ sample-fatjar で、以下のコマンドを実行します。
mvn package
実行すると、target ディレクトリに以下の jar が作成されます。
- 通常のjar:
sample-fatjar-0.0.1.jar - fat jar:
sample-fatjar-0.0.1-jar-with-dependencies.jar
jar コマンドを使うと、fat jar にライブラリが含まれていることを確認できます。
jar tvf target/sample-fatjar-0.0.1-jar-with-dependencies.jar
2.4. プログラムの実行
以下のコマンドで、fat jar を実行することができます。
java -jar target/sample-fatjar-0.0.1-jar-with-dependencies.jar
実行すると、Mainクラスの標準出力を確認できます。
{"name":"むぎ","age":2}
3. mainClass について
pom.xml で <mainClass> を設定しないと、プログラム実行時に以下のエラーが出力されました。
target/sample-fatjar-0.0.1-jar-with-dependencies.jarにメイン・マニフェスト属性がありません