如果 ItemGroup 包含项目,如何进行 MSBuild 条件测试?

发布于 2024-12-07 01:01:29 字数 306 浏览 0 评论 0原文

这应该很简单,但我找不到如何做到这一点(或者也许这是不可能的)。

在 MSBuild 中,我有一个 ItemGroup,它是文件列表。 我只想在特定文件位于该 ItemGroup 中时才执行任务,

例如:

<Copy Condition="@(Files) <contains> C:\MyFile.txt" .... />

有什么方法可以做到这一点吗?最好不编写自定义任务。

编辑:文件列表仅与条件有关。否则与任务无关。

This should be simple, but I can't find how to do this (or maybe it's not possible).

In MSBuild I have an ItemGroup that is a list of files.
I want to execute a task only if a particular file is in that ItemGroup

Something like:

<Copy Condition="@(Files) <contains> C:\MyFile.txt" .... />

Any way to do this? Preferably without writing a custom task.

Edit: The list of files is only to do with the condition. Otherwise it has no relation to the task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-12-14 01:01:29

尝试

<复制条件="'%(Files.Identity)' == 'C:\MyFile.txt'" ../>

Try

<Copy Condition="'%(Files.Identity)' == 'C:\MyFile.txt'" .. />

扬花落满肩 2024-12-14 01:01:29

我遇到了类似的问题,如果文件未包含在 itemGroup 中,我想做一些事情。就我而言,它是 项组。

此代码片段扫描标记中的所有文件,对于每个文件,如果文件的 FullPath 与 TheFileIAmLookingFor 匹配,则我设置一个属性来指示已找到该文件。

然后我可以使用该属性,如果未设置该属性,我可以假设该项目未包含在 项目组中。就我而言,最后的操作是仅在项目不存在时才添加该项目。对于上下文,我在通过自定义 msbuild 构建任务进行代码生成时遇到了这个问题,并且生成的代码有时会被引用两次,这会引发警告。

  <Target Name="CheckForFile" BeforeTargets="CoreCompile"">
    <PropertyGroup>
        <DoesMyFileExist Condition="%(Compile.FullPath) == $(TheFileIAmLookingFor)">1</DoesMyFileExist>
    </PropertyGroup>

    <Message Importance="high" Condition="$(DoesMyFileExist)=='1'" Text="It exists"/>
    <Message Importance="high" Condition="$(DoesMyFileExist)!='1'" Text="It does not exist!"/>
    
    <ItemGroup Condition="$(DoesMyFileExist)!='1'">
      <Compile Include="$(TheFileIAmLookingFor)" />
    </ItemGroup>
  </Target>

I had a similar issue, where I wanted to do something if a file was not included in an itemGroup. In my case, it was the <Compile> item group.

This snippet is scanning through all the files in the tag, and for each one, if the file's FullPath matches the TheFileIAmLookingFor, then I set a property indicating the file was found.

Then I can use that property, and if it isn't set, I can assume the item wasn't included in the <Compile> item group. In my case, the final action is to add the item only if it didn't exist. For context, I came across this issue while working on code-generation via custom msbuild build tasks, and the generated code was sometimes getting referenced twice, which was throwing a warning.

  <Target Name="CheckForFile" BeforeTargets="CoreCompile"">
    <PropertyGroup>
        <DoesMyFileExist Condition="%(Compile.FullPath) == $(TheFileIAmLookingFor)">1</DoesMyFileExist>
    </PropertyGroup>

    <Message Importance="high" Condition="$(DoesMyFileExist)=='1'" Text="It exists"/>
    <Message Importance="high" Condition="$(DoesMyFileExist)!='1'" Text="It does not exist!"/>
    
    <ItemGroup Condition="$(DoesMyFileExist)!='1'">
      <Compile Include="$(TheFileIAmLookingFor)" />
    </ItemGroup>
  </Target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文