仅将Maven依赖声明为测试运行时 [英] Declare Maven dependency as test runtime only

查看:141
本文介绍了仅将Maven依赖声明为测试运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Maven依赖项声明为仅用于测试运行时(但不是测试编译)类路径的最佳方法是什么?

What is the best way to declare a Maven dependency as only being used for the test runtime (but not test compilation) class path?

具体来说,我想要 slf4j-api (一个日志外观)作为一个典型的编译范围依赖,但我想 slf4j-simple (仅在测试运行时类路径上使用(仅适用于单元测试的准系统实现)(测试编译不需要它)。我一直这样做:

Specifically, I want slf4j-api (a logging facade) as a typical, compile-scope dependency, but I want slf4j-simple (the barebones implementation suitable for unit tests) only on the test runtime class path (it's not needed for test compilation). I've been doing this:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <scope>test</scope>
</dependency>

但是,缺点是依赖:分析报告 slf4j-simple 未使用,大概是因为它不需要编译:

However, the downside of this is that dependency:analyze reports slf4j-simple as unused, presumably because it's not needed for compilation:

[WARNING] Unused declared dependencies found:
[WARNING]    org.slf4j:slf4j-simple:jar:1.7.7:test

我不能使用运行时依赖项,因为我不希望该依赖项被传递地继承(例如,所以下游依赖项)可以使用log4j等代替)。我尝试使用 optional = true runtime ,但这会产生相同的警告。

I can't use a runtime dependency because I don't want that dependency transitively inherited (e.g. so downstream dependencies can use log4j, etc. instead). I tried runtime with optional=true, but that results in the same warning.

(请注意,我还可以为依赖插件设置 ignoreNonCompile ,但这似乎是一个非常生硬的工具,可以隐藏其他潜在的问题。)

(Note that I could also set ignoreNonCompile for the dependency plugin, but that seems like a very blunt instrument that would hide other potential problems.)

推荐答案

没有范围可以完全符合您的要求; test 是最好的选择。

There is no scope that does exactly what you want here; test is the best available option.

A test-runtime 之前已请求范围( Re:需要测试运行时范围?)建议的解决方法正是您已经发现的 ignoreNonCompile 配置。

A test-runtime scope has been requested before (Re: Need for a test-runtime scope?) and the suggested workaround is exactly the ignoreNonCompile configuration you've already discovered.

依赖:analyze 已经有一些限制(未检测到某些情况(常量,带有源保留的注释,javadoc中的链接))。您可能必须接受它所警告的任何测试 -scope依赖项都是误报。

dependency:analyze already has some limitations ("some cases are not detected (constants, annotations with source-only retention, links in javadoc)"). You may have to accept that any test-scope dependencies that it warns against are false positives.

(你可能将测试的定义拆分为一个单独的模块,该模块没有 slf4j 实现依赖项,然后在另一个模块中运行它们。我不知道认为这是值得的。)

(You could split the definition of your tests into a separate module, which would have no slf4j implementation dependencies, then run them in another module. I don't think that would be worth it.)

这篇关于仅将Maven依赖声明为测试运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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