MSBuild multiple outputpath - msbuild

I saw this S.O question and have a similar requirement. This is what I have in a .targets file -
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="$(TeamBuildOutDir) != '' ">
<OutputPath>$(TeamBuildOutDir)\Assemblies</OutputPath>
</PropertyGroup>
How can I output to multiple folders?
e.g.- $(TeamBuildOutDir)\Assemblies2
TIA
Thanks Nick, The copy/paste mucked it up. This is what I tried -
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="$(TeamBuildOutDir) != '' ">
<OutputPath>$(TeamBuildOutDir)\Assemblies</OutputPath>
</PropertyGroup>
<Target Name="AfterBuild">
<Copy SourceFiles="$(OutputPath)\**\*.*" DestinationFolder="$(TeamBuildOutDir)\Assemblies2" />
</Target>
</Project>
I've also tried -
<Copy SourceFiles="$(OutputPath)\***\*.*" DestinationFolder="$(TeamBuildOutDir)\Assemblies2" />
and -
<Copy SourceFiles="$(OutputPath)\***\*.*" DestinationFolder="$(TeamBuildOutDir)\" />
in case the directory not being present caused an issue but still no luck.
Updated 7/28. Tried this but doesn't work still (no errors but the files are not present in the output directory. They are present in the Assemblies folder so I know the targets file is being triggered.) -
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="$(TeamBuildOutDir) != '' ">
<OutputPath>$(TeamBuildOutDir)\Assemblies</OutputPath>
</PropertyGroup>
<Target Name="AfterBuild">
<CreateItem Include="$(OutputPath)\**\*.*">
<Output ItemName="Outfiles" TaskParameter="Include" />
</CreateItem>
<Copy SourceFiles="#(Outfiles)" DestinationFiles="#(Outfiles->'$(TeamBuildOutDir)\%(relativedir)%(Filename)%(Extension)')" SkipUnchangedFiles="false" />
</Target>
</Project>

You create an AfterBuild target with a copy task the contents of $(OutputPath) to $(TeamBuildOutDir)\Assemblies2.
<Target Name="AfterBuild">
<Copy SourceFiles="$(OutputPath)\**\*.*" DestinationFolder="$(TeamBuildOutDir)\Assemblies2" />
</Target>
Edit, updating this to include a test message, and include a "DependsOnTarget" attribute to see if we can get this to occur after the build process...
<Target Name="AfterBuild" DependsOnTarget="Build">
<Message Text="**** TEST **** " Importance="high" />
<Copy SourceFiles="$(OutputPath)\**\*.*" DestinationFolder="$(TeamBuildOutDir)\Assemblies2" />
</Target>

Related

Msbuild repeat task on condition

I have a zip msbuild ZipDirectory task that generate a 1kb file sometimes although the expected output should be greater than 20 MB. Normally when I detect that problem and rerun the project the output is correct. The issue is that I have to manually and visually check the result. Is there a way of automatically checking the file size and run the zip task in loop until the correct output file size is detected?
I have already checked, and the problem is not the source directory content.
Below is a snippet similar to my project content
<Project DefaultTargets="CreateZipPackage" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<OutVersion>1.1.1.1</OutVersion>
<OutDirAndVersion>$(OutDir)\$(OutVersion)</OutDirAndVersion>
<WebAppPackagePath>$(OutDirAndVersion)\WebApp_$(OutVersion).zip</WebAppPackagePath>
<Database1PackagePath>$(OutDirAndVersion)\Db1_$(OutVersion).Database.zip</Database1PackagePath>
<Database2PackagePath>$(OutDirAndVersion)\Db2_$(OutVersion).Database.zip</Database2PackagePath>
<Database3PackagePath>$(OutDirAndVersion)\Db3_$(OutVersion).Database.zip</Database3PackagePath>
<ReleasePackageFolderPath>$(OutDir)\Interim_$(OutVersion)_Packages</ReleasePackageFolderPath>
<ReleasePackagePath>$(OutDir)\Final_$(OutVersion).zip</ReleasePackagePath>
</PropertyGroup>
<ItemGroup>
<DeployablePackages Include="$(OutDirAndVersion)\*.zip" />
</ItemGroup>
<Target Name="CreateZipPackage">
<MakeDir Directories="$(ReleasePackageFolderPath)" />
<MSBuild Projects="WebApp.csproj" ContinueOnError="false" Targets="Package" Properties="PackageLocation=$(WebAppPackagePath);PackageAsSingleFile=True;" />
<MSBuild Projects="Db1.Database.sqlproj" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration);PackageLocation=$(Database1PackagePath);" />
<MSBuild Projects="Db2.Database.sqlproj" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration);PackageLocation=$(Database2PackagePath);" />
<MSBuild Projects="Db3.Database.sqlproj" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration);PackageLocation=$(Database3PackagePath);" />
<Copy SourceFiles="#(DeployablePackages)" DestinationFolder="$(ReleasePackageFolderPath)" />
<ZipDirectory SourceDirectory="$(ReleasePackageFolderPath)" DestinationFile="$(ReleasePackagePath)" />
</Target>
</Project>

Execute a XCopy operation after all the projects build in MSBuild

I have a .proj file which is configured to execute a solution file which in turn build all the projects in the solution.
I want to add an XCopy operation which should copy the .dll files of all projects to another location only after all the projects build is completed.
I have tried with below, but it is not copying the dlls.
I am newbie in writing MSBuild tags, so it could be that I may be wrong in choosing this approach to write the task in this way.
Please provide a solution, if anyone knows.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == 'Release|Debug'"/>
<Platform Condition="'$(Platform)' == ''">x64</Platform>
</PropertyGroup>
<ItemDefinitionGroup>
<SolutionToBuild>
<Properties>Configuration=$(Configuration);Platform=$(Platform)</Properties>
<Targets>Clean;Build</Targets>
</SolutionToBuild>
</ItemDefinitionGroup>
<ItemGroup>
<SolutionToBuild Include="..\Seg\Algorithms.sln" />
</ItemGroup>
<Target Name="Build" >
<MSBuild Projects="#(SolutionToBuild)" Targets="%(SolutionToBuild.Targets)" Properties="%(SolutionToBuild.Properties)" BuildInParallel="false" ContinueOnError="false" />
</Target>
<Target Name="Clean">
<MSBuild Projects="#(SolutionToBuild)" Targets="Clean" Properties="%(SolutionToBuild.Properties)" BuildInParallel="false" ContinueOnError="false" />
</Target>
<PropertyGroup>
<CopyDestination>..\Extern\Algo\bin\$(Configuration)\</CopyDestination>
<CopySource>..\Seg\Algorithms\$(Configuration)\DoBin\</CopySource>
</PropertyGroup>
<ItemGroup>
<FilesToCopy Include="$(CopySource)*.dll"/>
</ItemGroup>
<ItemGroup>
<CustomBuildStep Include ="#(FilesToCopy)">
<Message>Copying..</Message>
<Command> XCOPY %(Identity) $(CopyDestination) /f /y </Command>
</CustomBuildStep>
</ItemGroup>
<PropertyGroup>
<CustomBuildAfterTargets>Build</CustomBuildAfterTargets>
</PropertyGroup>
</Project>
Think of Targets as methods that are called. They run in sequence, so you just need to put your copy after the solution build:
<Target Name="Build">
<MSBuild Projects="#(SolutionToBuild)" Targets="%(SolutionToBuild.Targets)" Properties="%(SolutionToBuild.Properties)" BuildInParallel="false" ContinueOnError="false" />
<ItemGroup>
<FilesToCopy Include="..\Seg\Algorithms\$(Configuration)\DoBin\*.dll" />
</ItemGroup>
<Copy SourceFiles="#(FilesToCopy)" DestinationFolder="..\Extern\Algo\bin\$(Configuration)\" SkipUnchangedFiles="true" />
</Target>

MSBuild: how to include generated classes into compilation?

I have the following MSBuild script:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<BuildDependsOn>
NSwag;
$(BuildDependsOn)
</BuildDependsOn>
<!--<AfterTransform>NSwag</AfterTransform>-->
</PropertyGroup>
<ItemGroup>
...
</ItemGroup>
<Target Name="NSwag" BeforeTargets="BeforeBuild">
<Message Text="Generating C# client code via NSwag" Importance="high" />
<!-- ISSUE HERE -->
<Copy SourceFiles="..\..\MyClient.cs" DestinationFiles="Gen\MyClient.cs" />
</Target>
</Project>
The Target "NSwag" above is going to be used for code generation tool. But to simplify things, I use here just a file copy command.
The issue is that the .cs files added within this Target are not visible in the MSBuild compilation:
The type or namespace name 'MyClient' does not exist in the namespace 'MyNamespace'
NOTE: The issue occurs only if the file didn't exist in the destination folder.
NOTE: I was trying to mangle with the following but with no success so far:
<Target Name="RemoveSourceCodeDuplicates" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="UpdateGeneratedFiles">
<RemoveDuplicates Inputs="#(Compile)">
<Output TaskParameter="Filtered" ItemName="Compile"/>
</RemoveDuplicates>
</Target>
and
<Target Name="UpdateGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="NSwag">
<ItemGroup>
<Compile Include="Gen\MyClient.cs" Condition="!Exists('Gen\MyClient.cs')" />
</ItemGroup>
</Target>
What am I missing here?
I think I found a workaround for that - check and include the files first (UpdateGeneratedFiles target), then generate them (NSwag target). See the script below:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<BuildDependsOn>
NSwag;
$(BuildDependsOn)
</BuildDependsOn>
</PropertyGroup>
<Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild"
DependsOnTargets="UpdateGeneratedFiles">
<Message Text="Generating C# client code via NSwag" Importance="high" />
<Copy SourceFiles="..\..\MyClient.cs" DestinationFiles="Gen\MyClient.cs" />
</Target>
<Target Name="UpdateGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" >
<ItemGroup>
<Compile Include="Gen\MyClient.cs" Condition="!Exists('Gen\MyClient.cs')" />
</ItemGroup>
</Target>
</Project>

Using MSBuild to buld a solution (.sln) with many projects in how can I make each project build into its own folder?

I am trying to create a simple build process for a quite complex (many projects) vs2010 solution.
I wish for a folder structure such as this
-Build
-Proj1
-proj1.exe
-proj1.dll
-Proj2
-proj2.exe
-proj2.dll
......
-Projn
-projn.exe
-projn.dll
What I am getting from my attempts below is
-Build
-proj1.exe
-proj1.dll
-proj2.exe
-proj2.dll
-projn.exe
-projn.dll
I currently have this as a .proj file. (see below)
This builds things fine, however it puts everything in the "build" folder that I specify. I want each project to be in its own seperate folder within that 'build' folder. How can I achive this?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildOutputDir>C:\Projects\BuildScripts\Build</BuildOutputDir>
<SolutionToCompile>PathToSolution.sln</SolutionToCompile>
</PropertyGroup>
<Target Name="Clean">
<RemoveDir Directories="$(BuildOutputDir)" />
</Target>
<Target Name="Compile">
<MakeDir Directories="$(BuildOutputDir)" />
<MSBuild Projects="$(SolutionToCompile)"
properties = "OutputPath=$(BuildOutputDir)" Targets="Rebuild" />
</Target>
<Target Name="Build" DependsOnTargets="Clean;Compile">
<Message Text="Clean, Compile"/>
</Target>
</Project>
I call the .proj with a simple bat
"%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" /nologo externalBuild.proj /m:2 %*
pause
I have also tried a more complex version (copy and paste!) that looks more like it should work, but still puts things in a single folder.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="BuildAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectsToBuild Include="path to solution folder\**\*proj" Exclude="$(MSBuildProjectFile)"/>
</ItemGroup>
<PropertyGroup>
<Configuration>CI</Configuration>
</PropertyGroup>
<Target Name="CoreBuild">
<MSBuild Projects ="#(ProjectsToBuild)"
ContinueOnError ="false"
Properties="Configuration=$(Configuration)">
<Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
</MSBuild>
</Target>
<PropertyGroup>
<DestFolder>Build\</DestFolder>
</PropertyGroup>
<Target Name="CopyFiles">
<Copy SourceFiles="#(OutputFiles)"
DestinationFiles="#(OutputFiles->'$(DestFolder)%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<Target Name="CleanAll">
<!-- Delete any files this process may have created from a previous execution -->
<CreateItem Include="$(DestFolder)\**\*exe;$(DestFolder)\**\*dll">
<Output ItemName="GeneratedFiles" TaskParameter="Include"/>
</CreateItem>
<Delete Files="#(GeneratedFiles)"/>
<MSBuild Projects="#(ProjectsToBuild)" Targets="Clean" Properties="Configuration=$(Configuration);"/>
</Target>
<PropertyGroup>
<BuildAllDependsOn>CleanAll;CoreBuild;CopyFiles</BuildAllDependsOn>
</PropertyGroup>
<Target Name="BuildAll" DependsOnTargets="$(BuildAllDependsOn)"/>
</Project>
Using devenv.com to build from the command line will do what you want. It will use the output directories specified in the project files. This is what we're using, because at the moment we don't need more control over the build mechanism.

Why won't the copy task item work in my msbuild script?

I have the following code in my msbuild script:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" InitialTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SolutionName>CommTrac.Web\CommTrac.Web</SolutionName>
<SolutionFileName>$(SolutionName).csproj</SolutionFileName>
</PropertyGroup>
<Target Name="Build">
<Message Text="Building the solution"/>
<MSBuild Projects="$(SolutionFileName)" ContinueOnError="false" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="CopyOutput" DependsOnTargets="Build">
<ItemGroup>
<BinFolder Exclude="*.pdb" Include="$(ProjectDir)bin\**\*.*"/>
<BuildOutputFolder Include="C:\BuildOutput" />
</ItemGroup>
<Message Text="Copying from directory: $(BinFolder)"/>
<Copy SourceFiles="$(BinFolder)" DestinationFolder="$(BuildOutputFolder)"/>
</Target>
</Project>
For some reason, it will not copy the files to my output directory. I have tried all the similar
solutions with other questions that I have seen similar to this issue. Anyone have any ideas?
BindFolder and BuildOutputFolder are items, not properties. So you need to reference them using #(BindFolder) and #(BuildOutputFolder) instead of using '$'.