Cross-Join ItemGroups in MSBuild - msbuild

Given something like so..
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ConfigFiles Include="*.config" />
<DatabaseConfig Include="ABC">
<Database>DB1</Database>
<CsString>Database</CsString>
</DatabaseConfig>
<DatabaseConfig Include="DEF">
<Database>DB2</Database>
<CsString>Logging</CsString>
</DatabaseConfig>
</ItemGroup>
<Target Name="test" >
<!-- Some sort of join here (or somewhere)... -->
<Message Text=" %(Combined.ConfigFile) %(Combined.Database) " />
</Target>
</Project>
I'd like the Output to be something like this.. (given two files one.config & two.config)
one.config DB1
two.config DB1
one.config DB2
two.config DB2
(the order is not important, just the full cartesian product of the two ItemGroups)

This seems like a tidy solution:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ConfigFiles Include="*.config" />
<DatabaseConfig Include="ABC">
<Database>DB1</Database>
<CsString>Database</CsString>
</DatabaseConfig>
<DatabaseConfig Include="DEF">
<Database>DB2</Database>
<CsString>Logging</CsString>
</DatabaseConfig>
</ItemGroup>
<Target Name="test" >
<ItemGroup>
<Combined Include="#(DatabaseConfig)">
<ConfigFile>%(ConfigFiles.Identity)</ConfigFile>
</Combined>
</ItemGroup>
<Message Text=" %(Combined.ConfigFile) %(Combined.Database) " />
</Target>
</Project>

There is a way you can do this with minimal changes to your existing sample code. You can combine metadata from ConfigFiles items and DatabaseConfig items into a new "combined" item and then output that "combined" item.
To combine the metadata, use target batching with the batched target running once for each DatabaseConfig item. Then you can call another target to output the combined metadata to get the output you described. Take a look at my extension of your sample code to see how this would all be accomplished:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ConfigFiles Include="*.config" />
<DatabaseConfig Include="ABC">
<Database>DB1</Database>
<CsString>Database</CsString>
</DatabaseConfig>
<DatabaseConfig Include="DEF">
<Database>DB2</Database>
<CsString>Logging</CsString>
</DatabaseConfig>
</ItemGroup>
<Target Name="test" DependsOnTargets="test_setup;test_output" >
<!-- Logic here runs after targets listed in "DependsOnTargets". -->
</Target>
<!-- This will run once for each "DatabaseConfig" item. -->
<Target Name="test_setup" Outputs="%(DatabaseConfig.Identity)">
<PropertyGroup>
<!-- Specify the Database for the current DatabaseConfig item -->
<CurrentDb>%(DatabaseConfig.Database)</CurrentDb>
</PropertyGroup>
<ItemGroup>
<!-- Add a new CombinedOutput item with each run, combining metadata. -->
<CombinedOutput Include=" %(ConfigFiles.FileName)%(ConfigFiles.Extension) $(CurrentDb) " />
</ItemGroup>
</Target>
<Target Name="test_output">
<!-- Output the combined metadata from the CombinedOutput items -->
<Message Text=" %(CombinedOutput.Identity) " />
</Target>
</Project>
What's happening in the sample:
The test target now just serves as a way to call two other targets to perform the work: test_setup, and test_output
The test_setup target is batched and creates the new
CombinedOutput items.
The test_output target is called after test_setup
to output the CombinedOutput items' metadata.
Output from test_output:
one.config DB1
two.config DB1
one.config DB2
two.config DB2

Related

MSBuild: Output properties from imported projects

Let's say I have a build.proj like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="AfterBuild"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CustomAfterMicrosoftCSharpTargets>$(MSBuildThisFileDirectory)Common.Build.targets</CustomAfterMicrosoftCSharpTargets>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<ProjectProperties>
Configuration=$(Configuration);
Platform=$(Platform);
CustomAfterMicrosoftCSharpTargets=$(CustomAfterMicrosoftCSharpTargets);
</ProjectProperties>
</PropertyGroup>
<ItemGroup>
<ProjectToBuild Include="$(MSBuildThisFileDirectory)src\Proj\MyApp.csproj" />
</ItemGroup>
<Target Name="Build">
<MSBuild Targets="Build"
Projects="#(ProjectToBuild)"
Properties="$(ProjectProperties)" />
</Target>
<Target Name="AfterBuild" DependsOn="Build">
<Message Text="ChildProperty: $(ChildProperty)" />
</Target>
</Project>
In Common.Build.targets, I have a Target that creates a property:
<Target Name="DoSomethingUseful">
<!-- Do something useful -->
<CreateProperty Value="SomeComputedThingy">
<Output TaskParameter="Value" PropertyName="ChildProperty"/>
</CreateProperty>
</Target>
Now if I build build.proj, I do not see the value of ChildProperty in the message. The output is blank: ChildProperty:.
I was under the impression that any output for a target is merged back to global context after its execution. But it seems that it only applies to anything within that target file.
How do I make ChildProperty bubble up to the parent build.proj?
When you are calling <MSBuild> task on dependent projects, read TargetOutputs output parameter of the task. See example from MSDN:
<Target Name="BuildOtherProjects">
<MSBuild
Projects="#(ProjectReferences)"
Targets="Build">
<Output
TaskParameter="TargetOutputs"
ItemName="AssembliesBuiltByChildProjects" />
</MSBuild>
</Target>
You will also need to ensure the target you are calling in dependent projects correctly populates Returns or Output parameter (Returns takes precedence if used). E.g.:
<Target Name="MyTarget" Inputs="..." Outputs="..." Returns="$(MyOutputValue)">
<PropertyGroup>
<MyOutputValue>set it here</MyOutputValue>
</PropertyGroup>
</Target>

How to extract directory from property?

I have a property GroupProj storing a full path name. How can I extract the directory of the property?
I have the following code, but it doesn't work as expected:
<PropertyGroup>
<GroupProj>C:\development\project\default.groupproj</GroupProj>
</PropertyGroup>
<Target Name="Default">
<Message Text="Echo: $(GroupProj->'%(RootDir)')" />
</Target>
I will describe my actual intention of doing so. Perhaps there is a way to do the job that I am not aware of.
I have a Delphi groupproj (MSBuild project) file, C:\development\project\default.groupproj:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Projects Include="project1.dproj">
<Dependencies/>
</Projects>
<Projects Include="project2.dproj">
<Dependencies/>
</Projects>
<Projects Include="project3.dproj">
<Dependencies/>
</Projects>
</ItemGroup>
...
</Project>
There are other 3 MSBuild files (project1.dproj, project2.dproj and project3.dproj) stored in same folder as default.groupproj.
I create a MSBuild project file (c:\test.targets):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="3.5">
<Import Project="$(GroupProj)" />
<Target Name="Build">
<MSBuild BuildInParallel="True" Projects="project1.dproj;project2.dproj;project3.dproj"/>
</Target>
</Project>
And execute as:
c:\> msbuild /p:GroupProj="C:\development\project\default.groupproj" test.targets
The execution shall fail as MSBuild can't find projectN.dproj file. The issue shall be the working directory isn't set to default.groupproj.
One straight solution come into my mind is to extract directory of $(GroupProj) and concat to there projectN.dproj file.
That's the whole story of my question.
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<GroupProj>C:\development\project\default.groupproj</GroupProj>
</PropertyGroup>
<Target Name="Build">
<CreateItem Include="$(GroupProj)">
<Output TaskParameter="Include" ItemName="ItemFromProp"/>
</CreateItem>
<Message Text="1. #(ItemFromProp -> '%(RootDir)%(Directory)')"/>
<Message Text="2. %(ItemFromProp.RootDir)%(ItemFromProp.Directory)"/>
<Message Text="3. %(ItemFromProp.Identity)"/>
<Message Text="4. %(ItemFromProp.FullPath)"/>
<Message Text="5. %(ItemFromProp.FileName)"/>
<Message Text="6. %(ItemFromProp.Extension)"/>
</Target>
</Project>
EDIT:
To build the projects in parallel try this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GetGroupProjPath">
<ItemGroup>
<GroupProj Include="$(GroupProj)" />
<GroupProjPath Include="#(GroupProj->'%(Directory)')" />
</ItemGroup>
<PropertyGroup>
<GroupProjPath>#(GroupProjPath->'%(RootDir)%(Identity)')</GroupProjPath>
</PropertyGroup>
</Target>
<Import Project="$(GroupProj)" />
<Target Name="GetDProjs" DependsOnTargets="GetGroupProjPath">
<ItemGroup>
<DProjs Include="#(Projects->'$(GroupProjPath)%(FileName)%(Extension)')" />
</ItemGroup>
</Target>
<Target Name="Build" DependsOnTargets="GetDProjs">
<Message Text="#(DProjs)" />
</Target>
</Project>

MSBuild: How can I define using XmlPeek that is usable in a ResouceCompile task?

When I read the value from the XML file, it is correct, but when I use it (as an option to rc.exe), it is undefined. How can I fix that?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Read custom build number from generated XML file -->
<Target Name="ReadCustomVersion" BeforeTargets="ResourceCompile">
<XmlPeek XmlInputPath="$(OutDir)\CustomVersionNumber.xml" Query="/CustomVersion/CustomBuildNumber/text()">
<Output TaskParameter="Result" ItemName="CustomBuildNumber" />
</XmlPeek>
**<!-- Print out the custom build number -- it is correct here -->**
<Message Text="CustomBuildNumber = #(CustomBuildNumber)">
</Target>
<!-- Add version resource -->
<ItemGroup>
**<!-- CustomBuildNumber will not be set here -->**
<ResourceCompile Include="..\..\build\CommonVersionResource.rc">
<AdditionalOptions>/DBUILD_NUMBER=$(CustomBuildNumber) %(AdditionalOptions)</AdditionalOptions>
</ResourceCompile>
</ItemGroup>
</Project>
Use PropertyName attribute in XmlPeek/Output element instead of ItemName.
Why you don't add AdditionalOptions directly in your ReadCusomVersion target?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetCustomVersion" BeforeTargets="ResourceCompile">
<XmlPeek XmlInputPath="$(OutDir)\CustomVersionNumber.xml" Query="/CustomVersion/CustomBuildNumber/text()">
<Output TaskParameter="Result" PropertyName="CustomBuildNumber" />
</XmlPeek>
<ItemGroup>
<ResourceCompile>
<AdditionalOptions>/DBUILD_NUMBER=$(CustomBuildNumber) %(AdditionalOptions)</AdditionalOptions>
</ResourceCompile>
</ItemGroup>
</Target>

How wait copy process on msbuild

I create example to copy file from one folder to another (use msbuild). I try check after copy, if file was moved? But message still show, that files didn't moved. But when I see on folder, files was moved successful. So how it's fix?
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PathPackage>C:\Users\test\Desktop\test\1\*.txt</PathPackage>
<Files>C:\Users\test\Desktop\test\2\*.*</Files>
</PropertyGroup>
<ItemGroup>
<Packages Include="$(PathPackage)"/>
<FilesOnFolder Include="$(Files)"/>
</ItemGroup>
<Target Name="B">
<Message Importance="normal" Text="Package before copy:#(Packages)"/>
<CreateItem Include="#(Packages)">
<Output TaskParameter="Include" ItemName="FilesToMove" />
</CreateItem>
<Copy
SourceFiles="#(Packages)"
DestinationFolder="C:\Users\test\Desktop\test\2"
/>
<Delete Files="#(Packages)" />
<Message Importance="normal" Text="Package after package:#(Packages)"/><!--It's full! -->
<Message Importance="normal" Text="Destination Folder:#(FilesOnFolder)"/> <!--It's empty! -->
</Target>
</Project>
Problem was in ItemGroup. It need write inside target.

msbuild to copy to multiple locations defined in item metadata

I have an item with metadata I want to copy to perform some actions on and the result to occur in multiple locations,
for example, I want to copy the file to multiple locations:
<ItemGroup>
<MyItem Include="myFile.txt">
<Out>c:\blah;c:\test</Out>
</MyItem>
</ItemGroup>
how would I setup a target to create c:\blah and c:\test if they dont exist, then copy myFile.txt to c:\blah\myFile.txt and c:\test\myFile.txt
I also want to get the list of full output paths (c:\blah\myFile.txt and c:\test\myFile.txt) if I want to clean them during a clean.
If you dont want to change the structure of you ItemGroup, you need to handle that you have a nested ItemGroup (the MetaDataElement Out). Therefor you will need to batch the ItemGroup MyItem to the target and inside you can batch Out. I made a small example project:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="CopyFiles" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<MyItem Include="myFile.txt">
<Out>c:\blah;c:\test</Out>
</MyItem>
<MyItem Include="myFile2.txt">
<Out>c:\blah2;c:\test2</Out>
</MyItem>
</ItemGroup>
<Target Name="CopyFiles"
Inputs="%(MyItem.Identity)"
Outputs="%(MyItem.Identity)\ignore_this.msg">
<PropertyGroup>
<File>%(MyItem.Identity)</File>
</PropertyGroup>
<ItemGroup>
<Folders Include="%(MyItem.Out)" />
</ItemGroup>
<Message Text="%(Folders.Identity)\$(File)" />
</Target>
</Project>
The Output will be:
Project "D:\TEMP\test.proj" on node 1 (default targets).
CopyFiles:
c:\blah\myFile.txt
c:\test\myFile.txt
CopyFiles:
c:\blah2\myFile2.txt
c:\test2\myFile2.txt
Done Building Project "D:\TEMP\test.proj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
What you want to do is a concept called MSBuild Batching.
It allows you to divide item lists into different batches and pass each of those batches into a task separately.
<Target Name="CopyFiles">
<ItemGroup Label="MyFolders">
<Folder Include="c:\blah" />
<Folder Include="C:\test" />
</ItemGroup>
<Copy SourceFiles="myFile.txt" DestinationFolder="%(Folder.Identity)\">
<Output TaskParameter="CopiedFiles" ItemName="FilesCopy" />
</Copy>
</Target>
How about this:
<Target Name="CopyFiles">
<!--The item(s)-->
<ItemGroup>
<MyItem Include="myFile.txt"/>
</ItemGroup>
<!--The destinations-->
<ItemGroup>
<MyDestination Include="c:\blah"/>
<MyDestination Include="c:\test"/>
</ItemGroup>
<!--The copy-->
<Copy SourceFiles="#(MyItem)" DestinationFolder="%(MyDestination.FullPath)" />
<ItemGroup>
<FileWrites Include="%(MyDestination.FullPath)\*" />
</ItemGroup>
<!--The output -->
<Message Text="FileWrites: #(FileWrites)" Importance="high"/>
</Target>