I have a MSBuild project where I need to perform different prepare operations ,build some solutions from a list ,perform some final operations. If a solution fails –send an email (write a message)about this . If at least one solution failed –the whole process is considered as failed and I send an email (write a message) about this. In order to achieve this I use target batching on SolutionsToBuild item group . I check the result of each compilation and store it in CurrentSolutionBuildResult . Then my idea is to have a global property BuildSucceded with default value set to true. In case a solution fails inside the target BuildAllSolutions–I set this property to false so at the end of the target MainTarget I can check it and write the message with build failure or build success. Unfortunately at the end of the main target I get the message:
After building all the solutions -BuildSucceded is:'true' Build successful!
Although I expected to have this property set to false as it was done in each iteration for target BuildAllSolutions:
Build for C:\SampleSolution\SampleSolution3.sln failed! BuildSucceded in target BuildAllSolutions is:'false'
After searching on this site I found the reason: MSBuild Property Scope but still do not know how to solve this issue (marking the whole process as failed or successful) as I cannot use DependsOnTargets workaround (I want BuildAllSolutions target not to be executed at the beginning but after some other operations are performed). Below is the code ,maybe someone could advise me how to achieve what I need… Thanks
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="MainTarget">
<PropertyGroup>
<BuildSucceded>true</BuildSucceded>
</PropertyGroup>
<ItemGroup>
<SolutionsToBuild Include="C:\SampleSolution\SampleSolution1.sln" />
<SolutionsToBuild Include="C:\SampleSolution\SampleSolution2.sln" />
<SolutionsToBuild Include="C:\SampleSolution\SampleSolution3.sln" />
</ItemGroup>
<Target Name="BuildAllSolutions" Outputs="%(SolutionsToBuild.Identity)">
<Message Text="Building solution '%(SolutionsToBuild.Identity) " />
<MSBuild Projects="%(SolutionsToBuild.Identity) " Properties="Configuration=Debug" ContinueOnError="ErrorAndContinue"/>
<PropertyGroup>
<CurrentSolutionBuildResult>$(MSBuildLastTaskResult)</CurrentSolutionBuildResult>
<BuildSucceded Condition="'$(CurrentSolutionBuildResult)' == 'false' " >false</BuildSucceded>
</PropertyGroup>
<Message Text="Build for %(SolutionsToBuild.Identity) failed!" Condition="'$(CurrentSolutionBuildResult)' == 'false'"/>
<Message Text="Build for %(SolutionsToBuild.Identity) succeded!" Condition="'$(CurrentSolutionBuildResult)' != 'false'"/>
<Message Text=" BuildSucceded in target BuildAllSolutions is:'$(BuildSucceded)'"/>
</Target>
<Target Name="MainTarget" >
<Message Text="Different start operations in MainTarget" />
<Message Text=" Before building all the solutions -BuildSucceded is:'$(BuildSucceded)'"/>
<CallTarget Targets="BuildAllSolutions" ContinueOnError="ErrorAndContinue" />
<Message Text="Different end operations in MainTarget" />
<Message Text=" After building all the solutions -BuildSucceded is:'$(BuildSucceded)'"/>
<Message Text=" Build successful!" Condition=" '$(BuildSucceded)' != 'false' "/>
<Message Text=" Build failed!" Condition=" '$(BuildSucceded)' == 'false' "/>
</Target>
</Project>