【问题标题】:NuGet restoring PostSharp package before the build begins在构建开始之前 NuGet 恢复 PostSharp 包
【发布时间】:2014-06-26 19:09:04
【问题描述】:

我正在使用 PostSharp,我的项目文件中有以下目标描述:

<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
  <Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://www.postsharp.net/links/nuget-restore." />
  <Error Condition="Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>

据我了解,这是在通过NuGet引用PostSharp时添加到项目中的,错误情况检查如下:

  • 当 PostSharp 不可用时,第一个错误条件会中断构建(即 NuGet 未成功恢复它)。
  • 第二个错误条件在最后一次构建中由 NuGet 成功恢复 PostSharp 但因此未包含在项目中时会中断构建,因此需要重新构建。

但是,如果我在NuGet.Config.csproj file 中有以下配置,那么第二个错误条件是否必要?

NuGet.Config文件:

<configuration>
  <packageRestore>
    <!-- Allow NuGet to download missing packages -->
    <add key="enabled" value="True" />
    <!-- Automatically check for missing packages during build in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
  ...
</configuration>

.csproj文件:

<RestorePackages>true</RestorePackages>

据我了解,NuGet 将在构建开始之前恢复丢失的包。第二个错误条件基本上会无缘无故地破坏构建。

注意:我使用的是 Visual Studio 2013 和 NuGet 2.8。

【问题讨论】:

    标签: visual-studio msbuild nuget nuget-package postsharp


    【解决方案1】:

    这取决于还原的完成方式以及您安装的 NuGet 版本。看起来错误消息试图涵盖三种情况:

    1. 在未启用基于 MSBuild 的包还原的情况下进行构建(通过右键单击解决方案并选择启用包还原在 Visual Studio 中进行配置)。
    2. 在未启用基于 MSBuild 的包还原时在 Visual Studio 外部构建。
    3. 使用旧版本的 NuGet 通过 Visual Studio 构建,该版本不支持在构建前自动恢复。

    如果您使用的是基于 MSBuild 的包恢复,则在构建期间将进行恢复,此时不会导入 PostSharp 文件,因此 $(PostSharp30Imported) 将为空,并显示第二条错误消息。至少我怀疑是这样的。

    如果您从命令行构建而不使用基于 MSBuild 的包还原,那么如果 NuGet 包丢失,您将看到第一条错误消息。

    如果您没有使用基于 MSBuild 的包还原,而是在 Visual Studio 中使用最新版本的 NuGet 进行构建,那么在构建任何内容之前将还原包是正确的。因此,在执行之前,MSBuild 应该可以使用 PostSharp 导入。

    【讨论】:

      【解决方案2】:

      由于在 msbuild 加载期间需要 PostSharp dll(因此引用此 dll 的目标在构建期间可用)它们必须在最终调用 msbuild 期间可用。

      虽然在 VS 中单击构建两次是可以接受的,但我在 CI 环境中使用 PostSharp,并且两次调用构建解决方案的要求令人沮丧(第一次构建恢复 nugets,但由于错误也导致构建失败)。

      我最终得到了单独的构建步骤:

      1. 恢复 nuget 包(这会下载 PostSharp 包并将成功代码返回到环境): NuGet.exe restore SolutionWithProjectsUsingPostSharp.sln
      2. 构建解决方案。

      【讨论】:

        【解决方案3】:

        您需要在csproj中编辑目标中的第二个错误条件



        http://www.postsharp.net/links/nuget-restore。" />
        http://www.postsharp.net/links/nuget-restore。” />

        目标>>

        我已经在 SO 的 different post 中详细回答了

        【讨论】:

          【解决方案4】:

          我们正在使用“旧”的 MSBuild-Integrated 包还原(存在 .nuget\NuGet.targets 文件)并且通常不存储在源代码控制包中,而是依赖构建来为每个构建恢复它们。

          但是对于 TeamCity 构建服务器上的 PostSharp,我遇到了错误:

          构建恢复了 NuGet 包。再次构建项目以包含 构建中的这些包。

          最简单的方法是在源代码管理包中显式包含\PostSharp.VerXXX。

          另外的解决方案可以是migrating to automatic package restore,
          正如Issue Using PostSharp 3.x with NuGet Auto Restore中所建议的那样

          【讨论】:

            【解决方案5】:

            右击解决方案,Manage Nuget packages;并删除你不想要的包。

            在尝试从 Web 恢复包时,也会出现此错误。只需将您自己连接到互联网,然后尝试打开该项目。

            按照上述步骤,错误消失了。

            【讨论】:

            • 与问题无关的答案。
            猜你喜欢
            • 2021-11-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多