我正在编写一个NuGet包,其中将包含MSBuild .targets和.props文件,如nuget文档所述(需要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集成包恢复脚手架。我已按照文档手动删除了此脚手架,但当我将项目提交到构建服务器时,自动包恢复似乎会重新添加脚手架,导致构建失败。
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文件的问题。当添加到项目中时,这个新包本身包含一个依赖于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>