Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto add another test source folder to Maven and compile it to a separate folder?

I have the default src/test/java folder for our unit tests. A separate folder src/integration/java is available for the integration tests.

I configured the maven-surefire-plugin to execute the unit/integration tests in their respective phases. This works great when the compiled classes are in the correct directory. Unfortunately Maven only supports one test source folder and one test output folder.

With mavens build-helper plugin I could add another test-source folder but the compiled classes will be generated into test-classes but I want to compile the classes from src/integration/java into target/integration-test-classes. Is this possible?

src/test/java > target/test-classes src/integration/java > target/integration-test-classes 

PS: I don't like this exclude/include on package base solution (exclude all **/it/** files from the default test phase, and exclude all **/unit/** from the integration phase.

like image 457
Christopher Klewes Avatar asked Apr 13 '12 09:04

Christopher Klewes


People also ask

What is Sourcedirectory in Maven?

Maven - Directory Structure (Project)the src/main/resources directory contains the project resources, the src/test/java directory contains the test source, the src/test/resources directory contains the test resources, the target/classes directory contains the compiled classes.

How do I run an integration-test in Maven?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

Which option rightly gives the project's source directory in Maven?

Source Directories The Maven property ${project. basedir} defaults to the top level directory of the project, so the build directory defaults to the target directory in project base dir. It also sets the property ${project.

What is Maven generate sources?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.


1 Answers

Based what you've written it sounds like you didn't named your integration tests correctly and you didn't use the maven-failsafe-plugin for your integration tests. Based on the convention of the maven-failsafe-plugin you should name your integration tests like *IT.java. If you named your integration tests appropriately you can handle that with a more or less configuration like this:

<project ...>   [...]   <build>     [...]      <plugin>         <groupId>org.codehaus.mojo</groupId>         <artifactId>build-helper-maven-plugin</artifactId>         <version>1.9.1</version>         <executions>           <execution>             <id>add-test-source</id>             <phase>generate-test-sources</phase>             <goals>               <goal>add-test-source</goal>             </goals>             <configuration>               <sources>                 <source>src/integration/java</source>               </sources>             </configuration>           </execution>         </executions>       </plugin>       [...]   </build>   [...] </project> 

With the above it's possible to hold the integration tests within the same module. But this will not solve the idea of having the compiled integration tests classes into a separate folder.

Sometimes it's better to have a separate integration test module which contains only the integration tests (which results in having a multi-module build). If you like to leave the conventions of Maven you can try to configure the maven-compiler-plugin to use a different output path (eg. target/integration-tests/classes) which don't think will really work.

like image 149
khmarbaise Avatar answered Sep 19 '22 14:09

khmarbaise