.net-core - dotnet build, exclude only one dependency from build
问题描述
Consider the following project structure:
- MyProject.Api.a
- MyProject.Api.b
- MyProject.Data, referenced by a and b
I've set up a pipeline in Azure devOps that performs a restore, build and publish like this:
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"
problem is that MyProject.Data is not buildable from source, it needs an external tool to run first that generates some C# classes. Before this step, the project won't be able to build. So I added this:
- 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
This all works, but the codegen tool needs to run on each API project job I'm running, making my build quite slow. I was hoping there would be some way to only run the codegen tool once, and then all API projects could use the binaries from the data project where the files were generated?
Ideally, I would have to be able to prebuild the data project in a separate job, publish the dll's as an artifact, and then use those dll's in my subsequent API builds. I'm guessing this would be possible with dotnet build --no-dependencies
, but that would implicate I need to build everything else separately as well, which is undesirable from a maintainability point of view.
解决方案
You can try to combine multiple jobs into one job. First generate the required C # classes through the first two tasks, and then build with wildcards (e.g. **/*.csproj for all .csproj files in all subfolders) in the dotnet build task. You can consider removing restore tasks, because dotnet restore is run implicitly in dotnet build.
This is stated here : You don't have to run dotnet restore
because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack
. To disable implicit restore, use the --no-restore
option.
- 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)"
推荐阅读
- ruby-on-rails - 通过 Ruby on Rails 中的 HTTP 标头自定义授权
- terminal - 如何在两个终端之间通话,一个使用 Ncat,一个使用 Nping?
- ios - 点击segmentedControl时SwiftUI关闭键盘
- javascript - 使用变量反应动态导入不起作用
- typescript - “错误:无法处理请求”谷歌云功能错误
- c - 有没有办法硬编码一个二维整数数组而不必在 C 中提及任何维度?
- python - 即使日期存在,SQLAlchemy 按日期过滤也不返回
- python - 如何在 Aegis Blade 中运行 Selenium
- google-bigquery - Big Query 中的双重命令
- java - Android Studio 错误器 OnCreate