Using a WAR module as dependency in Maven

Today I created a Maven module which holds our integration tests. The disadvantage when holding these tests in your ‘normal’ Maven modules is that the tests are fired every time the module is built (by you or your continuous integration server like Hudson). Since these tests take rather long to execute, developers tend to use the ‘skipTests=true‘ option when they build the code, which is taking away a great part of one the main reasons why you should use Maven. See also this article for more info about this subject.
So when creating this test module I ran into the situation where I wanted to test the code I had put in a Maven WAR module. The first thing I tried was adding the WAR module as a dependency to the integration test project and make use classes that are defined in the WAR module. The configuration looks like this:

<dependency>
  <groupId>net.pascalalma.adapters</groupId>
  <artifactId>my-adapter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <type>war</type>
  <scope>test</scope>
</dependency>

However, this doesn’t work and this was where I started my search for a solution. One option to get it to work was to move the Java sources from the WAR module to a separate JAR module and include that in the WAR module. But that is a last resort because it ment quite some rework since I have to use multiple WAR modules as a dependency in this integration test module. The first option I tried out was to make use of the WarPath plugin. Although it might be useful in some cases it didn’t work for me.
The second option I tried was to make use of the war-overlay as described here. I added the following to my project pom:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <configuration>
        <webResources>
          <resource>
            <directory>${basedir}/src/main/java</directory>
            <filtering>true</filtering>
          </resource>
        </webResources>
        <overlays>
          <overlay/>
          <overlay>
            <groupId>net.pascalalma.adapters</groupId>
            <artifactId>my-adapter</artifactId>
          </overlay>
        </overlays>
      </configuration>
    </plugin>
  </plugins>
</build>

Although the result was a little better (I could see the classes in the WEB-INF of my WAR module were added to the integration test module) I still wasn’t able to refer to the classes in my integration tests…
Then I ended up at this page (I then realized I have been using this solution before but forgotten about it. So I hope by blogging it now I will remember it from now on). As described on the page I added to the pom of the WAR project the following:

<build>
...
  <plugins>
  ...
     <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.1-beta-1</version>
      <configuration>
        <attachClasses>true</attachClasses>
      </configuration>
    </plugin>
  ...
  </plugins>
...
</build>

And now I can refer to the classes of my WAR module in the integration test module by adding the following dependency:

 <dependency>
  <groupId>net.pascalalma.adapters</groupId>
  <artifactId>my-adapter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <classifier>classes</classifier>
  <scope>test</scope>
</dependency>

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Maven and tagged . Bookmark the permalink.