2012-01-09 77 views
8

我需要從Gradle執行依賴於環境變量的Ant腳本。 Ant爲它使用<property environment="env"/>從Gradle設置環境變量

我試圖在Gradle中做env.foo="bar",但它拋出了一個Groovy異常。

將環境變量從Gradle傳遞給Ant的正確方法是什麼?

+0

你嘗試通過使用Ant構建腳本'ant.importBuild'build.xml'? – 2012-01-10 00:46:18

+0

@Benjamin這就是我想要完成的。不幸的是,傳統的Ant腳本依賴於幾個環境變量。 – Sergey 2012-01-10 06:31:12

回答

4

這是不可能的設置從搖籃或JVM在一般的環境變量,但它有可能欺騙螞蟻這樣的:

ant.project.properties['env.foo'] = 'bar' 
+0

我收到一個錯誤:'找不到屬性'' – 2016-07-25 19:44:24

9

gradle 2.0 docs,我看到這樣的事情是可能的

test { 
    environment "LD_LIBRARY_PATH", "lib" 
} 

或在這種情況下,可以使用這個

systemProperty "java.library.path", "lib" 
+3

'我希望兩年前有Gradle 2.0 :) – Sergey 2014-08-06 11:57:38

+1

我在我的build.gradle文件(用另一個變量名)試過這個。然後我在我的測試中檢查系統變量,如下所示:'assertTrue(System.getenv()。containsKey(「SOME_TEST_VAR」));'這給出了斷言錯誤。所以,它看起來像環境變量沒有設置。 – user3791111 2016-03-31 03:11:01

+0

我收到一個錯誤'無法找到方法systemProperty()的參數 – 2016-07-25 19:43:39

1

Accep從@Sergey泰德的解決方案:

ant.project.properties['env.foo'] = 'bar' 

不爲我上gradle 2.9和1.9.7 ant工作。 這沒有引發任何錯誤,但什麼都不做。事實上,如果你看一下代碼它implemented as

public Hashtable<String, Object> getProperties() { 
    return PropertyHelper.getPropertyHelper(this).getProperties(); 
} 

其中org.apache.tools.ant.PropertyHelper#getProperties is

public Hashtable<String, Object> getProperties() { 
    //avoid concurrent modification: 
    synchronized (properties) { 
     return new Hashtable<String, Object>(properties); 
    } 
} 

所以作出明確的副本,它不能正常工作。

的方式正確做在gradle文件:

ant.project.setProperty('env.foo', 'bar') 

Documentation提及一些其他方法(注意,無項目):

ant.buildDir = buildDir 
ant.properties.buildDir = buildDir 
ant.properties['buildDir'] = buildDir 
ant.property(name: 'buildDir', location: buildDir)