Consuming nuget package containing .targets file via PackageReference

你离开我真会死。 提交于 2020-07-23 04:51:54

问题


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.

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).

Eg:

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

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.

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

So question:

  • 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)

回答1:


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

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.



来源:https://stackoverflow.com/questions/55279468/consuming-nuget-package-containing-targets-file-via-packagereference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!