I'm attempting to migrate to using MSBuild Pack support for using .csproj to generate a projects NuGet package where during development local .dll's are used to build the project but they need to be replaced/swapped to reference an external NuGet package in the generated .nuspec when Using MSBuild to "pack" the project.
The closest documented example of this use-case I could find is Replacing one library from a restore graph where it suggests you can replace an external NuGet reference:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1">
<ExcludeAssets>All</ExcludeAssets>
</PackageReference>
Which overrides the package to reference to a local .dll instead:
<Reference Include="Newtonsoft.Json.dll" />
I'm trying to do something similar where the project should build against local .dll's
that were injected as an artifact from a dependent TeamCity/CI build:
<Reference Include="..\..\lib\net45\ServiceStack.Interfaces.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Text.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Common.dll" />
But when using ExcludeAssets=All
as per the documentation:
<PackageReference Include="ServiceStack.Common" Version="5.0.0">
<ExcludeAssets>All</ExcludeAssets>
</PackageReference>
The PackageReference doesn't get exported in the generated dependency list, e.g:
<group targetFramework=".NETFramework4.5" />
The closest I've come to getting the preferred behavior is to use ExcludeAssets="compile"
so my local project doesn't build against it, except this behavior also gets exported in the .nuspec:
<group targetFramework=".NETFramework4.5">
<dependency id="ServiceStack.Common" version="5.4.0" exclude="Compile,Build,Analyzers" />
</group>
I only want to avoid building against it locally but have it exported as a normal dependency. The other issue with this approach is that I need it to reference a package that hasn't been published to NuGet yet (as all packages are published in lock-step), e.g:
<!-- v5.5.0 is the new version to publish -->
<PackageReference Include="ServiceStack.Common" Version="5.5.0" ExcludeAssets="compile"/>
The build fails that it can't find the unpublished package:
[NU1102] Unable to find package ServiceStack.Common with version (>= 5.5.0)
Effectively I need someway to "inject" the exact dependencies and version I need in the generated .nuspec so it's included in the generated .nuspec dependency list, e.g:
<group targetFramework="net45">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
<group targetFramework=".netstandard2.0">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
How can we inject a custom dependency in MSBuild generated .nuspec?
Is there some way I can manually declare <dependency/>
as above so it's only used when MSBuild/NuGet packs the project? Otherwise is there a way to apply some XML transform to the generated .nuspec so I can manipulate the XML in the .nuspec before it's packed/compressed?
Basically I'm only interested in injecting dependencies when the "pack" target is run so it's ignored/inert in all other targets.