Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why bother with dotnet build before dotnet publish?

Welp. This seems like the type of question where I'll facepalm seeing the answer.

So...

Why bother with dotnet build before doing a dotnet publish?

build automatically does a restore. Cool.

It seems publish does a build (unless you tell it not to). So... Why bother doing the build if you're going to publish right after? Why not just publish and everything happens in one step?

For further clarity...

I am asking in a basic scenario like:

  • dotnet build -c Release MyProj
  • dotnet publish -c Release -o /somedir MyProj

versus just

  • dotnet publish -c Release -o /somedir MyProj

They seem to do the same thing.

like image 648
Josh Avatar asked Sep 07 '19 21:09

Josh


People also ask

Do I need to run dotnet build before publish?

You are right that dotnet publish automatically does everything dotnet build already does. In most cases - as in your scenario mentioned in the question - that means an additional dotnet build is not necessary. Do note that you can dotnet build a solution, but should only dotnet publish individual project files.

What is the difference between dotnet build and dotnet publish?

Build compiles the source code into a (hopefully) runnable application. Publish takes the results of the build, along with any needed third-party libraries and puts it somewhere for other people to run it.

What does dotnet publish do?

dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. The output includes the following assets: Intermediate Language (IL) code in an assembly with a dll extension.

Does dotnet publish create web config?

Bookmark this question. Show activity on this post. dotnet publish -c Release doesn't create this web. config , so I created itself and put it in the solution (odd, because I've never had to do this before).


1 Answers

You are right that dotnet publish automatically does everything dotnet build already does. In most cases - as in your scenario mentioned in the question - that means an additional dotnet build is not necessary.

Do note that you can dotnet build a solution, but should only dotnet publish individual project files. Publishing solutions likely leads to unexpected results (from overriding files of different versions to publishing library projects in configurations that should not be published to the same output directory as referencing applications etc.)

Over time there was a community ask to allow both publishing and testing without potentially rebuilding the app, because some users felt more comfortable only ever publishing an application with the same binaries that have been tested so their build scripts can look like:

  1. dotnet build the.sln -c Release
  2. dotnet test -c Release --no-build
  3. dotnet publish the\app.csproj --no-build -c Release
like image 60
Martin Ullrich Avatar answered Sep 29 '22 16:09

Martin Ullrich