[JAVA] Things to keep in mind when adding war to dependency

Note that I stumbled upon trying to add the war project to the dependency.

Example 1 that does not work

war project (used side)

pom.xml


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

main project (user side)

pom.xml


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

When I try to maven build Could not resolve dependencies for project ... What are you saying. If you look closely Could not transfer artifact hoge:war-sample:jar:1.0-SNAPSHOT ... It seems that I went to look for the jar and could not find it, resulting in an error.

By default it will look for the jar, so if you want to put a war it seems you need to specify it.

Example 2 that doesn't work

main project (user side)

pom.xml


...
<dependencies>
    <dependency>
        <groupId>hoge</groupId>
        <artifactId>war-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>test</scope>
        <type>war</type> <!--Explicit type-->
    </dependency>
</dependencies>
...

I wondered if this would work, but this time there were many compilation errors. It seems that the classpath does not pass in the war file.

This page, And I was able to solve it by using mave-war-plugin.

Examples that work

war project (used side)

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

If you insert maven-war-plugin and set attachClasses = true, The jar file is built at the same time as the war file.

After that, if you specify the classifier on the user side, it will go to see the jar, so the classpath will pass.

main project (user side)

pom.xml


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

Confirm that maven build passes.

(It's a story that you should divide it into jars from the beginning ...)

Recommended Posts

Things to keep in mind when adding war to dependency
Things to keep in mind when committing to CRuby
Things to keep in mind when using if statements
Things to keep in mind when testing private methods in JUnit
Things to keep in mind when using Sidekiq with Rails
Things to keep in mind when combining if statements and logical operators
Things to keep in mind when using Apache PDFBox® with AWS Lambda
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
Things to keep in mind when installing Jekyll on Windows and loading themes! !! Need tzinfo !!
[Java Bronze] 5 problems to keep in mind
[Comma (,) is strictly prohibited in the address! ] Things to keep in mind when applying for an exam at Pearson VUE
Things to be aware of when writing code in Java
Things to note when using Spring AOP in Jersery resource classes
Things to note in conditional expressions
Command when dependency is broken in Ubuntu 20.04
Things to be aware of when writing Java
When the server fails to start in Eclipse
I get an error when adding a dependency
When you want to bind InputStream in JDBI3
Let's keep this in mind What's new in Java 9
[Ruby] When adding a null constraint to a table
What to do when IllegalStateException occurs in PlayFramework
How to log in automatically when Ubuntu restarts
Things to watch out for in Java equals