Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

Consuming nuget package containing .targets file via PackageReference

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:

Upvotes: 5

Views: 2860

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

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.

Upvotes: 6

Related Questions