LoginSignup
0
0

More than 1 year has passed since last update.

Maven で jarファイル に dependency が反映されない場合

Last updated at Posted at 2022-09-26

備忘録

jar ファイルに <dependency> の内容が追加されない。

以下のように maven-jar-plugin を使用した pom.xml に記入した

pom.xml
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.example.test1.App</mainClass>
              <addClasspath>true</addClasspath>
              <addExtensions>true</addExtensions>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.hoge.hoge</groupId>
      <artifactId>hogehoge</artifactId>
      <version>0.0.1</version>
    </dependency>

dependency (下の方) が、maven compilemaven install で出来た target フォルダの jarファイルに反映されているかを見るには、下のコマンドで見れる。

jar tvf 生成されたjarファイル.jar | grep 検索したいdependency

これで、dependency が 見つからない場合がある。


その場合は、下の plugin を追加し、

pom.xml
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>../lib</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>

下にある maven-jar-plugin に classpathPrefix を追加する。

pom.xml
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.example.test1.App</mainClass>
+             <classpathPrefix>../lib/</classpathPrefix>
              <addClasspath>true</addClasspath>
              <addExtensions>true</addExtensions>
            </manifest>
          </archive>
        </configuration>
      </plugin>

これで、target から見たら、../lib に dependency がまとめて入り、java -jar jarファイル名.jar のコマンドを打つと、jarファイルが lib の中の class ファイルを参照して実行を行う。

新しい言語を学ぶのは難しい。

0
0
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
0
0