Visual studio NuGet在构建开始前恢复PostSharp包

Visual studio NuGet在构建开始前恢复PostSharp包,visual-studio,msbuild,nuget,nuget-package,postsharp,Visual Studio,Msbuild,Nuget,Nuget Package,Postsharp,我正在使用PostSharp,我的项目文件中有以下目标描述: <Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''"> <Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This

我正在使用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>
.csproj
文件:

<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>
<RestorePackages>true</RestorePackages>
true
据我所知,NuGet将在构建开始之前恢复丢失的包。第二个错误条件实际上会无缘无故地破坏构建


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

这取决于恢复的方式以及安装的NuGet版本。错误消息似乎试图涵盖三种情况:

  • 在未启用基于MSBuild的包还原的情况下生成(在Visual Studio中通过右键单击解决方案并选择启用包还原进行配置)
  • 未启用基于MSBuild的包还原时在Visual Studio外部生成
  • 使用旧版本的NuGet使用Visual Studio生成,该版本不支持生成前的自动还原
  • 如果您使用的是基于MSBuild的包还原,则还原将在生成期间进行,此时不会导入PostSharp文件,因此$(PostSharp30Imported)将为空,并将显示第二条错误消息。至少我怀疑是这样

    如果从命令行生成而不使用基于MSBuild的包还原,则如果缺少NuGet包,则会看到第一条错误消息


    如果您没有使用基于MSBuild的包还原,并且正在使用最新版本的NuGet从Visual Studio中进行生成,则在生成任何内容之前将还原包是正确的。因此,在执行MSBuild之前,PostSharp导入应该对其可用;然后移除你不想要的包

    尝试从web还原包时,也会出现此错误。只需将自己连接到internet,然后尝试打开项目


    在执行上述步骤后,错误就消失了。

    因为在msbuild加载期间需要PostSharp DLL(因此在生成期间引用此DLL的目标可用),所以它们必须在最后调用msbuild期间可用

    在VS中,两次单击build是可以接受的,但我在CI环境中使用了PostSharp,两次调用解决方案上的build的要求令人沮丧(第一次build restore NUGET,但由于错误也导致build失败)

    我以单独的构建步骤结束:

  • 还原nuget软件包(这将下载PostSharp软件包并将成功代码返回到环境):
    NuGet.exe使用postsharp.sln还原项目解决方案
  • 构建解决方案

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






    >


    我已经在at SO中详细回答了,我们使用的是“旧”MSBuild集成包还原(.nuget\nuget.targets文件存在),通常不存储在源代码管理包中,而是依赖于生成来为每个生成还原它们

    但对于TeamCity build server上的PostSharp,我得到了一个错误:

    生成还原了NuGet包。再次构建项目以包括 这些包在构建中

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

    或者,解决方案可以是,

    正如

    中所建议的,答案与问题无关。