2016-12-13 44 views
2

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

+1

如果您在编译之前知道文件路径,则可以使用BeforeTargets =“AssignTargetPaths”添加目标,并在其中填充Content ItemGroup,如果CopyToOutputDirectory元数据设置为true,它将被复制到依赖项目。编译后我不会立即知道如何做到这一点。 – stijn

回答

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中,当一个相关项目调用我们时,我们会收到提示。

+0

请注意,MyTarget中的Copy任务不是必需的,它只是假装在第一个项目的输出中“生成”1.txt。 –

相关问题