1

I have an MSBuild ItemGroup and I would like to be able to echo it out in the "Post-Build Event".

However when I try commands like: echo My ItemGroup: @(Foo)

I get the error:

error MSB4164: The value "echo My ItemGroup: @(Foo)" of metadata "Command" contains an item list expression. Item list expressions are not allowed on default metadata values.

I'm not very good with ItemGroups as of yet. Is there a way I can just echo the list of files that Foo contains?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Can you post a bit more of the code that you're attempting to use? Something which includes the ItemGroup tags and the Target tags of what you're trying to do would help me understand your problem better. And then please reply to my comment so that I get a notification that the question has been updated :) – Zain Rizvi Nov 10 '14 at 19:17
  • @ZainRizvi So given the `ItemGroup` `Foo` contains "a.txt;b.txt;c.txt" I'd like to `echo`: "My ItemGroup: a.txt;b.txt;c.txt" – Jonathan Mee Nov 10 '14 at 19:29

2 Answers2

2

Try %(Foo.Identity) instead. That will print just one item from the list, but cause the Task containing it (the Exec I suppose) to loop over the items.

If that doesn't work, be sure to work with the XML file directly rather than the IDE, in case it escapes things or puts in other code we don't see.

(later) It might be like this post, where they lament it is not simple and needs direct editing of the XML anyway. So just change it to a Exec task where the itemlist expression appears in an attribute, not a metadata definition.

It is written that the PostBuildEvent is more of a backward compatibility thing, and the good one to use is the AfterBuild target, that “is able to contain arbitrary MSBuild tasks, including one ore more Exec tasks … it doesn't have a custom UI in the IDE … edit it as XML …” Tip 43 in Brian Kretzler's book.

Community
  • 1
  • 1
JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • That doesn't `echo` anything. In the .vcxproj `echo`s are represented like this: `echo My ItemGroup: @(Foo)` Because it is not in an XML attribute, I don't believe the "%" will cause the command to loop. – Jonathan Mee Nov 07 '14 at 13:33
  • @JonathanMee I edited the question. It helps to see the real build commands and not just the IDE actions that were used! – JDługosz Nov 07 '14 at 13:36
  • I actually _am_ using the `AfterBuild` event. The `echo` I'm trying to write in the `PostBuildEvent` is just to tell all my co-workers who use the `PostBuildEvent`: "Hey I'm copying these files to here with the `AfterBuild` target." – Jonathan Mee Nov 07 '14 at 14:22
  • Your link to http://stackoverflow.com/q/6780985/2642059 worked. But I have to make a `PropertyGroup` and output that in the `echo`. The `PropertyGroup` contains: `@(Foo->'%(FullPath)')<\FooOutput>` And the `echo` statement reads: `echo My ItemGroup: $(FooOutput)` I guess this technically answers the question but I'd like to see away to do this without an intermediate `PropertyGroup`. – Jonathan Mee Nov 07 '14 at 14:27
  • So put the echo command (via an Exec task) in the same target as AfterBuild's current work. (Target, not Event. The latter refers to the special item list containing commands as items) – JDługosz Nov 07 '14 at 16:52
  • Sadly, that misses the point I'm trying to accomplish. I want something _in_ the "Post-Build Event"'s `Command` so that my coworkers will be redirected to working with the `AfterBuildEvent`. – Jonathan Mee Nov 07 '14 at 19:18
  • Does it have to be a full list, or a note telling where to look? – JDługosz Nov 08 '14 at 06:49
  • It needs to be the full list of strings. So I'd like to see something along the lines of: "My ItemGroup: a.txt;b.txt;c.txt" – Jonathan Mee Nov 08 '14 at 14:32
1

You'll want something like:

<ItemGroup>
  <ForcedUsingFilesList Include="c:\path\to\files\*" />
</ItemGroup>
<Target Name="MyTarget">
  <PropertyGroup>
    <MyFiles>
        @(ForcedUsingFilesList->'%(FullPath)')
    </MyFiles>
  </PropertyGroup>
  <Exec>echo $(MyFiles)</Exec>
</Target>
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133