In a Target, I need to use %(Foo.Filename)%(Foo.Extension) several times.
Is there a way to define a “variable” (or something similar) that can be used instead of %(Foo.Filename)%(Foo.Extension)? Preferably inside the Target?
(For the sake of completeness: %(Foo.Identity) is something different.)
Depends on how the % is being used in that target, as it's not just a shorthand to reference metadata as if it's a class property but a method of batching item groups. Sans some edge cases, e.g. item group of 1, creating a property during execution won't work as it'll overwrite itself on loop; most common way would be to either do target batching, transforms or custom metadata.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Foo Include="*.cs" />
</ItemGroup>
<Target Name="Foo">
<ItemGroup>
<Foo>
<Lorem>Ipsum.%(Filename)%(Extension)</Lorem>
</Foo>
</ItemGroup>
<Message Text="%(Foo.Lorem)" />
</Target>
<Target Name="Bar">
<ItemGroup>
<Lorem Include="Ipsum.%(Foo.Filename)%(Extension)" />
</ItemGroup>
<Message Text="#(Lorem)" />
<Message Text="#(Foo -> 'Ipsum.%(Filename)%(Extension)')" />
</Target>
<Target Name="Baz" Inputs="#(Foo)" Outputs="Ipsum.%(Filename)%(Extension)">
<PropertyGroup>
<Lorem>Ipsum.%(Foo.Filename)%(Extension)</Lorem>
</PropertyGroup>
<Message Text="$(Lorem)" />
</Target>
</Project>
Related
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>
I use msbuild in main.proj to build a project like this:
<MSBuild Projects="outs.proj" Targets="Build">
<Output ItemName="CustomOutputs" TaskParameter="TargetOutputs"/>
</MSBuild>
Inside outs.proj I have a custom Target, I need to add an output from this target to get .dll,.pdb,..., and .mycustomfiles
How can I send data from child project to parent project ?
Thanks in advance for your help.
I'd recommend you simply Import the dependant project, however the basic scenario you described can be achieved with Target's Outputs or Returns and corresponding Output's TargetOutputs although there are few caveats as it's designed for incremental builds and not as a data transfer object.
foo.build
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Foo1">
<MSBuild Projects="bar.build">
<Output TaskParameter="TargetOutputs" ItemName="Bar" />
</MSBuild>
<Message Text="%(Bar.Identity)" />
</Target>
<Import Project="bar.build" />
<Target Name="Foo2" DependsOnTargets="Bar">
<Message Text="%(Bar.Identity)" />
</Target>
</Project>
bar.build
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Bar" Outputs="#(Bar)">
<ItemGroup>
<Bar Include="**\*.dll" />
</ItemGroup>
</Target>
</Project>
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>
I'm desperately curious why I am unable to create an item in a global scope based on a metadata condition which works as expected inside a target. For instance, this works as expected:
<ItemGroup>
<TestItems Include="TestItem1">
<TestFlag>true</TestFlag>
</TestItems>
<TestItems Include="TestItem2">
<TestFlag>false</TestFlag>
</TestItems>
</ItemGroup>
<Target Name="Default">
<Message Text="#(TestItems)" />
<Message Text="#(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
<ItemGroup>
<FilteredTestItems Include="#(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
</ItemGroup>
<Message Text="#(FilteredTestItems)" />
<Message Text="#(FilteredTestItems)" Condition="'%(FilteredTestItems.TestFlag)'=='true'" />
</Target>
and produces the following output:
TestItem1;TestItem2
TestItem1
TestItem1
TestItem1
And this works as expected:
<ItemGroup>
<TestItems Include="TestItem1">
<TestFlag>true</TestFlag>
</TestItems>
<TestItems Include="TestItem2">
<TestFlag>false</TestFlag>
</TestItems>
</ItemGroup>
<ItemGroup>
<FilteredTestItems Include="#(TestItems)" Condition="'false'=='true'" />
</ItemGroup>
<Target Name="Default">
<Message Text="#(TestItems)" />
<Message Text="#(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
<Message Text="#(FilteredTestItems)" />
<Message Text="#(FilteredTestItems)" Condition="'%(FilteredTestItems.TestFlag)'=='true'" />
</Target>
Producing the following output:
TestItem1;TestItem2
TestItem1
But this:
<ItemGroup>
<TestItems Include="TestItem1">
<TestFlag>true</TestFlag>
</TestItems>
<TestItems Include="TestItem2">
<TestFlag>false</TestFlag>
</TestItems>
</ItemGroup>
<ItemGroup>
<FilteredTestItems Include="#(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
</ItemGroup>
Produces the following MSBuild error:
temp.proj(13,45): error MSB4090: Found an unexpected character '%' at position 2 in condition "'%(TestItems.TestFlag)'=='true'".
So what gives? Certainly I can work around it, but what exactly am I not understanding about ItemGroup, metadata and/or the global scope?
The item group condition works outside a target, but batching doesn't (that's the "%" operator). Batching is used when you call a task, and since you can only call a task from inside a target, it makes sense for batching to also only work inside a target.
You might ask why the item group works inside the target since it's not a task. Prior to MSBuild 3.5, you weren't allowed item groups inside targets at all; you had to call CreateItem instead. In versions 3.5 and 4.0, using item groups that way is allowed, but I think it's just syntactic sugar for calling the CreateItem task, so your condition works because there is a task behind the scenes.
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.