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.
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.
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.
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.
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).
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:
dotnet build the.sln -c Release
dotnet test -c Release --no-build
dotnet publish the\app.csproj --no-build -c Release
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With