使用dotnet cli一步编译多个dotnet core项目

Compile multiple dotnet core projects in one step using dotnet cli

假定此文件夹结构

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

如何使用 dotnet 编译两个项目? (来自命令行,而不是 visual studio)

如果您 运行 dotnet build 在根文件夹级别,您会得到

Could not find file .. project.json

我可以看到 CLI 存储库上有 this outstanding enhancement,但那是从 2 月 2 日开始的。

任何脚本在对所有 src 子文件夹盲目调用 dotnet 之前都必须考虑依赖关系。

目前还没有这样的工具。即使 KoreBuild,ASP.NET 团队使用的工具,也会盲目地进入每个文件夹并调用 dotnet build/pack

好消息是 dotnet build 现在足够聪明,如果它们没有改变就不会重新编译依赖项,所以这不再是问题了。

我也有类似的需求。这是我的解决方法:

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

dotnet build 命令接受 glob 模式。所以你可以这样做:

dotnet build Src/**/project.json

使用 GNU Make。我用它来构建我的项目。您所要做的就是在项目根文件夹中创建一个 Makefile。您可以将 Makefile 嵌套在目录中,并拥有一个包含 运行 子目录的顶级 Makefile。然后为每个 "Sub Projects" 文件夹和 运行 任何命令行工具设置 Makefile。与 dotnet 核心是 dotnet 。

等等... GNU - "GNU is not Unix" 那是一个 Unix/Linux 应用程序... 我 运行 windows。好消息是您可以在 windows 中做到这一点。我通过 git-bash 安装使用 make.exe(git 用于 windows)。您将不得不去寻找 make 的 cygwin 端口。 (google: "make for git-bash")然后安装到你的cygwin文件夹下的bin目录下。如果你真的想要,你也可以只安装 cygwin。

使用 Gnu-Make 的好处在于它是通用的。由于 dotnet core 与平台无关,每个环境 Mac/FreeBSD/Linux 很可能已经安装了 "make"。将它添加到您的 Windows 机器和项目对我来说很有意义。由于您的项目现在可以由每个人以相同的方式构建。

我的一些项目需要使用 docker 文件或 snap 包构建 docker 容器,部署以测试等...制作(请原谅双关语)使它变得简单。

这里是简单项目 Makefile 的示例。 运行 'make' 本身就像说 'make all' 你可以设置一个像 'cd ./subdir; make' 这样的命令作为你的 .phoney 指令之一。 (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)

缺少的是,如果您没有 project.json

,您也可以在 project.sln 文件上使用命令
dotnet build src/**/project.json
-- or --
dotnet build src/project.sln

dotnet test

也一样

对于 linux 我正在使用:

for p in $(find . -name *.csproj); do dotnet build $p; done