出会いは突然に
仕事で急に遭遇したので、簡単に概要を記載します。
参考情報
Mave Assembly Plugin
Maven tips
Maven Assemblyとは
Maven Assemblyとは配布用のアーカイブファイルを作成したい場合に使用するMavenのプラグインを指します。
デフォルトで指定できる作成方法は以下3つに分かれるようです。
-
bin
バイナリ配布用のアーカイブを作成(プロジェクト成果物・README・ライセンスファイルが含まれる)
依存ライブラリは含まれない -
jar-with-dependencies
プロジェクトと依存するライブラリをまとめたJarファイルを作成 -
src
ソース配布用のアーカイブを作成
デフォルトのアセンブリコマンド
実行コマンドと作成されるファイルは以下のようになります。
$ mvn assembly:assembly -DdescriptorId=bin
$ ls target
...
hello-world-1.0-SNAPSHOT-bin.tar.bz2
hello-world-1.0-SNAPSHOT-bin.tar.gz
hello-world-1.0-SNAPSHOT-bin.zip
$ mvn assembly:assembly -DdescriptorId=jar-with-dependencies
$ ls target
...
hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar
$ mvn assembly:assembly -DdescriptorId=src
$ ls target
...
hello-world-1.0-SNAPSHOT-src.tar.bz2
hello-world-1.0-SNAPSHOT-src.tar.gz
hello-world-1.0-SNAPSHOT-src.zip
pom.xmlから実行する
とは言っても毎回長いコマンドを打つのも面倒ですよね?
pom.xml内で指定しているdescriptorRef
にbin
等のデフォルトコマンドを指定できます。
以下のサンプルでは、mvn package
時にアーカイブが実行されるようになっています。
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>bin</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- mvn package時実行 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</project>
独自アーカイブを作成したい
もちろん、自分でアーカイブする内容をカスタマイズすることもできます。
この場合、独立したxmlファイルでアーカイブ設定を作成することができます。
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd" >
<id>make-assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>/</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
独自アセンブリファイルのみの実行は以下のようになります。
DdescriptorId
ではなく、Ddescriptor
になっている点に注意です。
また、単独実行の場合、pom.xml内にassemblyプラグイン記述を行っているとエラーになるので、こちらだけを試したい場合はpom.xml側をコメントアウトしてください。
$ mvn assembly:assembly -Ddescriptor=assembly/assembly.xml
こちらのコマンド実行ではなく、pom.xmlの拡張として実行する場合は以下configuration
内のdescriptor
にファイルのパスを指定することで利用できます。
...
<configuration>
<descriptors>
<descriptor>src/assembly/src.xml</descriptor>
</descriptors>
</configuration>
...
最後に
細かく知りたい方は最初に貼った公式サイトにいろいろ情報が載っているのでご参照ください。
KotlinでもMavenを使えるので、まだまだ使い所はあるかも?
また、今回Maven Assemblyを試すにあたって、以下JetBrain社のKotlinのMaven Example Projectを使用しました。
Mavenだけではなく、gradle版などもあるので、ぜひご覧ください。
Kotlin hello-world example with maven