2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

war を dependency に追加する時の注意点

Posted at

war プロジェクトを dependency に追加しようとして色々つまずいたのでメモ。

上手くいかない例1

war project(使われる側)

pom.xml
...
<groupId>hoge</groupId>
<artifactId>war-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
...

main project(使う側)

pom.xml
...
<dependencies>
    <dependency>
        <groupId>hoge</groupId>
        <artifactId>war-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
</dependencies>
...

いざ maven build しようとすると
Could not resolve dependencies for project ...
なんてことを言われる。よく見ると
Could not transfer artifact hoge:war-sample:jar:1.0-SNAPSHOT ...
となっており、jar を探しに行って見つからずエラーとなっているみたい。

デフォルトで jar を探しに行くので、war を入れたい場合は明示する必要があるようです。

上手くいかない例2

main project(使う側)

pom.xml
...
<dependencies>
    <dependency>
        <groupId>hoge</groupId>
        <artifactId>war-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>test</scope>
        <type>war</type> <!-- type を明示 -->
    </dependency>
</dependencies>
...

これでいける...かと思いきや、今度はコンパイルエラーが多発。
war ファイルにクラスパスが通ってないみたい。

このページ を参考に、mave-war-plugin を使って解決できました。

上手くいく例

war project(使われる側)

pom.xml
...
<groupId>hoge</groupId>
<artifactId>war-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <attachClasses>true</attachClasses>
                <classesClassifier>classes</classesClassifier>
            </configuration>
         </plugin>
    </plugins>
</build>
...

maven-war-plugin をいれ、 attachClasses = true にすると、
war ファイルと同時に jar ファイルがビルドされる。

  • war-sample-1.0-SNAPSHOT.war
  • war-sample-1.0-SNAPSHOT-classes.jar

あとは使う側で classifier を指定すれば、jar を見に行ってくれるのでクラスパスが通る。

main project(使う側)

pom.xml
...
<dependencies>
    <dependency>
        <groupId>hoge</groupId>
        <artifactId>war-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <classifier>classes</classifier>
        <scope>test</scope>
    </dependency>
</dependencies>
...

maven build が通ることを確認。

(はじめから jar に分割しておけという話ですが...)

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?