我有一个包含三个模块的多模块 Maven 项目core
, utils
and test-utils
Core 有以下依赖定义
<dependency>
<groupId>my.project</groupId>
<artifactId>utils</artifactId>
</dependency>
<dependency>
<groupId>my.project</groupId>
<artifactId>test-utils</artifactId>
<scope>test</scope>
</dependency>
我已经添加了Java 9module-info.java
所有三个模块的定义和core
看起来像这样:
module my.project.core {
requires my.project.utils;
}
但是我不知道如何获得core
的测试类能够看到test-utils
测试执行期间的类。什么时候maven-surefire-plugin
尝试测试运行我找不到类。
如果我添加一个requires my.project.testutils;
to core
's module-info.java
:
module my.project.core {
requires my.project.utils;
requires my.project.testutils; //test dependency
}
然后在编译时我得到一个错误my.project.testutils
找不到模块(大概是因为它只是作为测试依赖项引入)。
在 Java 9 模块化世界中如何处理测试依赖项?出于明显的原因,我不希望我的主代码引入测试依赖项。我错过了什么吗?
有了maven和java9,如果你my.project.testutils
是测试范围依赖项,您不需要显式包含(requires
) 它在模块描述符中。
测试依赖项是通过类路径本身来处理的。所以你可以简单地删除testutils
并且它会在执行测试时由 Maven 打补丁。
module my.project.core {
requires my.project.utils;
}
请参阅第 30 张幻灯片涉及 maven-compiler-plugin。 https://www.slideshare.net/RobertScholte/java-9-and-the-impact-on-maven-projects
我还建议你看一下将 Java 8 项目迁移到 Jigsaw 时应该在哪里放置单元测试 https://stackoverflow.com/questions/41366582/where-should-i-put-unit-tests-when-migrating-a-java-8-project-to-jigsaw and 这条评论 https://stackoverflow.com/questions/41366582/where-should-i-put-unit-tests-when-migrating-a-java-8-project-to-jigsaw#comment69959520_41370766Robert 确认 Maven 遵循的实现。
Edit: 创建了一个示例项目 https://github.com/namannigam/jdk9-maventest打个比方,主模块和你的一样core
,依赖于guava
和你的一样utils
和junit
依赖性与你的相同testutils
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)