有没有办法全局排除 Maven 依赖项?
我试图找到一种“通用”方法来排除传递依赖项,而不必将其从依赖于它的所有依赖项中排除。例如,如果我想排除 slf4j,我会执行以下操作:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jmx</artifactId>
<version>3.3.2.GA</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
这部分是为了清理 pom 文件,部分是为了避免将来人们添加依赖于该排除依赖项的依赖项而忘记排除它时出现问题。
有办法吗?
I’m trying to find a “generic” way of excluding a transitive dependency from being included without having to exclude it from all the dependencies that depend on it. For example, if I want to exclude slf4j, I do the following:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jmx</artifactId>
<version>3.3.2.GA</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
This is partly to clean up the pom file, partly to avoid issues in the future with people adding dependencies that depend on that excluded dependency — and forgetting to exclude it.
Is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有帮助吗? http://jlorenzen.blogspot.com/2009/06/maven-global -excludes.html
“假设我想从我的 WAR 中排除 avalon-framework,我会将以下内容添加到我的项目 POM 中,并提供范围。这适用于所有传递依赖项,并允许您指定一次。
这甚至在父 POM 中指定它时也有效,这将防止项目必须在所有子 POM 中声明它。”
Does this help? http://jlorenzen.blogspot.com/2009/06/maven-global-excludes.html
"Assuming I want to exclude avalon-framework from my WAR, I would add the following to my projects POM with a scope of provided. This works across all transitive dependencies and allows you to specify it once.
This even works when specifying it in the parent POM, which would prevent projects from having to declare this in all child POMs."
要展开 dnault 的评论:
可以使用 Maven Enforcer 插件的禁止依赖项规则来确保排除依赖项。人们仍然需要手动排除它们,但是如果有人错误地在其他地方添加依赖项,构建将会失败。
还有一个开放的功能请求:MNG-1977 全局依赖项排除
To expand on dnault's comment:
One can use the Maven Enforcer plugin's Banned Dependencies rule to ensure dependencies are excluded. One still has to exclude them manually, but the build will fail if anyone adds the dependency elsewhere by mistake.
Also there is an open feature request: MNG-1977 Global dependency exclusions
提醒一下,这是 Maven 官方文档的答案:
如果想要使构建更加健壮,则<可以使用strong>版本范围。这将确保新版本的依赖项不会干扰项目。
任何 slf4j-api 版本 >= 1.4.2 都将被视为在运行时提供(提供),无论是从配置的类路径还是容器。
参考
As a reminder, here is the answer from Maven official documentation:
If one wants to make a build more robust, a version range can be used. This would ensure that no newer version of the dependency can interfere with the project.
Any slf4j-api version >= 1.4.2 will be considered as offered (provided) at runtime, either from a configured classpath or a container.
References
我创建了一个空 jar 并创建了此依赖项:
它并不完美,因为从现在开始,您的编译/测试路径中有一个空 jar。但这只是表面现象。
I created an empty jar and created this dependency:
It is not perfect because from now on you have an empty jar in your compile/test path. But that is just cosmetic.