在其他gradle文件中按名称获取Gradle构建脚本存储库

时间:2017-11-20 07:02:57

标签: gradle build.gradle build-script

我的gradle项目结构如下:

project
│   gradle.properties
│   build.gradle    
│
└───common-config
    │   common-buildscript.gradle
    │   common-java.gradle

文件内容:

gradle.properties

artifactory_user=username
artifactory_password=password

共buildscript.gradle

def properties = new Properties()
file("../gradle.properties").withInputStream { properties.load(it) }

repositories {
    maven {
        url "someRepo1.com"
        credentials {
            username = properties.get("artifactory_user")
            password = properties.get("artifactory_password")
        }
        name = "Some Repo 1"
    }
    maven {
        url "someRepo2.com"
        credentials {
            username = properties.get("artifactory_user")
            password = properties.get("artifactory_password")
        }
        name = "Some Repo 2"
    }
}

共java.gradle

apply plugin: "java"

repositories {
    add buildscript.repositories.getByName("Some Repo 1")
    add buildscript.repositories.getByName("Some Repo 2")
}

运行 gradle clean build 后,我收到了以下错误:

A problem occurred evaluating script.
> Repository with name 'Some Repo 1' not found.

似乎在 common-java.gradle 中,gradle无法检测到"某些回购1" common-buildscript.gradle 中的名称。但是,如果我将两个文件的内容放在相同的 build.gradle

中,这将有效。

我想这样做的原因是我想将公共构建脚本应用于我的存储库中的许多其他构建脚本。

请建议我如何解决这个问题。谢谢

更新

build.gradle 内容:

buildscript {
    ext {
        commonConfig = rootProject.projectDir.absolutePath
    }
    apply from: "${commonConfig}/common-config/common-buildscript.gradle", to: buildscript
}

apply from: "${commonConfig}/common-config/common-java.gradle"

0 个答案:

没有答案