从类路径运行Maven测试 [英] run maven tests from classpath
问题描述
为了清理一些混乱的东西,我着手将测试代码全部放在一个普通的Java项目中(全部在src/main/java中),然后将其声明为<scope>test</scope>
依赖项.在另一个项目中,并希望测试能够运行.
In order to clean up something of a giant mess, I set out to put the code of my tests all in one ordinary java project (all in src/main/java), and then declare that as a <scope>test</scope>
dependency in another project, and expect the tests to run.
没有这种运气. surefire希望只运行可以在源代码中看到的测试.
No such luck. surefire wants to just run the tests that it can see in the sources.
我在这里看到一个令人遗憾的显而易见的解决方案,其中涉及build-helper-plugin并将测试作为源目录添加到测试编译环境中,但是我希望避免这样做.
I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.
万一有人想知道,这一切的原因是使用故障安全插件运行某些集成测试的POM配置太复杂了,以至于我想从测试类的运行中分离出测试类的编译.测试.
In case anyone is wondering, the reason for all this is that the POM configuration for use of the failsafe plugin to run some integration tests got so complex that I wanted to split out the compiling of the test classes from the running of the tests.
推荐答案
现在可以通过Maven Surefire v2.15实现.只需将以下类型的配置添加到surefire插件中即可:
This is now possible with Maven Surefire v2.15. Simply add the following kind of configuration to the surefire plugin:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<dependenciesToScan>
<dependency>com.group.id:my-artifact</dependency>
<dependency>com.group.id:my-other-artifact</dependency>
</dependenciesToScan>
...
</configuration>
...
</plugin>
...
</build>
您还应该在依赖项"部分声明实际的依赖项:
You should also declare the actual dependencies in the dependencies section:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>my-artifact</artifactId>
<type>test-jar</type>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>my-other-artifact</artifactId>
<type>test-jar</type>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
这篇关于从类路径运行Maven测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!