LoginSignup
3
2

More than 3 years have passed since last update.

Maven Shade Plugin で system スコープの jar ファイルもひとつにまとめる

Posted at

概要

  • Maven Shade Plugin は依存関係にある jar ファイルを1つの jar ファイルにまとめる Maven のプラグイン
  • Maven Shade Plugin はリポジトリにある jar ファイルを取り込んでくれるが、system スコープ (ローカルファイルシステム上) で指定されている jar ファイルは含めない
  • 今回は system スコープの jar ファイルもひとつの jar にまとまるように設定する

ファイル構成

├── lib
│   └── my-lib-999.999.jar ← ローカルにしか無い jar ファイル
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── App.java
        └── resources
            └── my-app.properties ← 一般的なリソースファイル

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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.example</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>

  <name>my-app</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>

    <!-- ローカルに置いている jar ファイル -->
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>my-lib</artifactId>
      <version>999.999</version>
      <scope>system</scope>
      <systemPath>${basedir}/lib/my-lib-999.999.jar</systemPath>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
      <scope>compile</scope><!-- default scope -->
    </dependency>
  </dependencies>

  <build>
    <resources>
      <!-- デフォルトの resource 設定 -->
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
      <!-- system scope の jar ファイルをリソースとして含める 設定 -->
      <resource>
        <directory>${project.basedir}</directory>
        <includes>
          <include>lib/*.jar</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <!-- 依存関係にある jar ファイルを1つの jar ファイルにまとめるプラグインを導入 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <!-- dependency-reduced-pom.xml を生成しない設定 -->
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

jar ファイルを生成する

mvn package コマンドで jar ファイルを生成する。
system スコープで指定している jar ファイルがあるため警告文が表示されるが、今回は無視する。

$ mvn package
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.example:my-app:jar:1.0
[WARNING] 'dependencies.dependency.systemPath' for com.example:my-lib:jar should not point at files within the project directory, ${basedir}/lib/my-lib-999.999.jar will be unresolvable by dependent projects @ line 27, column 19
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

生成された jar ファイルの中身を確認すると、system スコープで指定した lib/my-lib-999.999.jar が含まれている。
デフォルトの compile スコープで指定したライブラリはクラスファイルに展開されてから含められるのに対して、system スコープで指定したものは jar ファイルのまま入ることになる。
jar のまま入っても動作する際にはちゃんと読み込まれるので問題ない。

$ jar -t -f target/my-app-1.0.jar 
META-INF/
META-INF/MANIFEST.MF
lib/
lib/my-lib-999.999.jar
my-app.properties
com/
com/example/
com/example/App.class
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/my-app/
META-INF/maven/com.example/my-app/pom.xml
META-INF/maven/com.example/my-app/pom.properties
META-INF/LICENSE
META-INF/NOTICE
META-INF/maven/com.fasterxml.jackson.core/
META-INF/maven/com.fasterxml.jackson.core/jackson-core/
META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties
META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml
META-INF/services/
META-INF/services/com.fasterxml.jackson.core.JsonFactory
com/fasterxml/
com/fasterxml/jackson/
com/fasterxml/jackson/core/
com/fasterxml/jackson/core/Base64Variant.class
com/fasterxml/jackson/core/Base64Variants.class
com/fasterxml/jackson/core/FormatFeature.class
com/fasterxml/jackson/core/FormatSchema.class
(以下略)

副作用として、通常は依存する jar ファイルを含めないはずの original-my-app-1.0.jar にも system スコープの jar ファイルが入ってしまう。
そのため original-*.jar を利用する場合は注意が必要となる。

$ jar -t -f target/original-my-app-1.0.jar
META-INF/
META-INF/MANIFEST.MF
lib/
com/
com/example/
lib/my-lib-999.999.jar
my-app.properties
com/example/App.class
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/my-app/
META-INF/maven/com.example/my-app/pom.xml
META-INF/maven/com.example/my-app/pom.properties

そもそも system スコープは推奨されていない

Maven – Introduction to the Dependency Mechanism

System Dependencies
Important note: This is deprecated.

参考資料

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2