声明Maven依赖关系的多个作用域的正确方法? [英] Correct way to declare multiple scope for Maven dependency?

查看:469
本文介绍了声明Maven依赖关系的多个作用域的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖关系,我想在test范围内使用它(以便在运行单元测试时将它放在类路径中),并在runtime范围内使用(以便可以将其包含在WAR/EAR中) /other打包以进行部署,但不会影响对依赖工件的传递依赖关系查找.

I have a dependency that I want to use in test scope (so that it is in the classpath when I am running unit tests), and in runtime scope (so that I can contain that in WAR/EAR/other packaging for deployment, but not affecting transitive dependency lookup for dependent artifacts).

一个真实的例子是SLF4J的实现JAR(例如Logback).在运行测试时,我希望它存在于类路径中,并且希望将它包含在我的WAR/EAR中,但是我不希望依赖于我的项目的项目将其包含在传递依赖项查找中.

A real life example is SLF4J's implementation JARs (e.g. Logback). I want it to exist in the classpath when I am running tests, and I want it to be included in my WAR/EAR, but I don't want project depending on my project to include that in transitive dependency lookup.

我尝试使用<scope>test,runtime</scope>,但是Maven 3产生警告:

I tried to use <scope>test,runtime</scope> but Maven 3 produces a warning:

[WARNING] 'dependencies.dependency.scope' for org.slf4j:jcl-over-slf4j:jar 
must be one of [provided, compile, runtime, test, system] but is 'test,runtime'. 

在这种情况下声明依赖关系范围的正确方法是什么?

What is the right way for declaring the dependency scope in such a case?

推荐答案

运行时范围还使工件在测试类路径上可用.只需使用运行时. (请参阅Maven 文档.)

The runtime scope also makes the artifact available on the test classpath. Just use runtime. (See the Maven documentation.)

为避免暂时解决依赖关系,也请使其成为可选<optional>true</optional>:

To avoid having the dependency resolved transitively, also make it optional with <optional>true</optional>:

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback</artifactId>
  <version>0.5</version>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

这篇关于声明Maven依赖关系的多个作用域的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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