【问题标题】:dotnet build, exclude only one dependency from builddotnet 构建,仅从构建中排除一个依赖项
【发布时间】:2020-05-18 13:32:52
【问题描述】:

考虑以下项目结构:

  • MyProject.Api.a
  • MyProject.Api.b
  • MyProject.Data,由 a 和 b 引用

我在 Azure devOps 中设置了一个管道,它执行还原、构建和发布,如下所示:

jobs:
  - job: api-a
    steps:
      - task: DotNetCoreCLI@2
        displayName: Restore
        inputs:
          command: restore
          projects: "MyProject.Api.a.csproj"
      - task: DotNetCoreCLI@2
        displayName: Build
        inputs:
          command: build
          projects: "MyProject.Api.a.csproj"
          arguments: "--configuration $(buildConfiguration)"
      - task: DotNetCoreCLI@2
        displayName: Publish
        inputs:
          command: publish
          projects: "MyProject.Api.a.csproj"
          publishWebProjects: false
          arguments: "--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/project-api-a-publish"
      - task: PublishPipelineArtifact@1
        displayName: Publish release Artifact
        inputs:
          targetPath: "$(Build.ArtifactStagingDirectory)/project-api-a-publish"
          artifactName: "a-publish"
  - job: api-b
    steps:
      - task: DotNetCoreCLI@2
        displayName: Restore
        inputs:
          command: restore
          projects: "MyProject.Api.b.csproj"
      - task: DotNetCoreCLI@2
        displayName: Build
        inputs:
          command: build
          projects: "MyProject.Api.b.csproj"
          arguments: "--configuration $(buildConfiguration)"
      - task: DotNetCoreCLI@2
        displayName: Publish
        inputs:
          command: publish
          projects: "MyProject.Api.b.csproj"
          publishWebProjects: false
          arguments: "--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/project-api-b-publish"
      - task: PublishPipelineArtifact@1
        displayName: Publish release Artifact
        inputs:
          targetPath: "$(Build.ArtifactStagingDirectory)/project-api-b-publish"
          artifactName: "b-publish"

问题是 MyProject.Data 不能从源代码构建,它需要首先运行一个外部工具来生成一些 C# 类。在此步骤之前,项目将无法构建。 所以我添加了这个:

      - task: DotNetCoreCLI@2
        displayName: "Restore tools"
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: restore --interactive --configfile ../NuGet.config
      - task: DotNetCoreCLI@2
        displayName: my-codegen-tool
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: run my-codegen-tool

这一切都有效,但是代码生成工具需要在我正在运行的每个 API 项目作业上运行,这使得我的构建速度很慢。 我希望有某种方法只运行一次代码生成工具,然后所有 API 项目都可以使用生成文件的数据项目中的二进制文件?

理想情况下,我必须能够在单独的作业中预构建数据项目,将 dll 作为工件发布,然后在后续的 API 构建中使用这些 dll。我猜dotnet build --no-dependencies 可以做到这一点,但这意味着我还需要单独构建其他所有内容,从可维护性的角度来看,这是不可取的。

【问题讨论】:

    标签: .net-core azure-pipelines


    【解决方案1】:

    您可以尝试将多个作业合并为一个作业。首先通过前两个任务生成所需的 C# 类,然后在 dotnet build 任务中使用通配符(例如所有子文件夹中的所有 .csproj 文件使用 **/*.csproj)进行构建。 您可以考虑删除还原任务,因为 dotnet restore 在 dotnet build 中隐式运行。

    这被声明为here:您不必运行dotnet restore,因为它由所有需要恢复的命令隐式运行,例如dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack。要禁用隐式还原,请使用 --no-restore 选项。

    - job: 
        steps:
          - task: DotNetCoreCLI@2
            displayName: "Restore tools"
            inputs:
              workingDirectory: "MyProject.Data"
              command: custom
              custom: tool
              arguments: restore --interactive --configfile ../NuGet.config
          - task: DotNetCoreCLI@2
            displayName: my-codegen-tool
            inputs:
              workingDirectory: "MyProject.Data"
              command: custom
              custom: tool
              arguments: run my-codegen-tool
          - task: DotNetCoreCLI@2
            displayName: Build
            inputs:
              command: build
              projects: "**/*.csproj"
              arguments: "--configuration $(buildConfiguration)"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多