MSBuild ContinueOnError - msbuild

I have an MSBuild project as follows:
<Target Name="StopApplications">
<BizTalk.BuildGenerator.Tasks.StopApplication MessageBoxConnection="$(BizTalkManagementDatabaseConnectionString)" ApplicationName="x.Direct.Brackmills"/>
<BizTalk.BuildGenerator.Tasks.StopApplication MessageBoxConnection="$(BizTalkManagementDatabaseConnectionString)" ApplicationName="x.Direct.Manhattan"/>
</Target>
<Target Name="RemoveApplications">
<Exec Command="BTSTask RemoveApp -ApplicationName:x.Direct.Brackmills -Server:$(BizTalkDatabaseServerName) -Database:$(BizTalkManagementDatabaseName)" />
<Exec Command="BTSTask RemoveApp -ApplicationName:x.Direct.Manhattan -Server:$(BizTalkDatabaseServerName) -Database:$(BizTalkManagementDatabaseName)" />
</Target>
My problem is that when calling the "RemoveApplications" target, the ContinueOnError does not work as I'd expect. I have a long list of applications to stop and remove. They won't all allways be present so I need the script to continue when it finds they're not there. This seems to work find for the "StopApplications" target but when it hits a missing application in the "RemoveApplications" target I get the message:
"Done building target "RemoveApplications" in project "cleardownApplications.proj" -- FAILED. Build continuing because "ContinueOnError" on the task "CallTarget" is set to "true".
But then, it drops out of "RemoveApplications" and moved onto "AddApplications"
Any help gratefully received,
Thanks
Rob.

I've solved this a bit differently and uses a separate target to check if the application exists before removing it.
<Target Name="ApplicationExists">
<BizTalk2006.Application.Exists Application="$(ApplicationName)">
<Output TaskParameter="DoesExist" PropertyName="ApplicationExists" />
</BizTalk2006.Application.Exists>
</Target>
The I use that exists-target as an "condition" in other targets.
<Target Name="DeleteApplication" Condition="$(ApplicationExists)=='True'" DependsOnTargets="ApplicationExists">
<BizTalk2006.Application.Stop Application="$(ApplicationName)"/>
<BizTalk2006.Application.Delete Application="$(ApplicationName)"/>
</Target>

Related

How can I have an msbuild target run only if new compilation actually occurred?

I am trying to have MSBuild generate a text file with a version number representing when the build was created.
Right now in my MSBuild task I have the following:
<Target Name="WriteBuildDate" AfterTargets="Compile">
<WriteLinesToFile File="buildversion.txt" Lines="$([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.Month).$([System.DateTime]::UtcNow.Day).$([System.Math]::Floor($([System.DateTime]::UtcNow.Subtract($([System.DateTime]::UtcNow.Date)).TotalSeconds)))" Overwrite="true" Encoding="Unicode" />
</Target>
This works, except that it executes even if the all assemblies were not rebuilt. This means if I use the VS publish capability for multiple servers each of them will have slightly different version numbers, even though the builds are all the same.
Is there any way (without custom msbuild tasks) to have this target only execute if at least one assembly was rebuilt?
Thanks to #stijn's comment/link, I was able to get this working via:
<PropertyGroup>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
<Target Name="WriteBuildDate" AfterTargets="CoreBuild" Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
<WriteLinesToFile File="buildversion.txt" Lines="$([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.Month).$([System.DateTime]::UtcNow.Day).$([System.Math]::Floor($([System.DateTime]::UtcNow.Subtract($([System.DateTime]::UtcNow.Date)).TotalSeconds)))" Overwrite="true" Encoding="Unicode" />
</Target>

How to make only one target BuildInParallel?

I am having a target which will build then checkin the assemblies.
<Target Name="CoreBuildCheckinSubSystem" DependsOnTargets="BuildDotNETSolutions;CheckinSubSystemDos">
</Target>
In this builddotnetSolution will build the list of solution in an order. So i do not want it to be buildinParallel but the CheckinSubsystemDos will checkin all the dlls. If the checkin process is being done in parallel it would save time for me.
How to make CheckinSubSystemDos alone in BuildInParallel?
An important thing to note about MSBuild, is that parallelization is done on the level of project. Targets within the same project are always executed sequentially, while two different project might (or might not) be executed in parallel. To rephrase it another way, if you want MSBuild to execute something concurrently, you have to create multiple projects and invoke them within the same <MSBuild> task.
In your case, the code would look like this. You have a list of projects or solutions to build:
<ItemGroup>
<MyProjects Include="One.proj"/>
<MyProjects Include="Two.proj"/>
<MyProjects Include="Three.proj"/>
</ItemGroup>
The build target would invoke <MSBuild> target sequentially:
<Target Name="BuildDotNETSolutions" ...>
<MSBuild Projects="#(MyProjects)" Targets="Build" BuildInParallel="false" />
</Target>
Your checkin target would invoke another target that is defined in the same projects -- call it MyCheckin:
<Target Name="CoreBuildCheckinSubSystem" DependsOnTargets="BuildDotNETSolutions;CheckinSubSystemDos">
<MSBuild Projects="#(MyProjects)" Targets="MyCheckin" BuildInParallel="true" />
</Target>
Another option for you is to create a sibling set of projects -- call it MyCheckinProjects and use them in you checkin target:
<ItemGroup>
<MyCheckinProjects Include="Checkin_One.proj"/>
<MyCheckinProjects Include="Checkin_Two.proj"/>
<MyCheckinProjects Include="Checkin_Three.proj"/>
</ItemGroup>
However I would suggest simply inserting a new target into existing projects.

MSBuild afterbuild step to copy code into a temp folder

I'm trying to place my compiled web application into a temporary directory after it has been built.
I have the following but it doesn't seem to work. It drops and create directors but the msbuild task doesn't seem to copy the compile output into the obj/publish directory that i need it to?
<Target Name="AfterBuild">
<CallTarget Targets="Publish" />
</Target>
<Target Name="Publish">
<RemoveDir Directories="$(SolutionDir)AsycLearn\obj\publish\" ContinueOnError="true" />
<MakeDir Directories="$(SolutionDir)AsycLearn\obj\publish\"/>
<MSBuild Projects="AsycLearn.csproj" Targets="ResolveReferences;_CopyWebApplication" Properties="WebProjectOutputDir=$(SolutionDir) AsycLearn\obj\publish\;OutDir=$(SolutionDir) AsycLearn\obj\publish\bin\" />
</Target>
any ideas? Thanks
It looks like you need to quote your output paths because they contain spaces. Otherwise the value that gets read will be truncated at the first space. That will also terminate the parameter set that gets passed to any other tasks.
Properties="WebProjectOutputDir=$(SolutionDir) AsycLearn\obj\publish\;OutDir=$(SolutionDir) AsycLearn\obj\publish\bin\"
should be
Properties="WebProjectOutputDir="$(SolutionDir) AsycLearn\obj\publish\";OutDir="$(SolutionDir) AsycLearn\obj\publish\bin\""
The escape sequence " is needed because we want the resolved value to include quotes.

Check a property on startup, depending on selected targets

I'm having some trouble with a msbuild file.
I'd like the build to have three targets, executed in this order:
Cleanup: Cleans the output of a previous build
Build: The actual build
CopyFiles: A task that packages the output of the build for an easier deployment
Now the problem is: I want a property (ReleasePath) for the CopyFiles target to be explicitly set by the caller. Also, the caller shouldn't have to set the output path if only calling the Cleanup and Build tasks.
My first try has been:
<PropertyGroup>
<ReleasePath></ReleasePath>
</PropertyGroup>
<Target Name="Initialize">
<Error Text="The ReleasePath property isn't defined" Condition="'$(ReleasePath)' == ''"/>
</Target>
<Target Name="CopyFiles" DependsOnTargets="Initialize">
</Target>
It works fine, but the Initialize target is executed right before CopyFiles, that is after Build. And the Buildtarget can take quite a long time to execute, so if something's wrong I'd like the user to be notified right away.
So basically, is there a way to run the Initialize target at the beginning, but only if CopyFiles is included in the targets?
I also thought of putting Initialize in the InitialTargets attribute of the project, then put a condition to execute this target only if CopyFiles is selected, but to my surprise I couldn't find a property containing the list of targets selected by the caller.
I think the easiest way to accomplish your goal would be to use a Property to toggle CopyFiles rather than the list of targets passed in. If you always add CopyFiles to the AfterBuild target but condition it on a property like "ShouldCopyFiles" then you could also conditionalize the DependsOnTargets of Build:
<PropertyGroup>
<ShouldCopyFiles Condition="'$(ShouldCopyFiles)'==''">false</ShouldCopyFiles>
<ReleasePath></ReleasePath>
</PropertyGroup>
<PropertyGroup>
<BuildDependsOn>
BeforeBuild;
CoreBuild;
AfterBuild;
CopyFiles;
</BuildDependsOn>
</PropertyGroup>
<PropertyGroup Condition="'$(ShouldCopyFiles)'=='true'">
<BeforeBuildDependsOn>
Initialize;
</BeforeBuildDependsOn>
</PropertyGroup>
<Target Name="Initialize" Condition="'$(ShouldCopyFiles)'=='true'">CheckStuff
</Target>
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)"/>
<Target Name="CoreBuild">YourBuildStuff</Target>
<Target Name="BeforeBuild" DependsOnTargets="$(BeforeBuildDependsOn)"/>
<Target Name="AfterBuild" />
<Target Name="CopyFiles" Condition="'$(ShouldCopyFiles)'=='true'">
CopyStuff
</Target>
At this point you could call MSBuild like this:
msbuild my.proj /targets:Clean;Build /p:ShouldCopyFiles=true
and that would toggle it on.

How do you stop MSBuild execution without raising an error?

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