如何使用 gradle 和多個 gradle 項目實現 monorepo (how to implement monorepo with gradle and multiple gradle projects)


問題描述

如何使用 gradle 和多個 gradle 項目實現 monorepo (how to implement monorepo with gradle and multiple gradle projects)

我們有幾個完整的 gradle 項目 A、B、C、D。這些是將開始共享 protobuf 生成的 java 文件的微服務。我們正在考慮這樣的結構

A
    build.gradle (this is a full on gradle build)
B
    build.gradle (this is B's full on gradle)
common
    build.gradle (build the protobuf that is used by A and B)

現在,問題是我們如何確保當開發人員構建 A 時,它也構建通用,以防它在他的 git pull 上發生變化。B 也是如此。 settings.gradle 文件似乎沒有 ../../:project 或類似的東西。

我確實記得 gradle 也提出了一種構建多個 gradle 項目的方法。

理想情況下,當有人更改共同點時,多個 jenkins 構建也將被啟動,以驗證更改核心代碼並沒有破壞任何使用它的服務。我不太清楚如何

1. document the things that depend on common
2. use the document to kick off builds of all things depending on common

如果這要增長,並且你有 D 依賴於 C 依賴於公共,每個構建都需要開始將二進制上游從公共提供給 C,然後將 C 的 jar 和公共的 jar 提供給 D。我知道在 twitter 上使用“pants”來執行此操作。谷歌正在使用 bazel。也許我會調查而不是gradle?或者我們可以將它們混合在一起嗎?


參考解法

方法 1:

Simply declaring a dependency on common should be sufficient enough:

// Project A's build.gradle
dependencies {
    implementation(project(":common"))
}

In order to build a, the build of common would need to succeed. If the build of common failed for whatever reason, then then build of a will also fail. Example:

$ ./gradlew project‑a:build
> Task :common:compileJava FAILED
/Users/cisco/code/example‑multi‑project/common/src/main/java/common/ExampleCommon.java:6: error: incompatible types: int cannot be converted to String
        return 1;
               ^
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':common:compileJava'.
> Compilation failed; see the compiler error output for details.

You can see in the above that when I tried to build project a (project‑a:build), the common's build task was invoked (:common:compileJava).

Both projects a/b/etc should have thorough tests (unit, integration, smoke, etc) to make sure that any incompatible changes are detected early/often.

You can read more about multi‑project builds in the official guide: https://docs.gradle.org/current/userguide/multi_project_builds.html

(by Dean HillerCisco)

參考文件

  1. how to implement monorepo with gradle and multiple gradle projects (CC BY‑SA 2.5/3.0/4.0)

#monorepo #bazel #gradle






相關問題

Webpack 無法解決 node_modules 惰性資產 (Webpack Cannot Resolve node_modules lazy assets)

如何使用 gradle 和多個 gradle 項目實現 monorepo (how to implement monorepo with gradle and multiple gradle projects)

Monorepos 和跨包開發。使用 src/ 或 dist/? (Monorepos and cross package development. To use src/ or dist/?)

如何在 Monorepo 中運行多個開發包 (How to run multiple packages in development inside a Monorepo)

帶有許多 npm 腳本的 Monorepo (Monorepo with Many npm-scripts)

Pip 從 github monorepo 中安裝 python 庫 (Pip install a python library from within a github monorepo)

你能撤消 lerna 引導程序嗎? (Can you undo a lerna bootstrap?)

pnpm -- 同步依賴版本 (pnpm -- sync versions of dependencies)

如何在同一個 monorepo 中的 Python 項目之間共享開發依賴項? (how to share dev-dependencies between Python projects within the same monorepo?)

Monorepo – Yarn 工作區 Typescript Node.JS 項目 – 運行 nodemon 時找不到模塊 (Monorepo – Yarn workspaces Typescript Node.JS project – cannot find module when running nodemon)

在 Azure Devops 的 mono repo 中設置項目的工作目錄 (Set working directory of a project in mono repo in Azure Devops)

紗線工作區共享包未在 create-react-app 中加載 (yarn workspaces shared package does not load in create-react-app)







留言討論