3

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)
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89

1 Answers1

6

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.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Thanks, that worked :) If you're up to I have a follow up question how to correctly reference .cs files from within such targets file - ref: https://stackoverflow.com/questions/55286717/targets-file-withing-nuget-how-to-reference-content-cs-files – Ondrej Svejdar Mar 21 '19 at 18:10