Gradle global variable not in scope within buildscript

寵の児 提交于 2019-12-01 14:26:23

问题


I have something like this in my top level build.gradle (Gradle 2.2)

ext.repo = "https://my-artifactory-repo"

buildscript {
    repositories {
        maven {
            credentials {
                username foo
                password bar
            }
            url repo //doesn't work
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.1'

    }
}

allprojects {
    repositories {
        maven {
            credentials {
                username foo
                password bar
            }
            url repo //works
        }
    }
}

This is the error

Could not find property 'repo' on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@718afa64.

So it works in allprojects but not buildscript.


回答1:


This is happening because the buildscript {...} configuration closure is always evaluated first, so the property is not yet defined. A workaround would be to define the property outside of the build script, either by placing it in a gradle.properties file or via the command line.




回答2:


You can define your variable as an extra property with ext in the buildscript {...}. This variable is then also accessible in the scope of allprojects {...}:

buildscript {
    ext {
        repo = "https://my-artifactory-repo"
    }
    repositories {
        maven {
            url repo // works
        }
    }
}

allprojects {
    repositories {
        maven {
            url repo // works
        }
    }
}


来源:https://stackoverflow.com/questions/27031587/gradle-global-variable-not-in-scope-within-buildscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!