Maven 入門 | 独自ライブラリの開発と利用 | maven-assembly-plugin で依存ライブラリを含める

依存ライブラリを含める

パッケージ化するときに、依存ライブラリを Jar ファイルに含めて保存できる。これは maven-assembly-plugin というプラグインを設定すれば行える。以下に、依存ライブラリを含めて Jar ファイルに保存するための設定を示す。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<configuration> タグについて

設定情報を記述する <configuration><descriptorRefs> というタグを持ち、さらにその中に <descriptorRef> というタグを用意している。ここに jar-with-dependencies を指定することで、Jar を一体化するための設定であることを指定できる。

その後、<executions> にある <execution> に必要な設定を行う。<phase> に package を、<goals><goal> に single をそれぞれ指定しているが、これはパッケージング時に 1 つの Jar ファイルにまとめるためのものである。Jar ファイル名は SampleMavenApp-1.0-jar-with-dependencies.jar のように -jar-with-dependencies が付く。

このプラグイン設定を pom.xml に追加し、再度 mvn package でパッケージ化してみよう。そして、もう一度 java コマンドで Jar ファイルを実行する。

$ java -classpath target\SampleMavenApp-1.0-jar-with-dependencies.ja com.devkuma.App

今回は正しくプログラムが実行されるはずである。Jar ファイルに MyLib が含まれるためだ。生成された Jar ファイルは、別のコンピュータにコピーして実行しても問題なく動作する。

SampleMavenApp の完成した pom.xml

SampleMavenApp の 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.devkuma</groupId>
  <artifactId>SampleMavenApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SampleMavenApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
       <groupId>com.devkuma.mylib</groupId>
       <artifactId>MyLib</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>com.devkuma.App</mainClass>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
                <mainClass>com.devkuma.App</mainClass>
                <addClasspath>true</addClasspath>
                <addExtensions>true</addExtensions>
                <packageName>com.devkuma</packageName>
            </manifest>
          </archive>
        </configuration>
      </plugin>
 
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
           <execution>
             <phase>package</phase>
             <goals>
               <goal>single</goal>
             </goals>
          </execution>
        </executions>
      </plugin>
     
    </plugins>
  </build>

</project>

今回行った作業からわかるように、独自ライブラリの使用では、ライブラリ自体に特別な作業は必要ない。唯一、mvn install コマンドでライブラリをローカルリポジトリにインストールするだけである。

最大のポイントは、ライブラリを利用するプロジェクト側にある。まず <dependency> タグでライブラリを追加することが必須である。これにより exec:java なら実行できる。

依存ライブラリまで含めてすべてを 1 つの Jar にまとめる exec-assembly-plugin プラグイン設定を追加する。これはプロジェクト固有の設定などがないため、ここに掲載したタグをそのままコピーして貼り付けて使用すればよい。