如何使MSBuild正确地跟踪引用项目中使用外部工具生成的文件?
问题描述:
我有一个MSBuild代码,它将文件与特定的构建操作(本示例中为CompileFoo
)并生成输出文件(具有不同的扩展名)。这是我到目前为止的代码:如何使MSBuild正确地跟踪引用项目中使用外部工具生成的文件?
<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引用的DLL(C#和一个程序集引用),并且我需要从这些生成的项目(示例中的.bin
文件)输出目录下的DLL,放到EXE的输出目录下。
我试图得到类似这样的效果的东西,当发生在DLL项目是:
<Content Include="Test\Test.txt"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></Content>
在这种情况下,Test\Test.txt
文件中的EXE的输出文件夹结束。虽然我不确定这是不是一回事。 (它是从原始文件,还是从DLL输出文件夹中的一个复制?)
我试图让某些东西相当兼容 - 具体来说,这将在VS2010和VS Mac上工作。
答
这里的技巧是让GetCopyToOutputDirectoryItems
目标返回一个额外AllItemsFullPathWithTargetPath
项目:(使用testet当前的MSBuild 15版)
<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>
(编辑后的版本与VS2010 -AR测试)
谢谢!我必须做一些小的修改才能让VS2010正确处理它,并且稍微修复'TargetPath'目标。 –