我有 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 "$(OutputPath)%(CompileFoo.RelativeDir)%(CompileFoo.Filename).bin" "%(CompileFoo.Identity)"" />
<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/