How to make only one target BuildInParallel? - msbuild

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.

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 retrieve #(TargetOutputs) without performing a build

I'm implementing an MSBuild framework to drive the building and deployment of many projects organized as a hierarchy.
<Target Name="_CoreBuild">
<MSBuild Projects="#(Project)" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="CompiledAssemblies" />
</MSBuild>
</Target>
In order to implement proper Clean/Clobber logic, I would like to retrieve the list of files that would be compiled if a build were performed with the current options.
<Target Name="_CoreClobber" DependsOnTargets="_CoreClean">
<!-- How to retrieve #(CompiledAssemblies) as if we were
building #(Project) and retrieving the #(TargetOutputs) item group.
-->
</Target>
I've tried various methods, including creating a custom task, in which I build a custom project file that imports the original project I want to retrieve the properties/items from. But that does not give me reliable values.
Is there a way to retrieve an MSBuild project's TargetOutputs item group without actually performing a build?
Never mind.
I stumbled upon the following similar question, and figured I had to use the GetTargetPath target, like so:
<Target Name="_CoreBuild">
<MSBuild Projects="#(Project)" Targets="GetTargetPath" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="CompiledAssemblies" />
</MSBuild>
</Target>

Creating MSBuild target hooks

Can someone please point me to a reference about target hooks in MSBuild?
I'm looking for something that will let me define targets to run before and after a specified target. I know this can be done using the DependsOnTargets property but I've seen references to using target hooks and I'd like to explore that area.
Thanks,
Zain
A good list of built-in overridable build process hooks can be found here. For custom targets, the only thing I can think of is to use either the DependsOnTarget attribute (like you mentioned) or the BeforeTargets/AfterTargets attribute (like #Ritch Melton mentioned.) Be careful, the BeforeTargets/AfterTargets are only available in MSBuild 4.0
If you understand the idea behind DependsOnTargets then open up the Microsoft.Common.targets file in the .Net SDK directory (C:\Windows\Microsoft.NET\Framework\v3.5). That file defines the build process for the MSBuild task and .Net projects created by Visual Studio. Look for tags called BeforeXXXX, and AfterXXXX. BeforeBuild and AfterBuild are referenced in the default.csproj file - Snippet:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
There are others, like Clean, Rebuild, etc..
Define a Target (or Targets) to execute inside those Target elements, like this (Creates a directory, or list of directories based on the value in the Directories Property:
<Target Name="CreateDir">
<MakeDir Directories="D:\Dogs.txt"/>
</Target>
Then include those Targets in the BeforeXXX Target:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild" BeforeTargets="CreateDir">
</Target>
</Project>

Determining outputs of a ProjectReference in MSBuild without triggering redundant rebuilds

As part of a solution containing many projects, I have a project that references (via a <ProjectReference> three other projects in the solution, plus some others). In the AfterBuild, I need to copy the outputs of 3 specific dependent projects to another location.
Via various SO answers, etc. the way I settled on to accomplish that was:
<MSBuild
Projects="#(ProjectReference)"
Targets="Build"
BuildInParallel="true"
Condition="'%(Name)'=='ProjectA' OR '%(Name)'=='ProjectB' OR '%(Name)'=='ProjectC'">
<Output TaskParameter="TargetOutputs" ItemName="DependentAssemblies" />
</MSBuild>
<Copy SourceFiles="#(DependentAssemblies)" DestinationFolder="XX" SkipUnchangedFiles="true" />
However, I ran into problems with this. The <MSBuild step's IncrementalClean task ends up deleting a number of the outputs of ProjectC. When running this under VS2008, a build.force file being deposited in the obj/Debug folder of ProjectC which then triggers ProjectC getting rebuilt if I do a Build on the entire solution if the project containing this AfterBuild target, whereas if one excludes this project from the build, it [correctly] doesn't trigger a rebuild of ProjectC (and critically a rebuild of all dependents of ProjectC). This may be VS-specific trickery in this case which would not occur in the context of a TeamBuild or other commandline MSBuild invocation (but the most common usage will be via VS so I need to resolve this either way)
The dependent projects (and the rest of the solution in general) have all been created interactively with VS, and hence the ProjectRefences contain relative paths etc. I've seen mention of this being likely to causing issues - but without a full explanation of why, or when it'll be fixed or how to work around it. In other words, I'm not really interested in e.g. converting the ProjectReference paths to absolute paths by hand-editing the .csproj.
While it's entirely possible I'm doing something stupid and someone will immediately point out what it is (which would be great), be assured I've spent lots of time poring over /v:diag outputs etc. (although I havent tried to build a repro from the ground up - this is in the context of a relatively complex overall build)
As noted in my comment, calling GetTargetPath on the referenced project only returns the Primary output assembly of that project. To get all the referenced copy-local assemblies of the referenced project it's a bit messier.
Add the following to each project that you are referencing that you want to get the CopyLocals of:
<Target
Name="ComputeCopyLocalAssemblies"
DependsOnTargets="ResolveProjectReferences;ResolveAssemblyReferences"
Returns="#(ReferenceCopyLocalPaths)" />
My particular situation is that I needed to recreate the Pipeline folder structure for System.AddIn in the bin folder of my top level Host project. This is kinda messy and I was not happy with the MSDN suggested solutions of mucking with the OutputPath - as that breaks on our build server and prevents creating the folder structure in a different project (eg a SystemTest)
So along with adding the above target (using a .targets import), I added the following to a .targets file imported by each "host" that needs the pipeline folder created:
<Target
Name="ComputePipelineAssemblies"
BeforeTargets="_CopyFilesMarkedCopyLocal"
Outputs="%(ProjectReference.Identity)">
<ItemGroup>
<_PrimaryAssembly Remove="#(_PrimaryAssembly)" />
<_DependentAssemblies Remove="#(_DependentAssemblies)" />
</ItemGroup>
<!--The Primary Output of the Pipeline project-->
<MSBuild Projects="%(ProjectReference.Identity)"
Targets="GetTargetPath"
Properties="Configuration=$(Configuration)"
Condition=" '%(ProjectReference.PipelineFolder)' != '' ">
<Output TaskParameter="TargetOutputs"
ItemName="_PrimaryAssembly" />
</MSBuild>
<!--Output of any Referenced Projects-->
<MSBuild Projects="%(ProjectReference.Identity)"
Targets="ComputeCopyLocalAssemblies"
Properties="Configuration=$(Configuration)"
Condition=" '%(ProjectReference.PipelineFolder)' != '' ">
<Output TaskParameter="TargetOutputs"
ItemName="_DependentAssemblies" />
</MSBuild>
<ItemGroup>
<ReferenceCopyLocalPaths Include="#(_PrimaryAssembly)"
Condition=" '%(ProjectReference.PipelineFolder)' != '' ">
<DestinationSubDirectory>%(ProjectReference.PipelineFolder)</DestinationSubDirectory>
</ReferenceCopyLocalPaths>
<ReferenceCopyLocalPaths Include="#(_DependentAssemblies)"
Condition=" '%(ProjectReference.PipelineFolder)' != '' ">
<DestinationSubDirectory>%(ProjectReference.PipelineFolder)</DestinationSubDirectory>
</ReferenceCopyLocalPaths>
</ItemGroup>
</Target>
I also needed to add the required PipelineFolder meta data to the actual project references. For example:
<ProjectReference Include="..\Dogs.Pipeline.AddInSideAdapter\Dogs.Pipeline.AddInSideAdapter.csproj">
<Project>{FFCD0BFC-5A7B-4E13-9E1B-8D01E86975EA}</Project>
<Name>Dogs.Pipeline.AddInSideAdapter</Name>
<Private>False</Private>
<PipelineFolder>Pipeline\AddInSideAdapter\</PipelineFolder>
</ProjectReference>
Your original solution should work simply by changing
Targets="Build"
to
Targets="GetTargetPath"
The GetTargetPath target simply returns the TargetPath property and doesn't require building.
You may protect your files in ProjectC if you call a target like this first:
<Target Name="ProtectFiles">
<ReadLinesFromFile File="obj\ProjectC.csproj.FileListAbsolute.txt">
<Output TaskParameter="Lines" ItemName="_FileList"/>
</ReadLinesFromFile>
<CreateItem Include="#(_DllFileList)" Exclude="File1.sample; File2.sample">
<Output TaskParameter="Include" ItemName="_FileListWitoutProtectedFiles"/>
</CreateItem>
<WriteLinesToFile
File="obj\ProjectC.csproj.FileListAbsolute.txt"
Lines="#(_FileListWitoutProtectedFiles)"
Overwrite="true"/>
</Target>
My current workaround is based on this SO question, i.e, I have:
<ItemGroup>
<DependentAssemblies Include="
..\ProjectA\bin\$(Configuration)\ProjectA.dll;
..\ProjectB\bin\$(Configuration)\ProjectB.dll;
..\ProjectC\bin\$(Configuration)\ProjectC.dll">
</DependentAssemblies>
</ItemGroup>
This however will break under TeamBuild (where all the outputs end up in one directory), and also if the names of any of the outputs of the dependent projects change.
EDIT: Also looking for any comments on whether there's a cleaner answer for how to make the hardcoding slightly cleaner than:
<PropertyGroup>
<_TeamBuildingToSingleOutDir Condition="'$(TeamBuildOutDir)'!='' AND '$(CustomizableOutDir)'!='true'">true</_TeamBuildingToSingleOutDir>
</PropertyGroup>
and:
<ItemGroup>
<DependentAssemblies
Condition="'$(_TeamBuildingToSingleOutDir)'!='true'"
Include="
..\ProjectA\bin\$(Configuration)\ProjectA.dll;
..\ProjectB\bin\$(Configuration)\ProjectB.dll;
..\ProjectC\bin\$(Configuration)\ProjectC.dll">
</DependentAssemblies>
<DependentAssemblies
Condition="'$(_TeamBuildingToSingleOutDir)'=='true'"
Include="
$(OutDir)\ProjectA.dll;
$(OutDir)\ProjectB.dll;
$(OutDir)\ProjectC.dll">
</DependentAssemblies>
</ItemGroup>

Checking if project has a specific target in MSBuild

Some of my .csproj project files have special target "AssembleJS" that merges all .js files included in project in one big file (i.e. webcontrols.csproj has target "AssembleJS" with output "webcontrols.js").
So if I have project parts.csproj
That has target AssembleJS.
References project webcontrols.csproj.
References utility project utils.csproj that does not have any JavaScript and does not have AssembleJS target.
I want target AssembleJS of parts.csproj execute AssembleJS in webcontrols.csproj (the same was as MSBuild works with standard Build target).
Something like
<MSBuild Project="#ReferencedProjects" Targets="AssembleJS"/>
does not work because utils.csproj does not have target AssembleJS.
Is there any way to filter #ReferencedProjects based on whether project has certain target?
Any other idea on how to handle this scenario?
You cannot do what you are requiring. But you might be able to acheive it with batching.
<Project xmlns=''>
<ItemGroup>
<ReferencedProjects Include="webcontrols.csproj">
<Type>Web</Type>
</ReferencedProjects>
<ReferencedProjects Include="utils.csproj">
<Type>NonWeb</Type>
</ReferencedProjects>
</ItemGroup>
<Target Name="BuildWebProjects">
<MSBuild Projects="#(ReferencedProjects)" Condition=" '%(ReferencedProjects.Type)' == 'Web' " />
</Target>
</Project>
Do a search for MSBuild Batching and find some results on sedodream.com for more
info.
Should I expand on this?