Compile multiple dotnet core projects in one step using dotnet cli

16,400

Solution 1

The dotnet build command accepts glob patterns. So you can do this:

dotnet build Src/**/project.json

Solution 2

There's no such a tool yet. Even KoreBuild, the tool that the ASP.NET team uses, goes blindly in each folder and invokes dotnet build/pack.

The nice thing is that dotnet build is now smart enough to not recompile the dependencies if they haven't changed, so that's not a problem anymore.

Solution 3

I had a similar requirement. This is my workaround:

@echo off
for /D %%d in (*) do (
    cd %%d  
    cd  
    dotnet restore
    dotnet build
    cd ..
)
exit /b

Solution 4

Use GNU Make. I use it to build my projects. all you have to do create a Makefile in your project root folder. You can nest Makefiles in directories and have a Top Level Makefile that runs the subdirectories. then you set up Makefiles for each of your "Sub Projects" folders and run any comandline tool. with dotnet core is is dotnet .

Wait... GNU - "GNU is not Unix" that's a Unix/Linux application... I run windows. Well the good news is you can do this is in windows. I'm using make.exe through my git-bash installation (git for windows). You will have to go find the cygwin port of make. (google: "make for git-bash") Then install it to your bin directory under the cygwin folder. You could also just install cygwin if you really wanted to.

The nice thing about using Gnu-Make is it is universal. Since dotnet core is platform agnostic, every environment Mac/FreeBSD/Linux have "make" most likely already installed. Adding it to your Windows machine and projects to me makes a lot of sense. Since you project can now be built by everyone the same way.

some of my projects need to build docker containers with dockerfiles, or snap packages, deploy to test, etc... Make (pardon the pun) makes it easy.

Here is a sample of simple projects Makefile. Running 'make' by itself is like saying 'make all' you could set up a command like 'cd ./subdir; make' as one of your .phoney directives. (Google: "Makefile documentation")

project_drive?=/c/prj
nuget_repo_name?=Local_Nuget_Packages
local_nuget_dir?=$(project_drive)/$(nuget_repo_name)

RELEASE_VERSION:= `grep "<Version>" *.csproj | cut -d '>' -f 2 | cut -d '<' -f 1`

.PHONEY: clean release test doc nuget install debug_nuget debug_install

all: doc MSBuild

test:
  ./test.sh

MSBuild: 
   dotnet build

clean:
   dotnet clean; dotnet restore

release: 
   dotnet build -c Release

doc:
   doxygen ./Doxyfile.config

nuget: release
   dotnet pack -c Release

install: 
   cp ./bin/Release/*.$(RELEASE_VERSION).nupkg $(local_nuget_dir)

debug_nuget: MSBuild
   dotnet pack 

debug_install: 
   cp ./bin/debug/*.$(RELEASE_VERSION).nupkg $(local_nuget_dir)
Share:
16,400
wal
Author by

wal

a cloud-based block chain enabled disruptive distributed ledger encompassing big data &lt;/sarcasm&gt; some handy things i keep forgetting find all services with name sc queryex type= service state= all | find /i "service-name" enable telnet client (from admin prompt) dism /online /Enable-Feature /FeatureName:TelnetClient generate self signed certificate that can be used to encrypt data New-SelfSignedCertificate -DnsName *.test.com -CertStoreLocation "Cert:\LocalMachine\My" -KeyUsage KeyEncipherment,DataEncipherment, KeyAgreement -Type DocumentEncryptionCert -KeySpec KeyExchange

Updated on July 03, 2022

Comments

  • wal
    wal almost 2 years

    Assuming this folder structure

    SampleApp
            global.json
            Src  
                Web             
                    project.json
                    Startup.cs
                    ...
                Model
                    project.json
                    Startup.cs
                    ...
    

    how does one compile both projects using dotnet? (from command line, not in visual studio)

    If you run dotnet build at the root folder level you get

    Could not find file .. project.json

    I can see there is this outstanding enhancement on the CLI repo but that is from Feb2.

    Any script would have to take dependencies into account before just blindly calling dotnet on all src sub-folders.