调用可重复使用的msbuild目标时将列表项传递给属性 [英] Pass list item to Properties when calling reusable msbuild target

查看:105
本文介绍了调用可重复使用的msbuild目标时将列表项传递给属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照我一直在尝试传递我想解释为列表的属性.我还没有在网上找到可以解决这种情况的示例.据我了解,问题在于属性"已被视为列表项,因此它也不喜欢传递列表项.有没有办法让msbuild在这里正确打包和解压缩列表?

I'm stuck trying to pass a property that I want interpreted as a list. I haven't found an example online that deals with this situation. As I understand it, the problem is that Properties is already treated as a list item, so it doesn't like having a list item passed in as well. Is there a way to get msbuild to pack and unpack the list correctly here?

Msbuild抱怨:

Msbuild is complaining with:

error MSB4012: The expression "FilesToZip=@(Scripts)" cannot be used in this context. Item lists cannot be concatenated with other strings where an item list is expected. Use a semicolon to separate multiple item lists.

这是一个示例呼叫者:

<Target Name="BuildMigrationZip">

   <MSBuild Projects="BuildZip.msbuild"
      Targets="BuildZip"
      Properties="FilesToZip=@(Scripts);OutputZipFile=$(MigrationPackageFilePath);OutputFolder=$(MigrationPackagePath);Flatten=true"/>

  <Message Text="Created database migration zip: $(MigrationPackageFilePath)" Importance="high"/>

</Target>

基本目标:

<Target Name="BuildZip">

  <MakeDir Directories="$(OutputFolder)"/>

  <Zip Files="@(FilesToZip)" 
  ZipFileName="$(OutputZipFile)"
  Flatten="$(Flatten)"
  ParallelCompression="false" />

</Target>

虽然我想在此处打包许多拉链,但基本上我只是想将它们剪切并粘贴.

I'm basically at the point of just going back to cut and paste for these, although I want to package up a number of zips here.

更新:同一问题适用于在可重用目标上设置输入".到目前为止,我的问题是解决原始功能,但保持依赖关系正常运行会很好.例如:

UPDATE: The same issue applies to setting Inputs on the reusable target. My question up to this point addresses the raw functionality, but it would be nice to keep dependencies working. So for example:

<Target Name="BuildZip"
   Inputs="@(FilesToZip)"
   Outputs="$(OutputZipFile)">

  <MakeDir Directories="$(OutputFolder)"/>

  <Zip Files="@(FilesToZip)" 
  ZipFileName="$(OutputZipFile)"
  Flatten="$(Flatten)"
  ParallelCompression="false" />

</Target>

推荐答案

它们的关键是将列表作为属性传递.因此,当您的Scripts列表定义为

They key is to pass the list around as a property. So when your Scripts list is defined as

<ItemGroup>
  <Scripts Include="A"/>
  <Scripts Include="B"/>
  <Scripts Include="C"/>
</ItemGroup>

然后您首先将其转换为属性(该属性仅使分号分隔的项目,但msbuild知道如何通过MSBuild目标的Properties传递此属性),然后将其传递给目标:

then you first convert it into a property (which just makes semicolon seperated items, but msbuild knows how to pass this via the Properties of the MSBuild target) then pass it to the target:

<Target Name="BuildMigrationZip">
  <PropertyGroup>
    <ScriptsProperty>@(Scripts)</ScriptsProperty>
  </PropertyGroup>

  <MSBuild Projects="$(MSBuildThisFile)" Targets="BuildZip"
           Properties="FilesToZip=$(ScriptsProperty)" />
</Target>

(请注意,我在这里使用$(MSBuildThisFile):您不一定需要为每个目标创建单独的构建文件,实际上,对于像您这样的小型目标,将其放置在同一文件中要方便得多)

(note I'm using $(MSBuildThisFile) here: you don't necessarily need to create seperate build files for every single target, in fact for small targets like yours it's much more convenient to put it in the same file)

然后在目标目标中,将属性再次变成列表:

Then in your destination target you turn the property into a list again:

<Target Name="BuildZip">
  <ItemGroup>
    <FilesToZipList Include="$(FilesToZip)"/>
  </ItemGroup>
  <Message Text="BuildZip: @(FilesToZipList)" />
</Target>

输出:

BuildZip: A;B;C

更新

在使用Inputs时,您不能通过@(FilesToZip),因为它不能扩展为无,因为它不是列表:它是一个属性-碰巧是许多用分号分隔的字符串.因此,它对于Inputs可用,您只需将其扩展为它的属性即可,即$(FilesToZip):

When working with Inputs, you cannot pass @(FilesToZip) since that expands to nothing because is not a list: it's a property - which happens to be a number of semicolon-seperated strings. And as such, it is usable for Inputs you just have to expand it as the property it is i.e. $(FilesToZip):

<Target Name="BuildZip"
        Inputs="$(FilesToZip)"
        Outputs="$(OutputZipFile)">
  ...
</Target>

第二次运行的输出:

BuildZip:
Skipping target "BuildZip" because all output files are up-to-date with respect to the input files.

这篇关于调用可重复使用的msbuild目标时将列表项传递给属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆