【问题标题】:Compile multiple dotnet core projects in one step using dotnet cli使用 dotnet cli 一步编译多个 dotnet core 项目
【发布时间】:2016-08-02 10:06:07
【问题描述】:

假设这个文件夹结构

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

如何使用dotnet 编译这两个项目? (来自命令行,不在 Visual Studio 中)

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

找不到文件 ..project.json

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

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

【问题讨论】:

    标签: .net-core dotnet-cli


    【解决方案1】:

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

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

    【讨论】:

    • 好的,你的最后一条评论让我意识到,如果在上面的例子中说 web 依赖于 model 那么你不需要先进入模型文件夹来构建它 - 你可以简单地@ 987654326@ 在 web 文件夹中,它将编译 model -
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      dotnet build Src/**/project.json
      

      【讨论】:

      • dotnet pack 不支持这个(对于那些可能最终看到这个并希望使用相同技术的人)
      • 已经有一段时间了,新项目模板中没有project.json。任何更新的解决方案?
      【解决方案4】:

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

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

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

      我的一些项目需要使用 dockerfiles 构建 docker 容器,或者 snap 包,部署到测试等...... Make(请原谅双关语)使它变得容易。

      这是一个简单项目 Makefile 的示例。单独运行'make'就像说'make all'你可以设置一个像'cd ./subdir; 这样的命令。 make' 作为您的 .phoney 指令之一。 (谷歌:“Makefile 文档”)

      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)
      

      【讨论】:

        【解决方案5】:

        缺少的是,如果你没有project.json,你也可以在project.sln文件上使用命令

        dotnet build src/**/project.json
        -- or --
        dotnet build src/project.sln
        

        dotnet test 也是如此

        【讨论】:

          【解决方案6】:

          对于我正在使用的 linux:

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

          【讨论】:

            猜你喜欢
            • 2017-10-11
            • 2020-11-09
            • 2022-08-15
            • 2023-03-04
            • 2018-10-18
            • 2016-08-10
            • 2023-04-09
            • 1970-01-01
            • 2017-01-12
            相关资源
            最近更新 更多