【问题标题】:Exclude transitive dependency of Gradle plugin排除 Gradle 插件的传递依赖
【发布时间】:2022-04-24 05:01:51
【问题描述】:

在 Gradle 中排除传递依赖非常简单:

compile('com.example.m:m:1.0') {
     exclude group: 'org.unwanted', module: 'x'
  }

我们将如何解决使用插件的情况:

apply: "somePlugin"

当获得依赖时,我们意识到插件带来了它自己的一些传递依赖?

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    您可以在应用插件后(从单个配置或所有配置中)删除依赖项,例如。 compile.exclude。请注意,compile 解析为“配置”;请参阅Configuration.exclude 的 javadocs。

    编辑

    请注意,如果配置已解决,排除依赖项可能会失败。


    示例脚本

    apply plugin: 'java-library'
    
    repositories {
        jcenter()
    }
    
    dependencies {
        compile 'junit:junit:4.12'
        compile 'ant:ant:1.6'
        compile 'org.apache.commons:commons-lang3:3.8'
    }
    
    // remove dependencies
    configurations.all {
      exclude group:'junit', module:'junit'
    }
    configurations.compile {
      exclude group:'org.apache.commons', module: 'commons-lang3'
    }
    
    println 'compile deps:\n' + configurations.compile.asPath
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式操作构建脚本本身的类路径:

      buildscript {
          configurations {
              classpath {
                  exclude group: 'org', module: 'foo' // For a global exclude
              }
          }
          dependencies {
              classpath('org:bar:1.0') {
                  exclude group: 'org', module: 'baz' // For excluding baz from bar but not if brought elsewhere
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        这是强制您的项目严格使用特定版本的 build.gradle.kts 的另一种方法

        val grpcVersion = "1.45.1"
        
        implementation("io.grpc:grpc-stub") {
         version {
             strictly(grpcVersion)
           }
        }
        
        

        更多信息可以在 gradle 文档中找到:https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-22
          • 2017-06-15
          • 2017-05-17
          • 2014-03-08
          • 2015-05-15
          • 2018-07-06
          • 1970-01-01
          相关资源
          最近更新 更多