toebens
toebens

Reputation: 4089

Using project reference instead of nuget package while developing?

we have a project A (csproj, netstandard2.0) that we would like to release as a nuget package.

We want to use this package in project B using <packageReference> (csproj, .net framework 4.7.2) to speed up builds on our build server. Because we won't need to build project A before build of project B.

To ease development however i would like to have a solution that includes both projects, so that i can edit the source of project A and (re-)compile so that project B makes use of project A's latest version (without the need to pack a new nupkg of project A (and publish it on our nuget package source server if possible)). (While still using nuget on our build server)

How can this be archieved?

I searched the web and stackoverflow but couldn't find a way how this is intended to work.

Thanks

Upvotes: 4

Views: 2395

Answers (2)

Ariel Moraes
Ariel Moraes

Reputation: 648

I know the question is a bit old, but I am leaving this answer to help future peers.

Leverage the dotnet pack command, when using it you can add a direct reference to a csproj. The only thing you need to be sure of is to use the property IsPackable and not generate a package on the build process. After that, the pack command will change the direct reference to a package reference. For more information check the dotnet runtime repo on how it's used.

The documentation for the pack command can be found here: https://learn.microsoft.com/pt-br/dotnet/core/tools/dotnet-pack

Upvotes: 0

Ross Gurbutt
Ross Gurbutt

Reputation: 1029

As @Zivkan says, this is a bit of an odd thing to do for a number of reasons, but if you want to try it, I think the following would work:

You could use a conditional statement in your project so that when built locally it uses a project reference, but when built on the build machine you specify a property in the command line to use a package reference instead.
e.g.

<Choose>
  <When Condition="'$(IsBuildServer)' == 'true'">
    <ItemGroup>
      <PackageReference Include="A" Version="*" />
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <ProjectReference Include="relativepath\A.csproj" />
    </ItemGroup>
  </Otherwise>
</Choose>

And then when building on the build server, just make sure to specify -p:IsBuildServer=true as a command line argument to msbuild / dotnet build.

The "*" in the package reference just means take the latest available stable package, but may not be exactly what you want depending on what exactly you want the build server to build.

Upvotes: 2

Related Questions