通过 PackageReference 使用包含 .targets 文件的 nuget 包 [英] Consuming nuget package containing .targets file via PackageReference

查看:43
本文介绍了通过 PackageReference 使用包含 .targets 文件的 nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET452 项目 - 让我称它为 Consumer.csproj 我想使用 nuget 让我们称它为 SharedTargets 包含一些自定义目标文件(SharedTargets.targets) 来自 msbuild.

I have .NET452 project - lets call it Consumer.csproj that I want to consume nuget lets call it SharedTargets that contained some custom targets files (SharedTargets.targets) from msbuild.

我正在使用 PackageReference 格式,现在(与以前相比)nuget 包正在恢复到共享文件夹 (%userprofile%.nuget\packages),而我不是确定通过那个引用它是否是个好主意(感觉不对).

I'm using PackageReference format and now (compared to what it used to be) nuget packages are being restored to shared folder (%userprofile%.nuget\packages), and I'm not sure if it is good idea to reference it via that (doesn't feel right).

例如:

<PackageReference Include="SharedTargets">
  <Version>1.0</Version>
</PackageReference>
<Import 
  Project="$(USERPROFILE)\.nuget\packages\SharedTargets\1.0\SharedTargets.targets"
/>

这也只适用于 VS,从命令行运行它 (msbuild) 我遇到了鸡蛋问题:

Also this works only in VS, running this from command line (msbuild) I'm getting chicken-egg problem:

确认声明中的路径是正确的,并且该文件存在于磁盘上.

Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

显然是因为我需要先恢复nuget才能使用它:)

Obviously since I need to restore nuget first before I can use it :)

那么问题:

  • 是否有一些更优雅的方法来解析项目文件中 nuget 包的路径
  • 有没有办法使 msbuild 成功(即在导入 SharedTargets.target 之前恢复包)
  • is there some more elegant way how to resolve path to the nuget package inside project file
  • is there a way how to make msbuild succeed (i.e. restore packages before SharedTargets.target is imported)

推荐答案

您不应该尝试手动导入通过 NuGet 分发的目标.

You shouldn't try to manually import targets distributed via NuGet.

将您的 .targets 文件放在包内的 build 子文件夹中,并将其命名为 SharedTargets.targets(包 id + .targets)和NuGet 将自动包含目标 - 对于 packages.config 项目,它将在安装时修改项目文件,对于 PackageReference 项目,将通过修改 obj\ 目录中隐式生成的目标文件来导入目标.

Put your .targets file inside a build subfolder inside the package and name it SharedTargets.targets (package id + .targets) and NuGet will automatically include the targets - for packages.config projects it will modify the project file on install and for PackageReference projects the targets will be imported by modifying an implicitly generated targets file in the obj\ directory.

这篇关于通过 PackageReference 使用包含 .targets 文件的 nuget 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆