Skip to content

Commit c606519

Browse files
committed
bazel: Support maven_install
maven_install is strongly superior to previous forms of grabbing dependencies from Maven as it computes the appropriate versions to use from the full transitive dependencies of all libraries used by an application. It also has much less boilerplate and includes dependencies with generated targets. In the future we will drop the jvm_maven_import_external usages and require maven_install, at which point we can swap to using the `@maven' repository and no longer depend on compat_repositories. Fixes #5359
1 parent 6a5ee19 commit c606519

File tree

9 files changed

+121
-33
lines changed

9 files changed

+121
-33
lines changed

Diff for: WORKSPACE

+27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
workspace(name = "io_grpc_grpc_java")
22

3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "rules_jvm_external",
7+
sha256 = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a",
8+
strip_prefix = "rules_jvm_external-3.0",
9+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/3.0.zip",
10+
)
11+
12+
load("@rules_jvm_external//:defs.bzl", "maven_install")
13+
load("//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS")
14+
load("//:repositories.bzl", "IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS")
15+
16+
maven_install(
17+
artifacts = IO_GRPC_GRPC_JAVA_ARTIFACTS,
18+
generate_compat_repositories = True,
19+
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
20+
repositories = [
21+
"https://repo.maven.apache.org/maven2/",
22+
],
23+
)
24+
25+
load("@maven//:compat.bzl", "compat_repositories")
26+
27+
compat_repositories()
28+
329
load("//:repositories.bzl", "grpc_java_repositories")
430

531
grpc_java_repositories()
632

733
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
34+
835
protobuf_deps()

Diff for: buildscripts/kokoro/bazel.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -exu -o pipefail
44
cat /VERSION
55

6-
use_bazel.sh 0.28.1
6+
use_bazel.sh 1.0.1
77
bazel version
88

99
cd github/grpc-java

Diff for: examples/BUILD.bazel

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ java_library(
7070
":helloworld_java_proto",
7171
":route_guide_java_grpc",
7272
":route_guide_java_proto",
73-
"@com_google_api_grpc_proto_google_common_protos//jar",
74-
"@com_google_code_findbugs_jsr305//jar",
75-
"@com_google_code_gson_gson",
76-
"@com_google_guava_guava//jar",
7773
"@com_google_protobuf//:protobuf_java",
7874
"@com_google_protobuf//:protobuf_java_util",
7975
"@io_grpc_grpc_java//api",
8076
"@io_grpc_grpc_java//protobuf",
8177
"@io_grpc_grpc_java//stub",
78+
"@maven//:com_google_api_grpc_proto_google_common_protos",
79+
"@maven//:com_google_code_findbugs_jsr305",
80+
"@maven//:com_google_code_gson_gson",
81+
"@maven//:com_google_guava_guava",
8282
],
8383
)
8484

Diff for: examples/WORKSPACE

+27-11
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,41 @@ local_repository(
1212
path = "..",
1313
)
1414

15-
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
16-
17-
15+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1816

19-
maven_jar(
20-
name = "com_google_api_grpc_cloud_pubsub_v1",
21-
artifact = "com.google.api.grpc:grpc-google-cloud-pubsub-v1:0.1.24",
22-
sha1 = "601d8be0fd0cc0e050b1af3b88f191ada9a2f4e5",
17+
http_archive(
18+
name = "rules_jvm_external",
19+
sha256 = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a",
20+
strip_prefix = "rules_jvm_external-3.0",
21+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/3.0.zip",
2322
)
2423

25-
maven_jar(
26-
name = "com_google_api_grpc_proto_cloud_pubsub_v1",
27-
artifact = "com.google.api.grpc:proto-google-cloud-pubsub-v1:0.1.24",
28-
sha1 = "e6dd66635f674b4e380dfd3de252ae019a51a67e",
24+
load("@rules_jvm_external//:defs.bzl", "maven_install")
25+
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS")
26+
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS")
27+
28+
maven_install(
29+
artifacts = [
30+
"com.google.api.grpc:grpc-google-cloud-pubsub-v1:0.1.24",
31+
"com.google.api.grpc:proto-google-cloud-pubsub-v1:0.1.24",
32+
] + IO_GRPC_GRPC_JAVA_ARTIFACTS,
33+
generate_compat_repositories = True,
34+
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
35+
repositories = [
36+
"https://repo.maven.apache.org/maven2/",
37+
],
2938
)
3039

40+
load("@maven//:compat.bzl", "compat_repositories")
3141

42+
compat_repositories()
3243

44+
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
45+
46+
# Run grpc_java_repositories after compat_repositories to ensure the
47+
# maven_install-selected dependencies are used.
3348
grpc_java_repositories()
3449

3550
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
51+
3652
protobuf_deps()

Diff for: examples/example-gauth/BUILD.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ java_library(
77
["src/main/java/**/*.java"],
88
),
99
deps = [
10-
"@com_google_api_grpc_cloud_pubsub_v1//jar",
11-
"@com_google_api_grpc_proto_cloud_pubsub_v1//jar",
12-
"@com_google_auth_google_auth_library_oauth2_http//jar",
1310
"@io_grpc_grpc_java//api",
1411
"@io_grpc_grpc_java//auth",
1512
"@io_grpc_grpc_java//protobuf",
1613
"@io_grpc_grpc_java//stub",
14+
"@maven//:com_google_api_grpc_grpc_google_cloud_pubsub_v1",
15+
"@maven//:com_google_api_grpc_proto_google_cloud_pubsub_v1",
16+
"@maven//:com_google_auth_google_auth_library_oauth2_http",
1717
],
1818
)
1919

Diff for: examples/example-tls/BUILD.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ java_library(
2424
["src/main/java/**/*.java"],
2525
),
2626
runtime_deps = [
27-
"@io_netty_netty_tcnative_boringssl_static//jar",
27+
"@maven//:io_netty_netty_tcnative_boringssl_static",
2828
],
2929
deps = [
3030
":helloworld_java_grpc",
@@ -33,7 +33,7 @@ java_library(
3333
"@io_grpc_grpc_java//netty",
3434
"@io_grpc_grpc_java//protobuf",
3535
"@io_grpc_grpc_java//stub",
36-
"@io_netty_netty_handler//jar",
36+
"@maven//:io_netty_netty_handler",
3737
],
3838
)
3939

Diff for: netty/shaded/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ java_library(
55
runtime_deps = [
66
"//netty",
77
"@io_netty_netty_tcnative_boringssl_static//jar",
8-
"@io_netty_netty_transport_native_epoll//jar",
8+
"@io_netty_netty_transport_native_epoll_linux_x86_64//jar",
99
],
1010
)

Diff for: protobuf/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ java_library(
1212
"@com_google_guava_guava//jar",
1313
"@com_google_j2objc_j2objc_annotations//jar",
1414
"@com_google_protobuf//:protobuf_java",
15-
"@com_google_protobuf//:protobuf_java_util",
1615
],
1716
)

Diff for: repositories.bzl

+56-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@
33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
55

6+
# For use with maven_install's artifacts.
7+
# maven_install(
8+
# ...
9+
# artifacts = [
10+
# # Your own deps
11+
# ] + IO_GRPC_GRPC_JAVA_ARTIFACTS,
12+
# )
13+
IO_GRPC_GRPC_JAVA_ARTIFACTS = [
14+
"com.google.android:annotations:4.1.1.4",
15+
"com.google.api.grpc:proto-google-common-protos:1.17.0",
16+
"com.google.auth:google-auth-library-credentials:0.19.0",
17+
"com.google.auth:google-auth-library-oauth2-http:0.19.0",
18+
"com.google.code.findbugs:jsr305:3.0.2",
19+
"com.google.code.gson:gson:jar:2.8.6",
20+
"com.google.errorprone:error_prone_annotations:2.3.3",
21+
"com.google.guava:failureaccess:1.0.1",
22+
"com.google.guava:guava:28.1-android",
23+
"com.google.j2objc:j2objc-annotations:1.3",
24+
"com.google.truth:truth:1.0",
25+
"com.squareup.okhttp:okhttp:2.5.0",
26+
"com.squareup.okio:okio:1.13.0",
27+
"io.netty:netty-buffer:4.1.42.Final",
28+
"io.netty:netty-codec-http2:4.1.42.Final",
29+
"io.netty:netty-codec-http:4.1.42.Final",
30+
"io.netty:netty-codec-socks:4.1.42.Final",
31+
"io.netty:netty-codec:4.1.42.Final",
32+
"io.netty:netty-common:4.1.42.Final",
33+
"io.netty:netty-handler-proxy:4.1.42.Final",
34+
"io.netty:netty-handler:4.1.42.Final",
35+
"io.netty:netty-resolver:4.1.42.Final",
36+
"io.netty:netty-tcnative-boringssl-static:2.0.26.Final",
37+
"io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.42.Final",
38+
"io.netty:netty-transport:4.1.42.Final",
39+
"io.opencensus:opencensus-api:0.24.0",
40+
"io.opencensus:opencensus-contrib-grpc-metrics:0.24.0",
41+
"io.perfmark:perfmark-api:0.19.0",
42+
"javax.annotation:javax.annotation-api:1.2",
43+
"junit:junit:4.12",
44+
"org.apache.commons:commons-lang3:3.5",
45+
"org.codehaus.mojo:animal-sniffer-annotations:1.18",
46+
]
47+
648
# For use with maven_install's override_targets.
749
# maven_install(
850
# ...
@@ -22,6 +64,9 @@ load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
2264
# "your.target:artifact": "@//third_party/artifact",
2365
# )
2466
IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS = {
67+
"com.google.protobuf:protobuf-java": "@com_google_protobuf//:protobuf_java",
68+
"com.google.protobuf:protobuf-java-util": "@com_google_protobuf//:protobuf_java_util",
69+
"com.google.protobuf:protobuf-javalite": "@com_google_protobuf_javalite//:protobuf_java_lite",
2570
"io.grpc:grpc-alts": "@io_grpc_grpc_java//alts",
2671
"io.grpc:grpc-api": "@io_grpc_grpc_java//api",
2772
"io.grpc:grpc-auth": "@io_grpc_grpc_java//auth",
@@ -39,6 +84,13 @@ IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS = {
3984

4085
def grpc_java_repositories():
4186
"""Imports dependencies for grpc-java."""
87+
if not native.existing_rule("com_google_protobuf"):
88+
com_google_protobuf()
89+
if not native.existing_rule("com_google_protobuf_javalite"):
90+
com_google_protobuf_javalite()
91+
if not native.existing_rule("io_grpc_grpc_proto"):
92+
io_grpc_grpc_proto()
93+
4294
if not native.existing_rule("com_google_android_annotations"):
4395
com_google_android_annotations()
4496
if not native.existing_rule("com_google_api_grpc_proto_google_common_protos"):
@@ -59,14 +111,8 @@ def grpc_java_repositories():
59111
com_google_guava_failureaccess()
60112
if not native.existing_rule("com_google_j2objc_j2objc_annotations"):
61113
com_google_j2objc_j2objc_annotations()
62-
if not native.existing_rule("com_google_protobuf"):
63-
com_google_protobuf()
64-
if not native.existing_rule("com_google_protobuf_javalite"):
65-
com_google_protobuf_javalite()
66114
if not native.existing_rule("com_google_truth_truth"):
67115
com_google_truth_truth()
68-
if not native.existing_rule("io_grpc_grpc_proto"):
69-
io_grpc_grpc_proto()
70116
if not native.existing_rule("com_squareup_okhttp_okhttp"):
71117
com_squareup_okhttp_okhttp()
72118
if not native.existing_rule("com_squareup_okio_okio"):
@@ -93,8 +139,8 @@ def grpc_java_repositories():
93139
io_netty_netty_tcnative_boringssl_static()
94140
if not native.existing_rule("io_netty_netty_transport"):
95141
io_netty_netty_transport()
96-
if not native.existing_rule("io_netty_netty_transport_native_epoll"):
97-
io_netty_netty_transport_native_epoll()
142+
if not native.existing_rule("io_netty_netty_transport_native_epoll_linux_x86_64"):
143+
io_netty_netty_transport_native_epoll_linux_x86_64()
98144
if not native.existing_rule("io_opencensus_opencensus_api"):
99145
io_opencensus_opencensus_api()
100146
if not native.existing_rule("io_opencensus_opencensus_contrib_grpc_metrics"):
@@ -368,9 +414,9 @@ def io_netty_netty_transport():
368414
licenses = ["notice"], # Apache 2.0
369415
)
370416

371-
def io_netty_netty_transport_native_epoll():
417+
def io_netty_netty_transport_native_epoll_linux_x86_64():
372418
jvm_maven_import_external(
373-
name = "io_netty_netty_transport_native_epoll",
419+
name = "io_netty_netty_transport_native_epoll_linux_x86_64",
374420
artifact = "io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.42.Final",
375421
server_urls = ["https://repo.maven.apache.org/maven2/"],
376422
artifact_sha256 = "7bdf3003d5b60b061b494e62d1bafc420caf800efb743b14ec01ceaef1d3fa3e",

0 commit comments

Comments
 (0)