How to create copying items from property values? - msbuild

Let's say I have a list of sub paths such as
<PropertyGroup>
<subPaths>$(path1)\**\*; $(path2)\**\*; $(path3)\file3.txt; </subPaths>
</PropertyGroup>
I want to copy these files from folder A to folder B (surely we already have all the sub folders/files in A). What I try was:
<Target Name="Replace" DependsOnTargets="Replace_Init; Replace_Copy1Path">
</Target>
<Target Name="Replace_Init">
<PropertyGroup>
<subPaths>$(path1)\**\*; $(path2)\**\*; $(path3)\file3.txt; </subPaths>
</PropertyGroup>
<ItemGroup>
<subPathItems Include="$(subPathFiles.Split(';'))" />
</ItemGroup>
</Target>
<Target Name="Replace_Copy1Path" Outputs="%(subPathItems.Identity)">
<PropertyGroup>
<src>$(folderA)\%(subPathItems.Identity)</src>
<dest>$(folderB)\%(subPathItems.Identity)</dest>
</PropertyGroup>
<Copy SourceFiles="$(src)" DestinationFiles="$(dest)" />
</Target>
But the Copy task didn't work. It doesn't translate the **\* to files. What did I do wrong? Please help!

I don't think you can do something like that.
$(subPathFiles.Split(';')) returns a property where value are separated by semicolon, so this call is useless.
If you want to keep this mechanism you should use the task StringToItemCol from MSBuild Extension Pack :
<Target Name="Replace_Init">
<PropertyGroup>
<subPaths>$(path1)\**\*; $(path2)\**\*; $(path3)\file3.txt; </subPaths>
</PropertyGroup>
<MsBuildHelper TaskAction="StringToItemCol"
ItemString="$(subPaths)" Separator=";">
<Output TaskParameter="OutputItems" ItemName="subPathItems "/>
</MsBuildHelper>
</Target>
Otherwise, you could directly pass items with folderA and subPaths embedded :
<ItemGroup>
<subPathIt Include="$(folderA)\$(path1)\**\*"/>
<subPathIt Include="$(folderA)\$(path2)\**\*"/>
<subPathIt Include="$(folderA)\$(path3)\file3.txt" Condition="Exists('$(path3)\file3.txt')"/>
</ItemGroup>
<Target Name="Replace_Copy1Path">
<Copy SourceFiles="#(subPathItems )"
DestinationFiles="$(folderB)\%(RecursiveDir)\%(Filename)%(Extension)" />
</Target>

Related

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>

Deleting parent folder msbuild

Am passing the the below path as parameter to msbuild project.
"D:\Tools\TestTools\Folder1\Folder2\Folder3"
How to I delete the "Folder1" by traversing this parameter using msbuild?
Thanks...
You could just split the path twice:
<Target Name="DeleteSubDir" DependsOnTargets="">
<PropertyGroup>
<Dir>D:\Tools\TestTools\Folder1\Folder2\Folder3</Dir>
<DirToDelete>$([System.IO.Path]::GetDirectoryName('$(Dir)'))</DirToDelete>
<DirToDelete>$([System.IO.Path]::GetDirectoryName('$(DirToDelete)'))</DirToDelete>
</PropertyGroup>
<RemoveDir Directories="$(DirToDelete)" />
</Target>
Just explicitly go two directories above:
<Target Name="DeleteSubDir" DependsOnTargets="">
<PropertyGroup>
<Dir>D:\Tools\TestTools\Folder1\Folder2\Folder3</Dir>
<DirToDelete>$([System.IO.Path]::GetFullPath('$(Dir)\..\..'))</DirToDelete>
</PropertyGroup>
<RemoveDir Directories="$(DirToDelete)" />
</Target>

Copy a single file in MSBuild without using Exec or ItemGroup

Is there any way to do this? I just need to copy a single file and thought there may be some syntax for the SourceFiles parameter of the Copy task that means you don't need to define an ItemGroup beforehand, I'd rather stick with ItemGroup than use Exec though.
Copy files also takes a straight propertygroup as input:
<PropertyGroup>
<SourceFile>Some file</SourceFile>
</PropertyGroup>
<Copy SourceFiles="$(SourceFile)" DestinationFolder="c:\"/>
Or even just a string
<Copy SourceFiles="Pathtofile" DestinationFolder="c:\"/>
Just put the single file name as the value for "SourceFiles".
Easy-Peezey.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="AllTargetsWrapper" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>
<Target Name="AllTargetsWrapper">
<CallTarget Targets="CopyItTarget" />
</Target>
<Target Name="CopyItTarget">
<Copy SourceFiles="c:\windows\system.ini" DestinationFolder="$(WorkingCheckout)\"/>
<Error Condition="!Exists('$(WorkingCheckout)\system.ini')" Text="No Copy Is Bad And Sad" />
</Target>
</Project>
For what it's worth, I needed to do the same thing, and wanted to put some version information in the file name. Here is how I did it for a project in $(SolutionDir) that references an executable created by another project in another solution that I can easily express the path to:
<Target Name="AfterBuild">
<GetAssemblyIdentity AssemblyFiles="$(SolutionDir)..\bin\$(Configuration)\SomeExectuable.exe">
<Output TaskParameter="Assemblies" ItemName="AssemblyVersions" />
</GetAssemblyIdentity>
<CreateProperty Value="$(TargetDir)$(TargetName)-%(AssemblyVersions.Version)$(TargetExt)">
<Output TaskParameter="Value" PropertyName="NewTargetPath" />
</CreateProperty>
<Copy SourceFiles="$(TargetPath)" DestinationFiles="$(NewTargetPath)" />
</Target>

MSBuild multiple outputpath

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>

How do I execute tasks in MSBUILD?

I'm trying to wrap my head around MSBuild.
I have a very simple script that does the following so far:
Builds a solution and places it in my drop location.
I have created a <Target> and in it I would like to copy files and from my source control location and drop them in the drop location as well.
Eventually the script will have to create the folders etc. For now I am just trying to copy one file over to see how this works.
The solution builds and is placed in the drop location but no files are copied. The build log makes no mention of this Target ever being run.
What am I missing?
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<Target Name="Build">
<Message Text="Building msbuildintro" />
</Target>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<ProjectExtensions>
<ProjectFileVersion>2</ProjectFileVersion>
<Description></Description>
<BuildMachine>hw-tfs-build02</BuildMachine>
</ProjectExtensions>
<PropertyGroup>
/* Properties here*/
</PropertyGroup>
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/HostASPX/mySolution.sln">
<Targets></Targets>
<Properties></Properties>
</SolutionToBuild>
<CommonFiles Include="$(SolutionRoot)\trunk\folder\Common\Shared\js\somefile.js"></CommonFiles>
</ItemGroup>
<ItemGroup>
<ConfigurationToBuild Include="Release|Any CPU">
<FlavorToBuild>Release</FlavorToBuild>
<PlatformToBuild>Any CPU</PlatformToBuild>
</ConfigurationToBuild>
</ItemGroup>
<Target Name="CopyCommonData">
<Message Text="Copy Common Data" />
<Copy SourceFiles="#(CommonFiles)"
DestinationFiles="$(DropLocation)\Common\somefile.js" />
</Target>
</Project>
Thanks!
OH I get it.. Target Names are not 'made up'. They must be a specific Target Name found here:
http://msdn.microsoft.com/en-us/library/aa337604.aspx