MSBuild incremental build error - msbuild

I am facing one problem in building and copying .NET solution output to a deployment folder.
What i want to do is. Build solution and put the output into C:\TempOutput
then copy the outfiles from C:\TempOutput to another deployment folder.
at 1st attemp it creates folder in C:\TempOutput and creates output dlls and exes in this folder
also it creates folder "Exec\Debug\Bin" but does not copy files from C:\TempOutput
logs says that
Target CopyBuildFiles:
Skipping target "CopyBuildFiles" because it has no outputs.
When i run the script again this time it copies the files from C:\TempOutput to "Exec\Debug\Bin"
Am i missing something? Why it is not detecting output at 1st attempt?
following is the msbuild script
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Deploy">
<PropertyGroup>
<ProjectName>Common Projects</ProjectName>
<SolutionFilePath>..\..\Solution\Solution.sln</SolutionFilePath>
<!--Build/Rebuild-->
<BuildType>Build</BuildType>
<!--Debug -> output : local-->
<!--Release -> output : local-->
<!--ProduDbg -> output : X:\Debug-->
<!--ProduRel -> output : X:\Release-->
<BuildMode>Debug</BuildMode>
<OutputPath>..\Exec\$(BuildMode)\Bin\</OutputPath>
<ExecPath>..\..\bin\$(BuildMode)</ExecPath>
<DestinitionFolder>$(OutputPath)</DestinitionFolder>
<SubDirPath>$(ExecPath)\**</SubDirPath>
<BuildFolder>C:\TempOutputs\</BuildFolder>
</PropertyGroup>
<ItemGroup>
<File Include="
$(BuildFolder)\*.*
"
Exclude="
$(BuildFolder)\*.vshost*
"
>
</File>
</ItemGroup>
<Target Name="PreBuild">
<MakeDir Directories="$(BuildFolder)" />
<MakeDir Directories="$(DestinitionFolder)" />
</Target>
<Target Name="Compile">
<MakeDir Directories="$(BuildFolder)" />
<!-- Build does build only-->
<Message Text="*******************************************************"/>
<Message Text="-->Building $(ProjectName)"/>
<Message Text="*******************************************************"/>
<Message Text="*******************************************************"/>
<Message Text="-->Building in [$(BuildMode) | $(BuildType)] mode"/>
<Message Text="*******************************************************"/>
<MSBuild Projects="$(SolutionFilePath)" Targets="$(BuildType)" Properties="Configuration=$(BuildMode);OutDir=$(BuildFolder)"/>
</Target>
<Target Name="CopyBuildFiles"
Inputs="#(File)"
Outputs=
"#(File->'$(DestinitionFolder)%(RecursiveDir)%(Filename)%(Extension)')">
<Copy SourceFiles="#(File)"
DestinationFiles="#(File->'$(DestinitionFolder)%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
<Target Name="Deploy">
<CallTarget Targets="PreBuild"/>
<CallTarget Targets="Compile"/>
<Message Text="*******************************************************"/>
<Message Text="#(File)"/>
<Message Text="$(DestinitionFolder)"/>
<Message Text="*******************************************************"/>
<CallTarget Targets="CopyBuildFiles"/>
</Target>
</Project>

Try changing this bit
<Target Name="CopyBuildFiles"
Inputs="#(File)"
Outputs="#(File->'$(DestinitionFolder)%(File.RecursiveDir)%(File.Filename)%(File.Extension)')">
<Copy SourceFiles="#(File)"
DestinationFiles="#(File->'$(DestinitionFolder)%(File.RecursiveDir)%(File.Filename)%(File.Extension)')"/>
</Target>

Related

Copying files using MSBuild in TeamCity

I've got the following xml file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="DeployPrototype" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<dotCover>..\..\..\plugins\dotCover\bin\dotCover.exe</dotCover>
</PropertyGroup>
<ItemGroup>
<SourceFiles Include="..\Prototype\Site\Site\Bin\TestServer\Default.html;..\Prototype\Site\Site\Bin\TestServer\Site.xap"/>
<DestinationFolder Include="C:\inetpub\wwwroot\ProjectName\Prototype"/>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="../ProjectName.Web.sln" Properties="Configuration=testserver" />
<Message Text="Building ProjectName solution" Importance="high" />
</Target>
<Target Name="TeamCity" DependsOnTargets="Build">
<Message Text="Before executing MSpec command" Importance="low" />
<Exec Command="..\packages\Machine.Specifications.0.4.10.0\tools\mspec-clr4.exe ..\Hosts\ProjectName.Hosts.Web.Specs\bin\ProjectName.Hosts.Web.Specs.dll --teamcity" />
<Message Text="Ran MSpec" Importance="low" />
<Exec Command="$(dotCover) c TestServerBuildAndRunTestsOnly_DotCover.xml" />
<Message Text="##teamcity[importData type='dotNetCoverage' tool='dotcover' path='build\coverage.xml']" Importance="high" />
</Target>
<Target Name="DeployPrototype" DependsOnTargets="TeamCity">
<Message Text="Before executing copy, source files are: #(MySourceFiles) and destination folder is: #(DestinationFolder)" Importance="low" />
<Copy
SourceFiles="#(MySourceFiles)"
DestinationFolder="#(DestinationFolder)"
/>
<Message Text="Atter executing copy" Importance="low" />
</Target>
</Project>
Everything in this script works apart from the copying of the files. The messages I've put put in the copying section don't appear in the full log in TeamCity. In the configuration settings of the latter, I've put "DeployPrototype" as my target.
Why is the copying operation not happening?
For a given problem involving MSBuild not working under TeamCity, the answer almost always involves adding /v:d (Every step carried out and information about skipped steps) or /v:diag (Detailed plus dumps of ItemGroups etc. for diagnostic purposes) to the MSBuild args and the TeamCity-managed build output will have the answer.

Make ItemGroup created in MSBuild target available to calling targets

If I have the following targets in an MSBuild file:
<Target Name="Temp">
<CallTarget Targets="CreateTestList" />
<Message Text="TestList: -- #(TestAssembly) -- " />
<Message Text="Testing "%(TestAssembly.Identity)"" />
</Target>
<Target Name="CreateTestList">
<CreateItem Include="**\bin\$(Configuration)\*Tests.dll">
<Output TaskParameter="Include" ItemName="TestAssembly" />
</CreateItem>
<Message Text="TestList: -- #(TestAssembly) -- " />
<Message Text="Testing "%(TestAssembly.Identity)"" />
</Target>
How do I make the Message statements in my Temp target print out the items that the CreateTestList target put into the #(TestAssemblyList) ItemGroup?
Two things to note. First, the CreateItem task is essentially obsolete. Make it more readable by just declaring an ItemGroup inside your target. Second, due to how MSBuild publishes items, you need to make the CreateTestList target run as a dependency, not with CallTarget, which in most cases has limited usefulness. So,
<Target Name="Temp" DependsOnTargets="CreateTestList">
<Message
...
</Target>
<Target Name="CreateTestList">
<ItemGroup>
<TestAssembly Include="**\bin\$(Configuration)\*Tests.dll">
</ItemGroup>
<Message
...
</Target>

MsBuild Copy output and remove part of path

I have an MsBuild project which builds various solutions and then copies the output of Web Deployment Projects into a destination folder with two sub folder as follows:
The WDP output folders are copied over from the BuildFolder "Release".
DestFolder/PresentationTier/MyProject.xxx0Services_deploy/**Release**/Files...
DestFolder/MidTier/MyProject.xx1UI_deploy/**Release**/Files...
This works but I want to remove the $(Configuration) value from the output.
So the desired output folder layout is to be:
DestFolder/PresentationTier/MyProject.xxx0Services_deploy/Files...
DestFolder/MidTier/MyProject.xx1UI_deploy/Files...
Note the removal of "Release" folder
My code is below.
How can I change this to give the desired out please:
Code extract is as follows
<Target Name="CopyMidTierBuildOutput" DependsOnTargets="CopyPresentationTierBuildOutput" >
<Message Text="Copying midTier Build Output=================" />
<CreateItem Include="$(DeploymentRoot)**/MyProject.xxx0Services_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/MyProject.xxx1Services.Host_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/MyProject.xxx2.Host.IIS.csproj_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/MyProject.xxx3Services_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx4_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx5Services.Host_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx6Services.Host_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx7Service.Host.IIS_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx8Services.Host_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx9Service.Host.IIS.csproj_deploy/$(Configuration)/**/*.*;
$(DeploymentRoot)**/Nad.xxx10Services.Host_deploy/$(Configuration)/**/*.*">
<Output TaskParameter="Include" ItemName="MidTierDeploys"/>
</CreateItem>
<Copy
SourceFiles="#(MidTierDeploys)"
DestinationFolder="$(DestFolder)/MidTier/%(RecursiveDir)" ContinueOnError="false" />
You can implement expected behaviour with biltin features of MSBuild 4:
<ItemGroup>
<DeploymentProjects Include="1_deploy" />
<DeploymentProjects Include="2_deploy" />
</ItemGroup>
<Target Name="CopyMidTierBuildOutput" >
<Message Text="Copying midTier Build Output" Importance="High"/>
<ItemGroup>
<MidTierDeploys Include="$(DeploymentRoot)**\%(DeploymentProjects.Identity)\$(Configuration)\**\*.*">
<DeploymentProject>%(DeploymentProjects.Identity)</DeploymentProject>
</MidTierDeploys>
</ItemGroup>
<Msbuild Targets="CopyDeploymentItem"
Projects="$(MSBuildProjectFile)"
Properties="ItemFullPath=%(MidTierDeploys.FullPath);ItemRecursiveDir=%(MidTierDeploys.RecursiveDir);ItemDeploymentProject=%(MidTierDeploys.DeploymentProject);Configuration=$(Configuration);DestFolder=$(DestFolder)" />
</Target>
<Target Name="CopyDeploymentItem" >
<PropertyGroup>
<ItemExcludePath>$(ItemDeploymentProject)\$(Configuration)</ItemExcludePath>
<ItemDestRecursiveDirIndex>$(ItemRecursiveDir.IndexOf($(ItemExcludePath))) </ItemDestRecursiveDirIndex>
<ItemExcludePathLength>$(ItemExcludePath.Length)</ItemExcludePathLength>
<ItemSkippingCount>$([MSBuild]::Add($(ItemDestRecursiveDirIndex), $(ItemExcludePathLength)))</ItemSkippingCount>
<ItemDestRecursiveDir>$(ItemRecursiveDir.Substring($(ItemSkippingCount)))</ItemDestRecursiveDir>
</PropertyGroup>
<Copy
SourceFiles="$(ItemFullPath)"
DestinationFolder="$(DestFolder)/MidTier/$(ItemDeploymentProject)/$(ItemDestRecursiveDir)" ContinueOnError="false" />
</Target>
See Property functions for more info.

MSBuild: Add additional files to compile without altering the project file

After looking around I can't find a simple answer to this problem.
I am trying to create an MSBuild file to allow me to easily use SpecFlow and NUnit within Visual Studio 2010 express.
The file below is not complete this is just a proof of concept and it needs to be made more generic.
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
BuildSolution;
SpecFlow;
BuildProject;
NUnit;
</BuildDependsOn>
</PropertyGroup>
<PropertyGroup>
<Solution>C:\Users\Craig\Documents\My Dropbox\Cells\Cells.sln</Solution>
<CSProject>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\Configuration.csproj</CSProject>
<DLL>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\bin\Debug\Configuration.dll</DLL>
<CSFile>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\SpecFlowFeature1.feature.cs</CSFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)">
<Message Text="Build Started" Importance="high" />
<Message Text="Build Ended" Importance="high" />
</Target>
<Target Name="BuildSolution">
<Message Text="BuildSolution Started" Importance="high" />
<MSBuild Projects="$(Solution)" Properties="Configuration=Debug" />
<Message Text="BuildSolution Ended" Importance="high" />
</Target>
<Target Name="SpecFlow">
<Message Text="SpecFlow Started" Importance="high" />
<Exec Command='SpecFlow generateall "$(CSProject)"' />
<Message Text="SpecFlow Ended" Importance="high" />
</Target>
<Target Name="BuildProject">
<Message Text="BuildProject Started" Importance="high" />
<MSBuild Projects="$(CSProject)" Properties="Configuration=Debug" />
<Message Text="BuildProject Ended" Importance="high" />
</Target>
<Target Name="NUnit">
<Message Text="NUnit Started" Importance="high" />
<Exec Command='NUnit /run "$(DLL)"' />
<Message Text="NUnit Ended" Importance="high" />
</Target>
</Project>
The SpecFlow Task looks in the .csproj file and creates a SpecFlowFeature1.feature.cs.
I need to include this file when building the .csproj so that NUnit can use it.
I know I could modify (either directly or on a copy) the .csproj file to include the generated file but I'd prefer to avoid this.
My question is: Is there a way to use the MSBuild Task to build the project file and tell it to include an additional file to include in the build?
Thank you.
I found no way of doing it without editing the project file.
So I made an MSBuild file to:
Copy the project files
Run the copies through SpecFlow
Add the new .cs files to the copied projects
Compile the projects
Debug Run each of the compiled DLLs through NUnit
Clean up - Delete the copied projects
I've blogged about how to use it here:
http://learntdd.wordpress.com/2010/06/10/using-specflow-and-nunit-on-visual-studio-2010-express/
(It's version 1, I'd like to improve the script)
I couldn't think of any way to achieve without any modification to the .csproj file.
The approach I'd suggest would look like this.
In your .csproj you Import a container target file
...
<Import Project="SpecFlow.target" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
...
just above the CSharp.targets.
Specflow.targets would look like this
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="#(Compile)" />
</ItemGroup>
</Project>
so it doesn't harm while building the project from VS.
You could then use the Output of your SpecFlow Exec and add it to the SpecFlow.targets file
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="#(Compile)" />
<Compile Include="SpecFlowFeature1.feature.cs" />
</ItemGroup>
</Project>
...
and clean SpecFlow.targets after building your .csproj.

Setting properties' values in MSBuild

Let's consider the below example.
There, I have:
target MAIN calls target t and then calls target tt.
target t calls target ttt, and target tt calls target tttt.
target t defines property aa, and target ttt modifies aa.
target tttt tries to print property aa's value.
In short, we have: MAIN -> {t -> {ttt->modifies aa, defines aa}, tt -> tttt -> prints aa}
But in target tttt, we can't "see" aa's updated value (by ttt)! How do I make that value visible to target tttt?
The whole script is as below:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
<Target Name="MAIN" >
<CallTarget Targets="t" />
<CallTarget Targets="tt" />
</Target>
<Target Name="t">
<Message Text="t" />
<PropertyGroup>
<aa>1</aa>
</PropertyGroup>
<CallTarget Targets="ttt" />
</Target>
<Target Name="tt">
<Message Text="tt" />
<CallTarget Targets="tttt" />
</Target>
<Target Name="ttt">
<PropertyGroup>
<aa>122</aa>
</PropertyGroup>
<Message Text="ttt" />
</Target>
<Target Name="tttt">
<Message Text="tttt" />
<Message Text="tttt:$(aa)" />
</Target>
</Project>
As already said in an answer to another post you should model your MSBuild project with dependencies between your Targets rather than calling Targets one after another.
<Project DefaultTargets="tttt" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="t">
<Message Text="t" />
<PropertyGroup>
<aa>1</aa>
</PropertyGroup>
</Target>
<Target Name="tt" DependsOnTargets="t">
<Message Text="tt" />
</Target>
<Target Name="ttt" DependsOnTargets="t;tt">
<PropertyGroup>
<aa>122</aa>
</PropertyGroup>
<Message Text="ttt" />
</Target>
<Target Name="tttt" DependsOnTargets="t;tt;ttt">
<Message Text="tttt" />
<Message Text="tttt:$(aa)" />
</Target>
</Project>
An approach I use, is to define a Target as my final goal, putting it into the projects DefaultTargets.
Then add all the things that need to happen to achieve this goal.