3

我正在编写一个 nuget 包,该包将根据 nuget 文档包含 MSBuild .targets 和 .props 文件需要 nuget 2.5+)。

我打算将它与自动包还原一起使用。

当我将生成的包添加到 csproj 时,会发生两件事:

正如预期的那样:

<Import Project="..\packages\MyPackage.1.0.0\build\net40\MyPackage.props" Condition="Exists('..\packages\MyPackage.1.0.0\build\net40\MyPackage.props')" />
...
...
<Import Project="..\packages\MyPackage.1.0.0\build\net40\MyPackage.targets" Condition="Exists('..\packages\MyPackage.1.0.0\build\net40\MyPackage.targets')" />

不期望:

<PropertyGroup>
<RestorePackages>true</RestorePackages>
...
</PropertyGroup>
...
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  ...
</Target>

即“未预期”的行为是 MSBuild 集成包还原脚手架。

我已经根据文档手动删除了这个脚手架,但是当我将项目提交到构建服务器时,automaitc-package-restore 似乎重新添加了脚手架,导致构建失败。

This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is D:\TFSBuilds\...\.nuget\NuGet.targets.

当包包含 msbuild 目标时,是否有人有办法阻止/防止包含此脚手架?

更新

代替解决方案,我尝试了一种解决方法。也就是说,我创建了另一个 nuget 包来解决丢失的 nuget.targets 文件。当添加到项目中时,这个新包本身包含一个 DependsOnTargets EnsureNuGetPackageBuildImports 的目标,并创建一个空白/有效的 nuget.targets。这满足了从csproj脚手架触发的条件,同时允许nuget自动包还原功能仍然在构建服务器上触发。

<Target Name="NuGetPackAutomaticPackageRestorePreBuildWorkaround" BeforeTargets="EnsureNuGetPackageBuildImports"  Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')">
  <ItemGroup>
    <PatchTargets Include="$(MSBuildThisFileDirectory)TargetPatches\*.targets"/>
  </ItemGroup>
  <Copy
      SourceFiles="@(PatchTargets)"
      DestinationFolder="$(SolutionDir)\.nuget\"/>
</Target>
4

1 回答 1

0

解决方案所在的文件夹是否有.nuget文件夹?如果是这样,请尝试删除它。

我正在调整一个使用旧的(nuget 2.7 之前)包还原方式的解决方案,并且在我删除该文件夹之前遇到了同样的问题。

于 2015-07-22T21:26:59.247 回答