How do I pass a property to MSBuild via command line that could be parsed into an item group?
Asked Answered
A

1

52

I have the following MSBuild script:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">    
  <PropertyGroup>
    <build_configurations>test1;test2;test3</build_configurations>
  </PropertyGroup>    
  <ItemGroup>
    <BuildConfigurations Include="$(build_configurations)" />
  </ItemGroup>    
  <Target Name="Main">    
    <Message Text="Running with args: %(BuildConfigurations.Identity)" />
  </Target>
</Project>

If I invoke the script without any parameters, I get the expected response:

Running with args: test1
Running with args: test2
Running with args: test3

However, when I attempt to set the property via command-line like so:

msbuild [myscript] /p:build_configurations=test5%3btest6%3btest7

I get the following:

Running with args: test5;test6;test7

So, it's not batching as expected. I need to get MSBuild to create the item group with three items instead of one item. How should I do that? Thanks.

P.S. The following article basically addresses my question except the case where I want to pass semicolon-separated values: http://sedodream.com/CommentView,guid,096a2e3f-fcff-4715-8d00-73d8f2491a13.aspx

Ardeha answered 18/5, 2011 at 21:8 Comment(0)
B
57

You've escaped the semicolons, preventing MSBuild from parsing them as individual items. Run like this instead, with quotes,

msbuild [myscript] /p:build_configurations="test5;test6;test7"

you will get the following output,

Running with args: test5
Running with args: test6
Running with args: test7
Birdcage answered 18/5, 2011 at 23:20 Comment(2)
This may have broken in MSBuild. PowerShell workaround seems to be /p:build_configurations=\`"test5`;test6\`" (slash and backtick before quotes, backtick before semicolon). For Cmd, /p:build_configurations=\"test5;test6\" (slash before quote).Abcoulomb
@Vimes: From PowerShell it should be '/p:build_configurations="test5;test6;test7"' or, if you need string interpolation, "/p:build_configurations=`"test5;test6;test7`"" In PowerShell 7.3+, you may additionally have to wrap your call in & { $PSNativeCommandArgumentPassing = 'Legacy'; msbuild ... }. See GitHub issue #18660.Makowski

© 2022 - 2024 — McMap. All rights reserved.