2010-02-17 78 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目标后检查。

相关问题