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

三世轮回 提交于 2019-12-03 04:16:20

Sorry, there's no way of doing that, IMHO even with some hacking in mind. The concept is that there's only one target directory for compiled classes and one for compiled test classes (even <build> tag schema exposes this). To be honest, I don't really think it should be possible with Maven. Maven promotes straight, clean and legible design of your application, by using well-crafted modules.

If think what you really want to do is to actually create integration tests module. That's the common practice, in fact. So far I always had separate integration testing module and never had problems with that. You should of course depend on all needed modules to run these tests. You can even depend on other module's test classes by using <type>test-jar</type> with your dependency declaration as mentioned here:

http://maven.apache.org/guides/mini/guide-attached-tests.html

I don't like this method, however, and usually prefer to have separate module with testing support stuff, like base classes for JUnit test cases etc.

khmarbaise

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.

If you only want to change the unit test source folder (rather than add an additional one), just change the testSourceDirectory element:

<build>
    <testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>

This is useful if all of your unit tests are written in groovy. (But you will also need to configure maven to compile your groovy code too - see the groovy-eclipse-maven-plugin or the build-helper-maven-plugin.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!