1

在我的 gradle 脚本中,我已经能够成功读取如下属性:

def environment = hasProperty('env') ? env : 'dev'

使用它我可以执行这样的构建脚本:

gradlew clean assemble -Penv=prod

当我尝试将其移动到init.gradle文件时,我的问题出现了。该文件已被识别,并且我可以使用我在脚本中定义的其他属性,但是我无法从命令行获取任何属性。我怎样才能做到这一点?

我的init.gradle文件:

allprojects {
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.6.+'
        }
    }
    project.ext['nexusDomainName'] = 'https://example.com/nexus'
    project.ext['compileSdkVersion'] = 19
    project.ext['buildToolsVersion'] = "19"
    project.ext['minSdkVersion'] = 8
    project.ext['targetSdkVersion'] = 19

    project.ext['sourceCompatibility'] = '1.7'
    project.ext['targetCompatibility'] = '1.7'

    //hasProperty('release') is always false
    project.ext['archiveType'] = hasProperty('release') ? '' : '-SNAPSHOT'
    project.ext['archiveUrl'] = hasProperty('release') ? "$nexusDomainName/content/repositories/releases/" : "$nexusDomainName/content/repositories/snapshots/"


    // This buildEnv property won't read either
    project.ext['buildEnv'] = hasProperty('env') ? env : 'dev'
    println "prepping for $buildEnv"
    project.ext['archivesBaseNameSuffix'] = (project.ext['buildEnv'] == 'stage' || project.ext['buildEnv'] == 'dev') ? '-' + project.ext['buildEnv'] : ''

    repositories {
        mavenLocal()
        maven {
            credentials {
                username 'username'
                password 'password'
            }
            url "$nexusDomainName/content/groups/public/"
        }
        mavenCentral()
    }
}
4

1 回答 1

1

-P设置项目属性,该属性在初始化脚本中不会立即可用。(您可以从 init 脚本访问项目及其属性,但该访问将推迟到项目创建完成。)但是,使用系统属性 ( -D) 应该可以工作。

于 2013-11-14T17:52:49.040 回答