Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to pass variables in MSBuild

Tags:

msbuild

I'm rather new to MS Build and have been reviewing many of the built in target files that ship with Visual Studio. I have seen variables passed a few different ways and am not quite sure of the differences between these:

$(...)
@(...)
%(...)
like image 299
Wallace Breza Avatar asked May 11 '10 21:05

Wallace Breza


People also ask

How do I set Environment Variables in MSBuild?

Click on System and Security and then on System. In the left pane, click on Advanced system settings. At the very bottom of the pop up, click on Environment Variables. Edit the Path variable and append the folder's path that contains the MSBuild.exe to it (e.g., ;C:\Windows\Microsoft.NET\Framework64\v4.

What is ItemGroup in MSBuild?

When multiple ItemGroup elements are used, items are combined into a single ItemGroup . For example, some items might be included by a separate ItemGroup element that's defined in an imported file. ItemGroups can have conditions applied by using the Condition attribute.

What is $( ProjectDir?

$(ProjectDir)The directory of the project (defined as drive + path); includes the trailing backslash '\'.

How do I set properties in MSBuild?

MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.


2 Answers

  • $(...)is used to access Property value (More info on Property element)

    <PropertyGroup>
      <Configuration>Debug</Configuration>
    </PropertyGroup>
    
    <Message Text="Configuration = $(Configuration)"/>
    
  • @(...) is used to access Item value (More info on Item element)

    <ItemGroup>
      <Reference Include="System.Data"/>
      <Reference Include="System.Web.*"/>
    </ItemGroup>
    
    <Message Text="References = @(Reference)"/>
    
  • %(...) is used to acces Item Metadata value (More info on Item Metadata). It's also used to do batching.

    <ItemGroup>
      <Compile Include="Account\ChangePassword.aspx.cs">
        <DependentUpon>ChangePassword.aspx</DependentUpon>
        <SubType>ASPXCodeBehind</SubType>
      <Compile/>
    </ItemGroup>
    
    <Message Text="Element @(Compile) of subtype %(SubType) and depend of %(DependentUpon)"/>
    
like image 158
Julien Hoarau Avatar answered Oct 17 '22 21:10

Julien Hoarau


Dollar - $(MyProp): Allows you to reference values specified within PropertyGroups.

At Sign - @(CodeFile): Allows you to reference lists of items specified within ItemGroups.

Percent - %(CodeFile.BatchNum): Allows you to reference batched ItemGroup values using metadata. This is a bit more complicated, so definitely review the documentation for more info.

Take a look at each link for more detailed info on how these are used. Good luck -- hope this helps!

like image 37
Indolent Coder Avatar answered Oct 17 '22 20:10

Indolent Coder