Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stop MSBuild execution without raising an error?

Tags:

msbuild

With MSBuild, as soon as an error occurs, the execution of the project is stopped unless ContinueOnError=true.

Is there a way to stop the execution of the project without raising an error?

I'd like to have this possibility because I have an existing set of msbuild project files and in some circumstances, I would need to stop processing the projects without raising an error because it is a normal exit point for the process and I don't want the person using the script to think something is wrong.

I know I could just set some property and put all remaining tasks conditional on this but I would like to avoid that.

like image 706
Marcel Gosselin Avatar asked Feb 17 '10 21:02

Marcel Gosselin


3 Answers

As you explain it, you want to stop your build under special circumstance without raising an error because it is a normal exit point. Why not create a target doing nothing that will serve as your exit point. Under your special conditions you will call this target.

<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>
like image 120
Julien Hoarau Avatar answered Oct 22 '22 07:10

Julien Hoarau


The way to do this is the create another target to wrap the target you're interested in conditioning.

So if you have a scenario with a target like this:

<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>

The point is that you want to save having to use the condition statement a whole bunch of times, right?

To address this, you can do this:

<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>
like image 34
Brandon Hawbaker Avatar answered Oct 22 '22 06:10

Brandon Hawbaker


Eventually found an elegant solution for a similar issue. I just needed to rephrase my concern from "Break/interrupt MSBuild execution" to "Skip the next targets".

<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>

This works ok with a command like:

msbuild.exe Do.targets /t:DoSomething

where target DoSomething Inputs/Outputs are correctly checked after the CheckSomethingFirst target was executed.

like image 1
Vlad Avatar answered Oct 22 '22 07:10

Vlad