.net - 如何使 MSBuild 在引用的项目中正确跟踪使用外部工具生成的文件?

标签 .net visual-studio visual-studio-2010 msbuild visual-studio-mac

我有 MSBuild 代码,它使用特定的构建操作(本例中为 CompileFoo)获取文件并生成输出文件(具有不同的扩展名)。这是我到目前为止的代码:

<Target Name="BuildFoo" BeforeTargets="Compile"
    Inputs="@(CompileFoo)"
    Outputs="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )" >

    <!-- makefoo doesn't know how to create directories: -->
    <MakeDir Directories="$(OutputPath)%(CompileFoo.RelativeDir)"/>
    <Exec Command="makefoo -o &quot;$(OutputPath)%(CompileFoo.RelativeDir)%(CompileFoo.Filename).bin&quot; &quot;%(CompileFoo.Identity)&quot;" />

    <ItemGroup>
        <!-- Required so we can handle Clean: -->
        <FileWrites Include="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )"/>
    </ItemGroup>
</Target>

如果我将它包含在生成最终 EXE 的项目中,效果会非常好。

但现在我想让它在一个生成由 EXE(具有程序集引用的 C#)引用的 DLL 的项目中工作,并且我需要获取这些生成的项目(.bin示例中的文件)从DLL的输出目录到EXE的输出目录。

当它出现在 DLL 项目中时,我试图获得与此类似的效果:

<Content Include="Test\Test.txt"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></Content>

在这种情况下,Test\Test.txt 文件最终位于 EXE 的输出文件夹中。尽管我不确定这是否完全相同。 (它是从原始文件复制还是从 DLL 输出文件夹复制?)

我正在尝试获得一些相当兼容的东西 - 特别是可以在 VS2010 和 VS Mac 上运行的东西。

最佳答案

这里的技巧是让 GetCopyToOutputDirectoryItems 目标返回一个额外的 AllItemsFullPathWithTargetPath 项:

<Target Name="IncludeFoo" BeforeTargets="GetCopyToOutputDirectoryItems">
  <ItemGroup>
    <CompiledFoos Include="@(CompileFoo -> '$(OutputPath)%(RelativeDir)%(Filename).bin' )">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>%(RelativeDir)%(FileName).bin</TargetPath>
    </CompiledFoos>
    <AllItemsFullPathWithTargetPath Include="@(CompiledFoos->'%(FullPath)')"  />
  </ItemGroup>
</Target>

(使用当前 MSBuild 15 版本的测试集)(使用 VS2010 -AR 测试的编辑版本)

关于.net - 如何使 MSBuild 在引用的项目中正确跟踪使用外部工具生成的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44752139/

相关文章:

c# - 禁用 ComboBox C# 中的项目

c# - “does not contain a definition…and no extension method..” 错误

c++ - 是否有 MSVC (Visual C++) 的 -Weffc++ 等价物?

visual-studio - Windows 8 Developer Preview 捆绑了哪个版本的 Visual Studio 11?

visual-studio - 如何在 Visual Studio 2017 RC 中从 .NET Core MSBuild 项目创建 NuGet 包?

visual-studio-2010 - 如何安装 Crystal Reports for VS2010?

c++ - 用 C++ 为非英语语言打印字符串

.net - 将控件映射到 View 模型

c# - 从另一个集合更新集合的有效方法

c# - 系统.Net.WebException : The operation has timed out on HttpWebResponse