MSBuild 不将引用的本机项目的输出复制到 C# 项目的输出目录。

4

我已经困扰很久了。

背景:

  • c# project
  • c++ project
  • c# project has a reference for the c++ project with the following lines:

    <ProjectReference Include="projectB.vcxproj">
        <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
        <OutputItemType>Content</OutputItemType>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </ProjectReference>
    

这在Visual Studio中可以工作。 使用命令行中的devenv时可以工作。 但是,当使用命令行中的msbuild时,C++项目的输出文件未复制到C#项目的输出目录中。

我无法使用msbuild解决这个问题。阅读了很多相关资料,但没有一个有效的方法。尝试通过诊断详细信息来进行调试,但msbuild和visual-studio的日志格式非常不同... 由于构建机器上没有合法的Visual Studio,我不能使用devenv。

在msbuild日志中,使用诊断详细信息显示: Target "GetCopyToOutputDirectoryItems" skipped. Previously built successfully. 在Visual Studio日志中,此处看起来不同,并且实际上复制了引用的本地文件到C#输出目录中。 可能与构建顺序有关?..

在msbuild日志中,我还看到: Target "_CopyOutOfDateSourceItemsToOutputDirectoryAlways" skipped, due to false condition; ( '@(_SourceItemsToCopyToOutputDirectoryAlways)' != '' ) was evaluated as ( '' != '' ). 而在Visual Studio构建日志中,我看到此目标被执行(它紧随GetCopyToOutputDirectoryItems目标之后)。

1个回答

2

更新3: 似乎之前的解决方案会导致不必要的副作用,例如在运行多线程构建时破坏构建。

当前的解决方案是在ProjectReference部分中添加: <Targets>Build;BuiltProjectOutputGroup</Targets> 看起来是有效的。

更新2:

更改:

Targets="%(_MSBuildProjectReferenceExistent.Targets)"

Targets="%(_MSBuildProjectReferenceExistent.Targets);GetTargetPath"

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets中的MSBuild任务之前加上注释Build referenced projects when building from the command line. - 就可以了。

然而,我对这个解决方案没有信心,因为我不理解整个构建过程。这只是一个猜测。

更新1:

使用/p:DesignTimeBuild=true会影响依赖项的构建顺序。无法工作。继续调查...

可能的解决方案1:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets中添加了很多消息后,我最终得到了以下内容: QUIRKING FOR DEV10

我仍然不确定这是怎么回事,我看到开发人员计划删除这个怪癖(参见https://github.com/Microsoft/msbuild/issues/1890)。

突然在以下行中注意到了DesignTimeBuild

<Output TaskParameter="TargetOutputs" ItemName="_ResolvedProjectReferencePaths" Condition="'%(_MSBuildProjectReferenceExistent.ReferenceOutputAssembly)'=='true' or '$(DesignTimeBuild)' == 'true'"/>

我知道在Visual Studio内部可以工作。通过谷歌搜索,我找到了https://github.com/Microsoft/msbuild/wiki/MSBuild-Tips-&-Tricks。 从那里到msbuild命令行添加/p:DesignTimeBuild=true的路径就很短了。

这使它工作了。引用的程序集被复制过去了。

我不认为这应该是解决方案,但它起作用,并且似乎没有破坏其他任何东西(尚未)。

欢迎提出其他建议。


BuiltProjectOutputGroup 目标有文档吗? - yair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接