MSBuild 不获取引用项目的引用

发布于 2024-07-07 06:38:36 字数 366 浏览 6 评论 0原文

我刚才在使用 MSBuild 时遇到了一个奇怪的情况。 有一个解决方案包含三个项目:LibX、LibY 和 Exe。 Exe 引用 LibX。 LibX 又引用 LibY,有一些内容文件,还引用第三方库(GAC 和本地 lib 文件夹中安装的几个预构建程序集)。 第三方库被标记为“复制本地”(“私有”)并出现在 LibX 项目的输出中,就像 LibY 的输出和 LibX 的内容文件一样。 现在,Exe 项目的输出具有 LibX 项目输出、LibX 项目的内容文件、LibY 项目输出(来自 LibX),但没有第三方库的程序集。

现在我通过直接在 Exe 项目中引用第三方库来解决这个问题,但我不认为这是一个“正确”的解决方案。

以前有人遇到过这个问题吗?

I bumped into a strange situation with MSBuild just now. There's a solution which has three projects: LibX, LibY and Exe. Exe references LibX. LibX in its turn references LibY, has some content files, and also references to a third-party library (several pre-built assemblies installed in both GAC and local lib folder). The third-party library is marked as "Copy Local" ("private") and appears in the output of the LibX project, as the LibY's output and LibX's content files do. Now, Exe project's output has LibX project output, content files of the LibX project, LibY project output (coming from LibX), but NO third-party library's assemblies.

Now I worked this around by referencing the third-party library directly in Exe project, but I don't feel this is a "right" solution.

Anyone had this problem before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

安静 2024-07-14 06:38:36

与使用 Visual Studio 构建相比,使用 MSBuild(即命令行、TFS Build 和其他工具)构建时的行为存在差异。 辅助引用不包含在发送到 MSBuild 编译任务的引用变量中。

MSBuild 提供了多个扩展点来更改引用的解析方式。 我已成功使用 AfterResolveReference 解决了我的一些项目的此问题 - 我已经在我的博客上发布了有关背景的更多信息

解决方法是将以下代码添加到 vbproj 或 csproj 文件中

  <Target Name="AfterResolveReferences">
    <!-- Redefine referencepath to add dependencyies-->
    <ItemGroup>
     <ReferencePath Include="@(ReferenceDependencyPaths)">
     </ReferencePath>
    </ItemGroup> 
  </Target>

Microsoft 已声明此问题无法修复 连接

There is a difference in behavior when building with MSBuild (i.e. command line, TFS Build and other tools) compared to building with Visual Studio. The secondary references are not included in the references variable sent into MSBuild compile tasks.

There are several extension points provided by MSBuild to change how references are to be resolved. I have successfully used AfterResolveReference to fix this issue for some of my projects - I have posted more info about the background on my blog.

The workaround is to add the following code into you vbproj or csproj files

  <Target Name="AfterResolveReferences">
    <!-- Redefine referencepath to add dependencyies-->
    <ItemGroup>
     <ReferencePath Include="@(ReferenceDependencyPaths)">
     </ReferencePath>
    </ItemGroup> 
  </Target>

Microsoft has stated that this is a won't fix on Connect

梦中的蝴蝶 2024-07-14 06:38:36

您实际上可以进入 Microsoft.CSharp.targets 或 Microsoft.VisualBasic.targets 文件(位于框架目录中,通常为 C:\Windows\Microsoft.NET\Framework\v3.5)并将 csc 或 vbc 任务参数修改为包括额外的参考依赖项。 在文件(VB 目标,第 166 行;C# 目标,第 164 行)中,将:\ 更改

References="@(ReferencePath)"

References="@(ReferencePath);@(ReferenceDependencyPaths)"

这可能会导致其他问题,具体取决于事情的复杂程度,并且它可能会与 Visual Studio inproc 编译器一起使用,但这是唯一的方法我在 MSBuild 中找到了它。

You can actually go into the Microsoft.CSharp.targets or Microsoft.VisualBasic.targets file (located in the framework directory, usually C:\Windows\Microsoft.NET\Framework\v3.5) and modify the csc or vbc task parameters to include additional reference dependencies. In the file (VB targets, line 166; C# targets, line 164) change:\

References="@(ReferencePath)"

to

References="@(ReferencePath);@(ReferenceDependencyPaths)"

This might cause other issues depending on how complicated things are and it may play tricks with the Visual Studio inproc compiler, but it's the only way to do it in MSBuild that I've found.

述情 2024-07-14 06:38:36

乔桑的回答几乎对我有用; 当我尝试这样做时,我在 Visual Studio 中不断收到错误:

尝试为 IDE 的进程内编译器设置“引用”参数时出现问题。 错误 HRESULT E_FAIL 已从对 COM 组件的调用返回

我的问题的解决方案是在 ItemGroup 上设置一个条件,如下所示:

<Target Name="AfterResolveReferences">
  <!-- Redefine referencepath to add dependencies-->
  <ItemGroup Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">
    <ReferencePath Include="@(ReferenceDependencyPaths)"></ReferencePath>
  </ItemGroup>
</Target>

这导致 Visual Studio 完全忽略引用更改,并且构建工作正常本地和构建服务器上。

josant's answer almost worked for me; I kept getting an error in Visual Studio when I tried that:

A problem occurred while trying to set the "References" parameter for the IDE's in-process compiler. Error HRESULT E_FAIL has been returned from a call to a COM component

The solution to my problem was to put a condition on the ItemGroup, like this:

<Target Name="AfterResolveReferences">
  <!-- Redefine referencepath to add dependencies-->
  <ItemGroup Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">
    <ReferencePath Include="@(ReferenceDependencyPaths)"></ReferencePath>
  </ItemGroup>
</Target>

That caused Visual Studio to ignore the reference change completely, and the build works fine locally and on the build server.

一花一树开 2024-07-14 06:38:36

是的,我也遇到过这个问题。 尽管我很想说,但我相信您必须将所有传递依赖项作为引用包含在构建文件中。

Yes, I've had that problem, too. Though I'd love to say otherwise, I believe you must include all transitive dependencies as references in your build file.

白馒头 2024-07-14 06:38:36

我结合了 Alex Yakunin 的 解决方案还可以复制本机 dll< /a>.

I've combined Alex Yakunin's solution with one that will also copy native dll's.

那支青花 2024-07-14 06:38:36

如果您得到的是有向图而不是树,并且出现“尝试部署 dll 的不同副本”错误,则 AfterResolveReferences 方法会失败。 (参见如何配置 msbuild/ MSVC部署依赖程序集的依赖文件

The AfterResolveReferences method fails if you've got a directed graph not a tree with a "trying to deploy different copies of the dll" error. (cf. How to configure msbuild/MSVC to deploy dependent files of dependent assemblies)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文