2016-08-03 98 views
1

我有以下插件運行.sh腳本:EXEC Maven插件:退出代碼

<plugin> 
    <artifactId>exec-maven-plugin</artifactId> 
    <groupId>org.codehaus.mojo</groupId> 
    <executions> 
    <execution> 
     <id>deploy-bundles</id> 
     <phase>install</phase> 
     <goals> 
     <goal>exec</goal> 
     </goals> 
     <configuration> 
     <executable>${basedir}/deploy.sh</executable> 
     <successCodes> 
      <successCode>0</successCode> <-- Not working 
     </successCodes> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

其拷貝一些文件夾和文件到一定的位置。有用。不過,爲了以防萬一,我希望有一個錯誤失敗機制。我在.sh腳本中已經有set -e命令,但我也想要一個maven解決方案。我聽說有一個叫successCodestag,我試着把它合併。但到目前爲止沒有運氣。有人能指出正確的做法嗎?

編輯:.sh腳本是這樣的:

cp ../template/config.properties $component/conf 
cp ../../runtime/group1/group1.mw/conf/log4j.xml $component/conf 
# if the component is planning, create an additional folder called plans 
if [[ $component == *".planning"* ]] 
then 
mkdir -p $component/plans 
    # copy all the plans here 
    cp ../../mission.planning/plans/* $component/plans 
fi 

情況,預計萬一失敗,這些文件夾/文件不存在。所以,作爲一個測試,我手動更改上面的路徑,並期望它失敗。它執行失敗並告訴我錯誤(因爲我在.sh腳本中有set -e命令),但maven報告是「成功」的。

回答

2

這不是Exec Maven插件的問題,而是Shell腳本中處理退出代碼的問題。

退出代碼被解析爲成功執行對不符合規定的應用程序(應用程序不返回:

successCodes參數情況下可執行的具有比0爲「成功執行」不同的退出代碼是有用0表示成功)。

The default behaviour是考慮退出代碼0作爲一個成功的執行,並作爲失敗的所有其他人,該插件會在這種情況下構建失敗。

在您的Shell腳本中,您有多個命令,each of which has its own exit code。作爲一個整體,腳本本身的退出代碼沒有任何額外的處理,就是最後一個命令的退出代碼。因此,即使其中一個命令失敗(因此其退出代碼不爲零)之後的成功命令也會將腳本退出代碼重置爲0.您可以通過在Maven之外的命令行上調用腳本來測試該腳本,並且可以使用echo $?變量,which contains the exit code

因此,您需要測試可能在您的Shell中失敗的每個調用命令的退出代碼。 (您也可以使用a bit of arithmetic累積每個退出代碼。)

+0

完美答案,謝謝。 –