2013-11-14 90 views
0

屬性在我的劇本的gradle我已經能夠成功地讀取性能是這樣的:init.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() 
    } 
} 

回答

1

-P設置一個項目財產,這是不是在一個初始化腳本立即可用。 (您可以從初始化腳本訪問項目及其屬性,但該項訪問將延遲至項目創建完成。)但是,使用系統屬性(-D)應該可以工作。

+0

感謝您的快速回答! (抱歉我的延遲響應) – Sababado