NuGet在构建开始之前恢复PostSharp包

Dav*_*New 7 msbuild postsharp visual-studio nuget nuget-package

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

据我所知,当通过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>
Run Code Online (Sandbox Code Playgroud)

.csproj 文件:

<RestorePackages>true</RestorePackages>
Run Code Online (Sandbox Code Playgroud)

据我了解,NuGet将在构建开始之前恢复丢失的包.第二个错误条件将基本上打破构建,完全没有任何理由.

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

Mat*_*ard 4

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

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

如果您使用基于 MSBuild 的包还原,则还原将在构建期间发生,此时不会导入 PostSharp 文件,因此 $(PostSharp30Imported) 将为空,并且将显示第二条错误消息。至少我怀疑情况确实如此。

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

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