How do I include test classes and configuration in my war for integration testing using maven?

You can also do it straightforwardly. This will add both test classes and test resources to the WEB-INF/classes:

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>process-test-classes</phase>
                    <configuration>
                        <target>
                            <copy todir="${basedir}/target/classes">
                                <fileset dir="${basedir}/target/test-classes" includes="**/*" />
                            </copy>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I also recommend you place it into separate profile like "integration" and also to override the package name in that profile to not be able to confuse normal war without tests packaged in and the testing war.

The full example with profile is here. You may run mvn clean package to have a war war-it-test.war without tests included, or you may run mvn clean package -Pintegration to have a war war-it-test-integration.war for the war with tests included.


I believe the following configuration for the maven war plugin would do what you want. You copy your test-classes to your WEB-INF/classes folder. You can even filter those resources.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-test-war</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <warSourceDirectory>${basedir}/src/test/webapp</warSourceDirectory>
        <warName>${project.artifactId}-test</warName>
        <webappDirectory>${basedir}/target/${project.artifactId}-test</webappDirectory>
        <primaryArtifact>false</primaryArtifact>
        <webResources>
            <resource>
                <directory>${basedir}/target/test-classes</directory>
                <targetPath>WEB-INF/classes</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

See http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html


You can use the maven build helper plugin to add additional folders to the "normal" class path.

But I would recommend to create an new folder for your integration test (for example src/it/java), and add this folder, but not the "normal" test folder (src/test/java) -- the same for the resources folder.