Home >Java >javaTutorial >How do I exclude specific transitive dependencies in Gradle?

How do I exclude specific transitive dependencies in Gradle?

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 08:23:301064browse

How do I exclude specific transitive dependencies in Gradle?

How to Exclude Transitive Dependencies with Gradle

Background

Occasionally, you may need to exclude specific dependencies from your Gradle project. This is especially useful when a library with unwanted transitive dependencies is included in your project.

Solution

To exclude an individual transitive dependency, use the exclude rule with both group and module specified. Here's an example:

<code class="groovy">configurations {
  runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}</code>

Explanation

An Exclude Rule takes two attributes: group and module. By specifying both the group and module, you are precisely identifying the dependency you want to exclude.

If you attempt to specify an arbitrary property (e.g., name) in the Exclude Rule, Gradle will raise an error. This is because Exclude Rules only accept group and module properties.

Maven Module vs. Gradle Module

In Maven, the concept of a module is equivalent to a jar. In Gradle, however, the term module is more flexible and can refer to a single artifact or a group of related artifacts (e.g., a plugin with multiple dependencies). The specific meaning of the module in a Gradle Exclude Rule will depend on the context.

Additional Notes

  • To exclude a dependency from a specific individual dependency, use the following syntax:
<code class="groovy">dependencies {
  compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
    exclude group: "org.slf4j", module: "slf4j-log4j12"
  }
}</code>

The above is the detailed content of How do I exclude specific transitive dependencies in Gradle?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn