【问题标题】:How to resolve all dependencies with gradle without running gradle build?如何在不运行 gradle build 的情况下使用 gradle 解决所有依赖项?
【发布时间】:2021-06-17 19:39:17
【问题描述】:

我正在使用 gradle 6.4.1。

在我的build.gradle 文件中,我应用了许多插件,例如 mkdocs、sonar 等。

另外,我有以下部分:


allprojects {
    group = 'blah'
    version = '1.0'
    apply plugin: 'checkstyle'

    repositories {
        jcenter()
        maven {
            url = "${artifactory_url}libs-release-local"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_key}"
            }
        }
    }
 
    ext {
        springCloudVersion = "Hoxton.SR10"
    }
    apply plugin: "blah.java"
    task allDeps(type: DependencyReportTask) {}
    task getRunTimeDeps(type: Copy) {
        from sourceSets.main.runtimeClasspath
        into 'runtime/'

        doFirst {
            ant.delete(dir: 'runtime')
            ant.mkdir(dir: 'runtime')
        }

        doLast {
            ant.delete(dir: 'runtime')
        }
    } 
}

这意味着我尝试在构建之前创建一些任务来解决依赖关系。

然后,我运行以下命令以尝试下载所有项目依赖项:

gradle dependencies
gradle --info allDeps
gradle --info getRunTimeDeps

我可以看到下载了许多依赖项。稍后,我运行:

gradle --info build

我可以看到 gradle 下载了一堆额外的依赖项。例如:

> Task :sub_project:compileJava 
Downloading https://jcenter.bintray.com/org/hibernate/common/hibernate-commons-annotations/5.1.2.Final/hibernate-commons-annotations-5.1.2.Final.jar to /tmp/gradle_download9086773233498314888bin
Downloading https://jcenter.bintray.com/org/hibernate/hibernate-core/5.4.30.Final/hibernate-core-5.4.30.Final.jar to /tmp/gradle_download7503997830142702838bin
Downloading https://jcenter.bintray.com/org/jboss/jandex/2.2.3.Final/jandex-2.2.3.Final.jar to /tmp/gradle_download7604295797727562679bin
Downloading https://jcenter.bintray.com/com/fasterxml/jackson/module/jackson-module-jaxb-annotations/2.11.0/jackson-module-jaxb-annotations-2.11.0.jar to /tmp/gradle_download16534853131601223061bin
Downloading https://jcenter.bintray.com/org/javassist/javassist/3.27.0-GA/javassist-3.27.0-GA.jar to /tmp/gradle_download3958588894727834776bin

我不明白为什么之前任务中没有下载依赖项,但有时我看到插件中的依赖项也没有下载。

如何执行任务并获得构建任务所需的一切,例如 build 任务不必下载东西?

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    我认为您所做的allDeps 任务应该尽可能解决(并因此下载)所有配置。所以我真的不明白你是否在编译时看到额外的文件被下载。可能是你使用的插件在配置阶段没有正确声明配置或依赖?

    但是您所做的另一项任务getRunTimeDeps 只下载其中的一些,因为它不考虑配置而不是贡献给runtimeClasspath 的配置。不包括compileOnlyannotationProcessortestImplementation 等配置。所以也许可以尝试这样的事情:

    // Groovy DSL, root project
    allprojects {
      task cacheDeps {
        doLast {
          configurations.each { conf ->
            if (conf.isCanBeResolved()) {
              conf.resolve()
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您是否尝试过以下方法?

      ./gradlew build --refresh-dependencies
      

      【讨论】:

      • 据我了解,这实际上是执行构建,但会强制下载所有依赖项。我不想执行构建,只下载构建的依赖项。
      猜你喜欢
      • 2018-11-06
      • 2014-11-17
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多