I have a netstandard2.0 csproj (let's call it MyPackage) that is packed at build time (as specified by GeneratePackageOnBuild) into a nuget package. This nuget package has custom props and targets in the build directory (so referencing projects get these imported).
I have another project (let's call it MyConsumer) in the same solution for testing MyPackage. I want MyConsumer to have the build asset props and targets imported from MyPackage at build time, just as if it were consuming it as a PackageReference from some remote nuget source.
How can I get this working (most simply)?
I have been able to do it via a very convoluted method where I have MyConsumer add a PackageReference to MyPackage and override the RestoreSources in MyConsumer to point to the bin directory of MyPackage. This gets very weird when running dotnet build or Visual Studio build of the sln, because project metadata is generated upfront for all projects during Restore and thus MyPackage doesn't exist at that point. The resolution was to add nested calls to MSBuild within the MyConsumer project, but then this becomes even worse, since Visual Studio restores operate quite differently than that automatic restores performed by dotnet build.
Is there any simple way of doing this?
This is what I have now
<Project>
<Target Name="Build">
<Message Text="Running inner build" Importance="high" />
<!--
Need to call MSBuild twice, once to restore, then again to restore and build to get the restore of the Sdk to work
because of this bug in MSBuild: https://github.com/Microsoft/msbuild/issues/2455
Note the trailing Prop=1 is required to get MSBuild to invalid it's cache of the project target imports
-->
<MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Restore" Properties="Configuration=$(Configuration);Version=$(Version);IsInnerBuild=true;Prop=1" />
<!-- Have to use dotnet build instead of another call to MSBuild because of another bug that prevents proper imports within the same physical process -->
<Exec Command="dotnet build /p:Configuration=$(Configuration) /p:Version=$(Version) /p:IsInnerBuild=true" />
<Message Text="Finished inner build" Importance="high" />
</Target>
<Target Name="Restore" />
<Target Name="RemoveBin">
<RemoveDir Directories="bin" />
</Target>
<!-- Don't do real cleans old rebuild since it breaks MSBuild due to the same above bug -->
<Target Name="Rebuild" DependsOnTargets="RemoveBin;Build">
</Target>
</Project>
If I understand you correct, you want to generate the package with project
MyPackage
, then install it to the test projectMyConsumer
and have the build asset props and targets imported from MyPackage at build time.To accomplish this goal, you need to complete the following few things:
MyPackage
build before the projectMyConsumer
.MyPackage.nupkg
to the test projectMyConsumer
during the build time.Details for above:
Since you wan to test the package which generated by the project
MyConsumer
, you should make sure this package grnerate before test project using it, so we need set the projectMyConsumer
reference the the projectMyPackage
.You can use a post-build event for the project
MyPackage
to copy the packageMyPackage.nupkg
to the local feed, or you can just add the bin directory ofMyPackage.nupkg
to the package source.Using VS 2017 and the
PackageReference
style of the test projectMyConsumer
, you can set aDirectory.Build.props
file into the root of your solution containing the test projectMyConsumer
you need:This will add these NuGet packages to the test project
MyConsumer
in the solution, it will be used as a PackageReference from some remote nuget source.Check the Martin`s answer for some more details.
Hope this helps.