简体   繁体   中英

What's the difference between @ and $ and % in MSBuild?

What are the differences when referring to variables in MSBuild. For example in the following there is a @ and $ and as well as a % used.

<Copy SourceFiles="@(Files)" DestinationFolder="$(TempBuildDir)\%(RecursiveDir)">
   <Output TaskParameter="CopiedFiles" ItemName="DeployFiles" />
</Copy>

$ denotes access to a property (some kind of variable that contains a simple value)

@ is for item, which typically is a group of files with attached metadatas under a name

% denote an access to a metadata of an item. There are wellknown metadatas (like RecursiveDir, see the definition in msdn) automatically attached to an item, or you can attach your own metadata when you define your items

lets say you define @(files) like this:

<ItemGroup>
   <Files include='c:\source\**\*.*'>  <!-- all files in all subfolder in c:\source -->
     <Color>Blue</Color> <!-- attach metadata color = 'Blue' to these files -->
   </Files>
   <Files include='c:\source2\**\*.*'>  <!-- all files in all subfolder in c:\source2 -->
     <Color>Red</Color> <!-- attach metadata color = 'Red' to these files -->
   </Files>
</ItemGroup>

if c:\\source contains files 1.txt, b/2.dll , c/3.xml, and c:\\source2 contains a/4.exe, @(Files) is formed like this

  • file c:\\source\\1.txt, with metadata color = 'Blue' and RecursiveDir = ''

  • file c:\\source\\b\\2.dll, with metadata color = 'Blue' and RecursiveDir = 'b'

  • file c:\\source\\c\\3.xml, with metadata color = 'Blue' and RecursiveDir = 'c'

  • file c:\\source2\\a\\4.exe, with metadata color = 'Red' and RecursiveDir = 'a'

If you define TempBuildDir like this

<PropertyGroup>
 <TempBuildDir>c:\temp<TempBuildDir>
</PropertyGroup>

You have some kind of variable that contains a simple value : c:\\temp

Your examples reads like this : copy every files defined in item File in a directory that is formed by concatening value of variable TempBuildDir with the Recursive directory where you found the file.

You end up with :

  • c:\\temp\\1.txt

  • c:\\temp\\b\\2.dll

  • c:\\temps\\c\\3.xml

  • c:\\temp\\a\\4.exe

Here is the complete list of the special characters:

http://msdn.microsoft.com/en-us/library/bb383819.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM