Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "dotnet pack" an already compiled project

I'm trying to design a build script for a dotnet core 2.0 project that does the following actions

  1. Cleans output directories
  2. Builds Solution with -o bin\Publish
  3. Runs Unit Tests with dotnet vstest
  4. Creates a Nuget package with dotnet pack

Because I know the source code has been built and tested in steps 1-3 I do not want to rebuild my code for the nuget package, so I am specifying --no-build and --no-restore

The difficulty I'm having is that when creating the package, because I am not building and the output directory is set to be bin\Publish - the pack command is looking for items in the bin\Debug directory.

Is there a way I can set the dotnet pack command to know where to look for the already compiled objects?

Here is a sample of my build script

dotnet clean ..\MySolution.sln -o bin/Publish/
dotnet build ..\MySolution.sln /p:Configuration=Release -o bin/Publish/
dotnet vstest ..\MySolution.UnitTests\bin\Publish\MySolution.UnitTests.dll
dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\

....

error : The file 'D:\MySolution.ConsoleApp\bin\Debug\netcoreapp2.0\MySolution.ConsoleApp.runtimeconfig.json' to be packed was not found on disk

According to the Microsoft Docs on dotnet pack

"By default, dotnet pack builds the project first. If you wish to avoid this behavior, pass the --no-build option. This is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built."

So I'm hoping that this is possible and I'm missing something obvious. Any help would be appreciated.

like image 800
Joe van de Bilt Avatar asked May 08 '18 11:05

Joe van de Bilt


People also ask

Does dotnet pack also build?

By default, dotnet pack builds the project first. If you wish to avoid this behavior, pass the --no-build option. This option is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built.

Does dotnet build do a restore?

You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new , dotnet build , dotnet run , dotnet test , dotnet publish , and dotnet pack . To disable implicit restore, use the --no-restore option.


1 Answers

The reason for this is that the dotnet pack --no-build option will try to use the previously built output, but can't find it since you built to a non-standard output path and the pack logic needs to locate some assets generated into the build output.

The -o options are internally different for the pack and build commands but you can change the pack command to:

dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\ /p:OutputPath=bin\Publish\

This will use the output built into the bin\Publish directory and put it into the Nuget output directory in bin\Publish\Nuget\

like image 73
Martin Ullrich Avatar answered Sep 28 '22 08:09

Martin Ullrich