如何使 Maven 依赖于运行时类路径而不依赖于测试类路径?

发布于 2024-10-14 19:35:44 字数 305 浏览 2 评论 0原文

我有一个情况,我想要依赖于运行时类路径,但测试类路径。有问题的依赖项是 Logback,一个 SLF4J 实现。在运行时,我希望我的代码(可选)依赖于 logback,以便它具有可用的日志记录基础设施。然而,在测试时,我想使用 slf4j-nop 实现来黑洞日志输出。使用 logback 作为运行时依赖项和 slf4j-nop 作为测试依赖项,我在运行测试时收到来自 SLF4J 的多重实现警告。我没有找到从测试类路径中排除 logback 的方法。

如果可以避免的话,我不想将我的测试分成一个单独的包。

有想法吗?

I have a case where I want a dependency on the runtime classpath but not the test classpath. The dependency in question is Logback, an SLF4J implementation. In runtime, I want my code to (optionally) depend on logback so that it has a logging infrastructure available. At test time, however, I want to use the slf4j-nop implementation to black-hole the log output. With logback as a runtime dependency and slf4j-nop as a test dependency, I get a multiple implementation warning from SLF4J when running my tests. I do not see a way to exclude logback from the test classpath.

I do not want to split my tests into a separate package if it can be avoided.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如若梦似彩虹 2024-10-21 19:35:44

我终于找到了真正的解决方案。从 Maven Surefire 插件 2.6 版本开始,现在有一个 classpathDependencyExcludes 配置元素,允许从类路径中排除特定的依赖项。因此,这有效:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.8</version>
  <configuration>
    <classpathDependencyExcludes>
      <classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
    </classpathDependencyExcludes>
  </configuration>
</plugin>

I've finally found a real solution to this. Since version 2.6 of the Maven Surefire plugin, there is now a classpathDependencyExcludes configuration element that allows specific dependencies to be excluded from the classpath. This therefore works:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.8</version>
  <configuration>
    <classpathDependencyExcludes>
      <classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
    </classpathDependencyExcludes>
  </configuration>
</plugin>
大姐,你呐 2024-10-21 19:35:44

如果您想要禁用日志输出,请将 logback 配置文件添加到 src/test/resources 中,该文件将丢弃所有输出。

如果您需要对同一反应器构建中的多个模块执行此操作,请考虑使用 maven远程资源插件

此插件用于从远程存储库检索资源 JAR,处理这些资源,并将它们合并到您使用 Maven 构建的 JAR 中。一个非常常见的用例是需要在整个组织中以一致的方式打包某些资源。

If disabling log output is what you want, add a logback configuration file to src/test/resources which discards all output.

If you need to do this for multiple modules in the same reactor build, consider using the maven remote resources plugin.

This plugin is used to retrieve JARs of resources from remote repositories, processes those resources, and incorporate them into JARs you build with Maven. A very common use-case is the need to package certain resources in a consistent way across your organization.

用心笑 2024-10-21 19:35:44

据我所知,您不必将其从测试类路径中排除。 Maven 应该保持类路径中依赖项的顺序。如果将测试依赖项放在依赖项中的运行时依赖项之前,它也应该位于类路径的第一个位置,并且当两个依赖项包含相同的类时,类加载器应该首先找到测试依赖项中的类。因此 slf4j 将找到 slf4j-nop 的静态绑定,而不是 logback 绑定。

As far as i know you don't have to exclude it from the test classpath. Maven should keep the order of dependencies in the classpath. If you put your test dependency before the runtime dependency in the dependencies it should also be first in the classpath and a ClassLoader should find the classes in the test dependency first when 2 dependencies contain the same classes. So slf4j would then find the static binding of slf4j-nop and not the logback binding.

太阳男子 2024-10-21 19:35:44

从 slf4j-nop 测试范围的依赖项中添加对 logback 的依赖项排除是否有效?像这样的东西

<dependency>
  <groupId>foo</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.0</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>  
      <groupId>foo</groupId>
      <artifactId>logback</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

Would it work to add a dependency exclusion on logback from the slf4j-nop test-scoped dependency? Something like

<dependency>
  <groupId>foo</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.0</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>  
      <groupId>foo</groupId>
      <artifactId>logback</artifactId>
    </exclusion>
  </exclusions> 
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文