2010-02-17 81 views
6

使用MSBuild,只要發生錯誤,項目的執行就會停止,除非ContinueOnError=true如何在不引發錯誤的情況下停止MSBuild的執行?

有沒有辦法停止項目的執行而不會產生錯誤?

我想有這種可能性,因爲我有一套現有的msbuild項目文件,在某些情況下,我需要停止處理項目而不會引發錯誤,因爲它是該過程的正常退出點,我不希望使用腳本的人認爲有什麼錯誤。

我知道我可以設置一些屬性,並將所有剩餘的任務作爲條件,但我想避免這種情況。

+0

你是什麼意思「*正常退出點*」。如果沒有完成目標,那麼這怎麼可能是正常的?你能否更詳細地解釋你正在努力完成什麼,以便我們能夠理解你到底需要什麼? – 2010-02-19 06:03:50

回答

7

正如你所解釋的那樣,你想在特殊情況下停止構建,而不會因爲這是一個正常的退出點而引發錯誤。爲什麼不創建一個無所事事的目標,作爲你的退出點。在你的特殊條件下,你會稱這個目標。

<target Name="BuildProcess"> 
    <Message Text="Build starts"/> 
    ... 
    <CallTarget Targets="Exit" 
       Condition="Special Condition"/> 

    <CallTarget Targets="Continue" 
       Condition="!(Special Condition)"/> 
    ...  
</target> 

<target Name="Continue"> 
    <Message Text="Build continue"/> 
</target> 

<target Name="Exit"> 
    <!-- This target could be removed --> 
    <!-- Only used for logging here --> 
    <Message Text="Build ended because special condition occured"/> 
</target> 
+1

這真的很漂亮,但不適用於我使用msbuild 4.0。執行返回到使用CallTarget的目標並繼續(因此「生成繼續」也被打印在屏幕上)。有沒有可以建議的解決方法? – charisk 2012-04-30 09:57:36

+0

我已經更新了我的答案,現在應該會更好。 – 2012-04-30 10:04:02

+0

謝謝。但是現在不需要退出目標。如果沒有滿足特殊條件,我們跳轉到繼續目標,否則我們將停留在BuildProcess(它應該在那裏結束)。 – charisk 2012-04-30 10:18:02

2

要做到這一點的方法是創建另一個目標來包裝你感興趣的目標調節。

所以,如果你有一個場景,像這樣的目標:

<Target Name="MainTarget"> 
command - run under a certain condition 
command - run under a certain condition 
command - run under a certain condition 
command - run under a certain condition 
command - run under a certain condition 
</Target> 

的一點是,要保存不必使用條件語句一大堆的時間,對不對?

爲了解決這個問題,你可以這樣做:

<Target Name="MainWrapper" DependsOnTargets="EstablishCondition;MainTarget" /> 

<Target Name="EstablishCondition"> 
<SomeCustomTask Input="blah"> 
<Output PropertyName="TestProperty" TaskParameter="value" /> 
</SomeCustomTask> 
</Target> 

<Target Name="MainTarget" Condition="$(TestProperty)='true'"> 

command 
command 
command 
command 
command 

</Target> 
0

最終發現了類似的問題,一個完美的解決方案。我只需要將我的問題從「中斷/中斷MSBuild執行」更改爲「跳過下一個目標」。

<PropertyGroup> 
<LastInfoFileName>LastInfo.xml</LastInfoFileName> 
<NewInfoFileName>NewInfo.xml</NewInfoFileName> 
</PropertyGroup> 

<Target Name="CheckSomethingFirst" BeforeTargets="DoSomething"> 

<Message Condition="ConditionForContinue" 
      Text="Let's carry on with next target" /> 
<WriteLinesToFile Condition="ConditionForContinue" 
        File="$(NewInfoFileName)" 
        Lines="@(SomeText)" 
        Overwrite="true" /> 

<Message Condition="!ConditionForContinue" 
      Text="Let's discard next target" /> 
<Copy Condition="!ConditionForContinue" 
     SourceFiles="$(LastInfoFileName)" 
     DestinationFiles="$(NewInfoFileName)" /> 

</Target> 

<Target Name="DoSomething" Inputs="$(NewInfoFileName)" 
          Outputs="$(LastInfoFileName)"> 
<Message Text="DoSomethingMore" /> 
<Copy SourceFiles="$(NewInfoFileName)" 
     DestinationFiles="$(LastInfoFileName)" /> 
</Target> 

此工程確定與像一個命令:

msbuild.exe Do.targets /t:DoSomething 

其中目標DoSomething的輸入/輸出被正確地執行該CheckSomethingFirst目標後檢查。

相關問題