Java9多模块Maven项目测试依赖项 [英] Java9 Multi-Module Maven Project Test Dependencies

查看:786
本文介绍了Java9多模块Maven项目测试依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块maven项目,有三个模块 core utils test-utils



Core具有以下依赖关系定义

 <依赖性> 
< groupId> my.project< / groupId>
< artifactId> utils< / artifactId>
< / dependency>
< dependency>
< groupId> my.project< / groupId>
< artifactId> test-utils< / artifactId>
< scope> test< / scope>
< / dependency>

我添加了Java 9 module-info.java 所有三个模块的定义和 core 的定义如下:

 模块my.project.core {
需要my.project.utils;
}

但我无法弄清楚如何获得 core 的测试类能够在测试执行期间看到 test-utils 类。当 maven-surefire-plugin 尝试测试运行时,我找不到类。



如果我添加需要my.project.testutils; core module-info.java

 模块my.project.core {
需要my.project.utils;
需要my.project.testutils; // test dependency
}

然后在编译时我收到错误<$找不到c $ c> my.project.testutils 模块(可能是因为它只是作为测试依赖项引入)。



如何在Java 9模块化世界中使用测试依赖项?出于显而易见的原因,我不希望我的主代码引入测试依赖项。我错过了什么吗?

解决方案

使用maven和java9,如果你的 my.project.testutils 是测试范围依赖项,您不需要在模块描述符中显式包含( requires )。






测试依赖关系通过类路径本身来处理。所以你可以简单地删除 testutils ,并在执行测试时由maven修补。

 模块my.project.core {
需要my.project.utils;
}

请参阅



我还建议你看一下在迁移Java 8项目时,我应该在哪里进行单元测试到Jigsaw 此评论由Robert确认maven的实施情况。



编辑 :创建了示例项目,模拟主模块与核心相同,依赖于 guava utils 相同, junit 依赖项与<$ c相同$ C> testutils 。


I have a multi-module maven project with three modules core, utils and test-utils

Core has the following dependencies definition

<dependency>
   <groupId>my.project</groupId>
   <artifactId>utils</artifactId>
</dependency>
<dependency>
   <groupId>my.project</groupId>
   <artifactId>test-utils</artifactId>
   <scope>test</scope>
</dependency>

I have added Java 9 module-info.java definitions for all three modules and core's looks like this:

module my.project.core {
   requires my.project.utils;
}

However I cannot figure out how to get core's test classes to be able to see the test-utils classes during test execution. When maven-surefire-plugin attempts the test run I get class not found.

If I add a requires my.project.testutils; to core's module-info.java:

module my.project.core {
   requires my.project.utils;
   requires my.project.testutils; //test dependency
}

Then at compile time I get an error that the my.project.testutils module can't be found (presumably because it's only brought in as a test dependency).

How does one work with test dependencies in a Java 9 modular world? For obvious reason's I don't want my main code to pull in test dependencies. Am I missing something?

解决方案

With maven and java9, if your my.project.testutils is a test scope dependency, you don't need to explicitly include(requires) it in the module descriptor.


The test dependencies are taken care via the classpath itself. So you can simply remove the testutils and it would be patched by maven while executing tests.

module my.project.core {
   requires my.project.utils;
}

Refer to the slide 30 pertaining to maven-compiler-plugin.

I would also suggest you take a look at Where should I put unit tests when migrating a Java 8 project to Jigsaw and this comment by Robert confirming on the implementation that maven follows.

Edit: Created a sample project drawing an analogy that the main module is same as your core, the dependency on guava is same as your utils and the junit dependency is same as your testutils.

这篇关于Java9多模块Maven项目测试依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆