【问题标题】:Gradle buildSrc and buildscriptGradle buildSrc 和 buildscript
【发布时间】:2018-05-18 13:33:55
【问题描述】:

我们有一个 Gradle 构建,其中包含一个带有一些自定义插件的 buildSrc。这些插件适用于其他插件。例如,我们的插件应用com.android.tools.build:gradle。对于注释处理,该库需要在编译期间位于 Gradle 的类路径中。所以,把它放在我们的主要build.gradle 作品中:

buildscript {
    repositories {
        google()
    }
    dependencies {
       classpath "com.android.tools.build:gradle:$gToolsVersion"
    }
}

但是,这意味着用户要应用此插件,他们必须 (1) 应用我们的插件并 (2) 添加 buildscript 样板文件。看来这应该是不必要的。我们还可以在我们的插件中添加一个project.buildscript 块,但这似乎也没有必要,并且由于这个错误是有问题的:https://developer.android.com/studio/build/gradle-plugin-3-0-0.html?utm_source=android-studio#known_issues

我将com.android.tools.build:gradle 依赖添加到buildSrc/build.gradle 作为runtime 依赖。看起来应该可行:我认为这告诉 Gradle,为了运行我的插件,该库(及其依赖项)需要位于类路径中。但是,gradle buildEnvironment(以及我们的构建失败的事实)清楚地表明情况并非如此。

所以,问题:

  1. buildSrc/build.gradle 中指定的 runtime 依赖项与常规 build.gradle 中 buildscript 块中指定的 classpath 依赖项有什么区别?
  2. 我该如何安排,以便用户可以从buildSrc 应用插件,并且还必须将buildscript 块添加到他们的build.gradle

【问题讨论】:

    标签: gradle android-gradle-plugin


    【解决方案1】:

    我遇到了一个稍微不同的问题,并找到了一个可接受的解决方案,可能有助于解决您的第二个问题:我想在 buildSrc/build.gradle 中应用相同的存储库,并在根 build.gradle 中应用两次。

    项目根目录中的repositories.gradle

    repositories {
        if (project.hasProperty('nexus')) {
            maven {
                url 'http://localhost:8081/repository/JCenter/'
            }
            maven {
                url 'http://localhost:8081/repository/Maven_Google/'
            }
        } else {
            jcenter()
            google()
        }
    }
    
    ext {
        androidGradleBuildToolsDependency = 'com.android.tools.build:gradle:3.1.3'
    }
    

    buildSrc/build.gradle:

    buildscript {
        apply from: '../repositories.gradle'
    }
    
    allprojects {
        apply from: '../repositories.gradle'
    }
    
    dependencies {
        // androidGradleBuildToolsDependency is defined in repositories.gradle
        implementation androidGradleBuildToolsDependency
    }
    

    build.gradle:

    buildscript {
        apply from: 'repositories.gradle'
    }
    
    allprojects {
        // this line will also be executed from the build.gradles in subprojects, so the working
        // directory isn't always the same, so we use the absolute path here
        apply from: "${rootProject.projectDir}/repositories.gradle"
    }
    

    请注意,您不需要像往常一样在根 build.gradlebuildscript 块中使用 classpath 依赖项。 repositories.gradle 中的 implementation 依赖项似乎会自动应用它。

    build.gradles 通过依赖项提供时,我的解决方案可能不起作用。

    【讨论】:

      【解决方案2】:

      在研究一个切线问题时偶然发现了这一点。

      我已经成功地使用buildSrc/build.gradle 作为定义依赖项的地方,这些依赖项通常属于我的一些项目的根项目的 buildscript 类路径。

      您可以在此处查看一个工作示例:https://github.com/episode6/chop/blob/develop/buildSrc/build.gradle

      我曾经使用compiledependencie,但刚刚切换到runtimeClasspath,感觉更合适并且也有效。我认为您的 classpath 依赖项无法正常工作,因为它们将位于 buildSrc 项目的类路径中,但不会编译到该项目中或与其一起运行。

      如果你决定走这条路,你可能会遇到我刚刚研究的问题,因为这种方法才出现。

      当我使用 dokka 插件尝试这种方法时,出现以下错误

      Could not resolve all files for configuration ':detachedConfiguration1'.
         > Cannot resolve external dependency org.jetbrains.dokka:dokka-fatjar:0.9.17 because no repositories are defined
      

      我可以通过将 jcenter() 添加到根项目的 buildscript 存储库来解决此问题:https://github.com/episode6/chop/blob/develop/build.gradle#L2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-05
        • 2022-06-21
        • 2013-07-20
        • 1970-01-01
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2018-01-26
        相关资源
        最近更新 更多