问题
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