如何从MSBuild任务向项目输出文件添加文件

时间:2016-12-13 12:47:38

标签: msbuild msbuild-task

AfterTargets="AfterCompile"中运行的Give和MSBuild任务并生成一些文件如何将这些文件包含在当前项目输出中,以便将文件复制到引用该项目的任何项目的bin目录中

2 个答案:

答案 0 :(得分:0)

我无法保证这是正确的解决方案,但似乎有效:

<Target Name="MyTarget" AfterTargets="AfterCompile">
  <PropertyGroup>
    <MyInput>D:\1.txt</MyInput>
    <MyOutput>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)$(OutDir)\1.txt'))</MyOutput>
  </PropertyGroup>
  <Copy SourceFiles="$(MyInput)" DestinationFolder="$(OutDir)" SkipUnchangedFiles="true" />
  <ItemGroup>
    <AllItemsFullPathWithTargetPath Include="$(MyOutput)">
      <TargetPath>1.txt</TargetPath>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </AllItemsFullPathWithTargetPath>
  </ItemGroup>
</Target>

相关逻辑在这里: http://source.roslyn.io/#MSBuildTarget=GetCopyToOutputDirectoryItems http://source.roslyn.io/#MSBuildItem=AllItemsFullPathWithTargetPath

基本上我们依赖于以下事实:确定要从依赖项目复制的文件列表MSBuild调用依赖项目的GetCopyToOutputDirectoryItems目标并使用其输出(即AllItemsFullPathWithTargetPath)。

通过在最后一刻将我们自己添加到AllItemsFullPathWithTargetPath,当依赖项目调用我们时,我们会被拾取。

答案 1 :(得分:0)

谢谢,基里尔。那是一个很好的答案,当尝试从其他项目的输出中复制ETW清单文件时,它对我有帮助。下面是最终输出。

由于我只是简单地介绍了基里尔的答案,所以我不希望这个答案会被接受。我将其发布在这里,希望对其他人有所帮助。

<Target Name="IncludeEtwFilesInOutput"
        BeforeTargets="GetCopyToOutputDirectoryItems">

    <PropertyGroup>
        <EtwManifestFile>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)$(OutDir)\My.etwManifest.man'))</EtwManifestFile>
        <EtwResourceFile>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)$(OutDir)\My.etwManifest.dll'))</EtwResourceFile>
    </PropertyGroup>

    <ItemGroup>
        <AllItemsFullPathWithTargetPath Include="$(EtwManifestFile)">
            <TargetPath>My.etwManifest.man</TargetPath>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </AllItemsFullPathWithTargetPath>
        
        <AllItemsFullPathWithTargetPath Include="$(EtwResourceFile)">
            <TargetPath>My.etwManifest.dll</TargetPath>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </AllItemsFullPathWithTargetPath>
    </ItemGroup>
</Target>