How To Reuse Java EE 6 WAR Code With Maven (3) 📎
Configuration of maven-jar-plugin and maven-install-plugin solves the issue. The first plugin creates a JAR and the latter installs it into the local repo:
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-jar</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
After configuration of both plugins, WAR contents are redundantly stored as JAR in the local maven repository and are accessible to other projects by specifying a "usual" dependency to it.
You could also use the attachClasses tag to deploy the jar in addition to the WAR with the "classes" classifier (as suggested by struberg):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
But then you will also have to specify the classifier in the dependency:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<classifier>classes</classifier>
<scope>test</scope>
</dependency>