2014-09-30 32 views
0

當某些資源未找到時,我想停止從我的程序構建gradle。如何在特定條件下停止Gradle或Maven構建

例如,我的程序搜索abc.properties。當前未找到時,將顯示所有可能的錯誤消息,其中屬性文件的值是必需的。相反,我只想顯示一行錯誤消息,如「文件abc.properties丟失」,然後停止構建而不顯示其他冗長的消息。我試過System.exit(),但沒有幫助。

try { 
    properties.load(PropertyFactory.class.classLoader.getResourceAsStream("abc.properties")); 
} catch(Exception e) { 
    System.out.println("Please put a valid properties file in the src/main/resources folder named abc.properties"); 
    System.exit(1);//something that will stop the build and prevent other error messages 
} 

回答

0

您可能會拋出一個異常,但這會導致包含整個堆棧跟蹤的詳細輸出。另一種方法是使用ant.fail構造。

例如:

if(1 != 2) { 
    ant.fail('1 is not equal to 2') 
} 

也有一個類似的question

2

在gradle這個你可以把這個在您的buildscript:

if(!file("src/main/resources/abc.properties").exists()){ 
    throw new GradleException("Please put a valid properties file in the src/main/resources folder named abc.properties") 
} 
+0

這是一個相當不錯的解決方案。 – Sean 2014-11-19 12:21:22

1

在搖籃另一種方法是聲明文件作爲一個依賴。 如果無法解決,構建將失敗。 這是一種解決方法,但它應該起作用。

+0

我將不勝感激這次倒票的解釋。謝謝。 – Sean 2014-10-02 10:15:09

+0

我不是downvote,但這不適合我。我試圖只爲一個buildType「發行版」使用依賴項,並且對我的文件使用「releaseCompile」依賴項,即使它缺失也不會收到錯誤。 – 2014-11-18 20:22:04

+0

你確定你的依賴關係實際上正在解決嗎? – Sean 2014-11-19 12:20:39