問題描述
如何使用 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 Hiller、Cisco)