简体   繁体   English

Java9多模块Maven项目测试依赖项

[英]Java9 Multi-Module Maven Project Test Dependencies

I have a multi-module maven project with three modules core , utils and test-utils 我有一个多模块maven项目,包含三个模块coreutilstest-utils

Core has the following dependencies definition Core具有以下依赖项定义

<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: 我为所有三个模块添加了Java 9 module-info.java定义, core的内容如下所示:

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. 但是我无法弄清楚如何让core的测试类能够在测试执行期间看到test-utils类。 When maven-surefire-plugin attempts the test run I get class not found. maven-surefire-plugin尝试测试运行时,我找不到类。

If I add a requires my.project.testutils; 如果我添加一个requires my.project.testutils; to core 's module-info.java : coremodule-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). 然后在编译时我得到一个错误,即无法找到my.project.testutils模块(可能是因为它只是作为测试依赖项引入)。

How does one work with test dependencies in a Java 9 modular world? 如何在Java 9模块化世界中使用测试依赖项? 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. 使用maven和java9,如果my.project.testutils是测试范围依赖项,则不需要在模块描述符中显式包含( requires )它。


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. 因此,您可以简单地删除testutils并在执行测试时由maven进行修补。

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

Refer to the slide 30 pertaining to maven-compiler-plugin. 请参阅有关maven-compiler-plugin幻灯片30。

在此输入图像描述

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. 我还建议你看看在将Java 8项目迁移到Jigsaw时我应该把单元测试放在哪里这篇评论由Robert确认maven所遵循的实现。

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 . 编辑 :创建了一个示例项目 ,其中模拟主模块与您的core相同,对guava的依赖与您的utils相同, junit依赖与testutils相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM