java9模块系统maven,Java9多模块Maven项目测试依赖项

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

Core has the following dependencies definition

my.project

utils

my.project

test-utils

test

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;

}

ZTJ1C.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值