如何使MSBuild在引用的项目中正确跟踪使用外部工具生成的文件? [英] How to make MSBuild correctly track files generated with an external tool across referenced projects?

查看:76
本文介绍了如何使MSBuild在引用的项目中正确跟踪使用外部工具生成的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MSBuild代码,该代码使用具有特定Build Action(在此示例中为CompileFoo)的文件并生成输出文件(具有不同的扩展名).这是我到目前为止的代码:

I have MSBuild code that takes files with a particular Build Action (CompileFoo in this example) and generates output files (with a different extension). This is the code I have so far:

<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的项目中,则效果很好.

This works great if I include it in a project that generates the final EXE.

但是现在我想使其在一个项目中工作,该项目生成一个由EXE(带有程序集引用的C#)引用的DLL,我需要从这些项目中获取这些生成的项(示例中的.bin文件) DLL的输出目录,进入EXE的输出目录.

But now I want to make it work in a project that generates a DLL that is referenced by the EXE (C# with an assembly reference) and I need to get those generated items (the .bin files in the example) from the output directory of the DLL, into the output directory of the EXE.

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

I am trying to get something similar to the effect of this, when it occurs in the DLL project:

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

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

In this case the Test\Test.txt file ends up in the output folder of the EXE. Although I am not sure that is quite the same thing. (Does it copy from the original file, or the one from the DLL output folder?)

我正在尝试获得相当兼容的东西-特别是可以在VS2010和VS Mac上使用的东西.

I'm trying to get something fairly compatible - specifically that will work on VS2010 and VS Mac.

推荐答案

此处的窍门是使GetCopyToOutputDirectoryItems目标返回附加的AllItemsFullPathWithTargetPath项目:

The trick here is to make the GetCopyToOutputDirectoryItems target return an additional AllItemsFullPathWithTargetPath item:

<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版本的Testet)(经VS2010 -AR测试的编辑版本)

(testet using a current MSBuild 15 version) (Edited version tested with VS2010 -AR)

这篇关于如何使MSBuild在引用的项目中正确跟踪使用外部工具生成的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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