diff --git a/BUILD b/BUILD index 1b637445d..60ea9d867 100644 --- a/BUILD +++ b/BUILD @@ -79,6 +79,9 @@ bzl_library( # stardoc. "//visibility:public", ], + deps = [ + "@rules_java//java:rules", + ], ) alias( diff --git a/MODULE.bazel b/MODULE.bazel index 7a1975a89..2d2abc0b8 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -11,6 +11,10 @@ bazel_dep( name = "platforms", version = "0.0.8", ) +bazel_dep( + name = "rules_java", + version = "7.3.2", +) bazel_dep( name = "rules_kotlin", version = "1.9.0", diff --git a/private/rules/has_maven_deps.bzl b/private/rules/has_maven_deps.bzl index ccdd5add2..df75b67a4 100644 --- a/private/rules/has_maven_deps.bzl +++ b/private/rules/has_maven_deps.bzl @@ -1,3 +1,5 @@ +load("@rules_java//java:defs.bzl", "JavaInfo") + MavenInfo = provider( fields = { # Fields to do with maven coordinates diff --git a/private/rules/javadoc.bzl b/private/rules/javadoc.bzl index 98ce3a08b..c683dc4fe 100644 --- a/private/rules/javadoc.bzl +++ b/private/rules/javadoc.bzl @@ -1,3 +1,4 @@ +load("@rules_java//java:defs.bzl", "JavaInfo") load(":maven_project_jar.bzl", "DEFAULT_EXCLUDED_WORKSPACES") _JavadocInfo = provider( diff --git a/private/rules/jvm_import.bzl b/private/rules/jvm_import.bzl index 229e3f6fc..26292aed5 100644 --- a/private/rules/jvm_import.bzl +++ b/private/rules/jvm_import.bzl @@ -7,6 +7,7 @@ # [0]: https://github.com/square/bazel_maven_repository/pull/48 # [1]: https://github.com/bazelbuild/bazel/issues/4549 +load("@rules_java//java:defs.bzl", "JavaInfo") load("//settings:stamp_manifest.bzl", "StampManifestProvider") def _jvm_import_impl(ctx): diff --git a/private/rules/maven_bom_fragment.bzl b/private/rules/maven_bom_fragment.bzl index 4472e6772..35d4b816e 100644 --- a/private/rules/maven_bom_fragment.bzl +++ b/private/rules/maven_bom_fragment.bzl @@ -1,3 +1,4 @@ +load("@rules_java//java:defs.bzl", "JavaInfo") load(":has_maven_deps.bzl", "MavenInfo", "has_maven_deps") MavenBomFragmentInfo = provider( diff --git a/private/rules/maven_project_jar.bzl b/private/rules/maven_project_jar.bzl index 08ae674c2..158e40ceb 100644 --- a/private/rules/maven_project_jar.bzl +++ b/private/rules/maven_project_jar.bzl @@ -1,3 +1,4 @@ +load("@rules_java//java:defs.bzl", "JavaInfo", "java_common") load(":has_maven_deps.bzl", "MavenInfo", "calculate_artifact_jars", "calculate_artifact_source_jars", "has_maven_deps") load(":maven_utils.bzl", "determine_additional_dependencies") diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 2f8b74b0b..2a3825d18 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -1,3 +1,4 @@ +load("@rules_java//java:defs.bzl", "JavaInfo") load(":has_maven_deps.bzl", "MavenInfo", "calculate_artifact_jars", "has_maven_deps") load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom") diff --git a/repositories.bzl b/repositories.bzl index 74a54e52b..f2970ae62 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -19,6 +19,39 @@ def rules_jvm_external_deps(repositories = _DEFAULT_REPOSITORIES): ], ) + # The `rules_java` major version is tied to the major version of Bazel that it supports, + # so this is different from the version in the MODULE file. + major_version = native.bazel_version.partition(".")[0] + if major_version == "5": + maybe( + http_archive, + name = "rules_java", + urls = [ + "https://github.com/bazelbuild/rules_java/releases/download/5.5.1/rules_java-5.5.1.tar.gz", + ], + sha256 = "73b88f34dc251bce7bc6c472eb386a6c2b312ed5b473c81fe46855c248f792e0", + ) + + elif major_version == "6": + maybe( + http_archive, + name = "rules_java", + urls = [ + "https://github.com/bazelbuild/rules_java/releases/download/6.5.2/rules_java-6.5.2.tar.gz", + ], + sha256 = "16bc94b1a3c64f2c36ceecddc9e09a643e80937076b97e934b96a8f715ed1eaa", + ) + + else: + maybe( + http_archive, + name = "rules_java", + urls = [ + "https://github.com/bazelbuild/rules_java/releases/download/7.3.2/rules_java-7.3.2.tar.gz", + ], + sha256 = "3121a00588b1581bd7c1f9b550599629e5adcc11ba9c65f482bbd5cfe47fdf30", + ) + maven_install( name = "rules_jvm_external_deps", artifacts = [ diff --git a/setup.bzl b/setup.bzl index ac9842c12..6996148e7 100644 --- a/setup.bzl +++ b/setup.bzl @@ -1,6 +1,14 @@ load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") +load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains") load("@rules_jvm_external_deps//:defs.bzl", "pinned_maven_install") def rules_jvm_external_setup(): bazel_skylib_workspace() + + # When using bazel 5, we have undefined toolchains from rules_js. This should be fine to skip, since we only need + # it for the `JavaInfo` definition. + major_version = native.bazel_version.partition(".")[0] + if major_version != "5": + rules_java_dependencies() + rules_java_toolchains() pinned_maven_install() diff --git a/tests/unit/aar_import/aar_import_test.bzl b/tests/unit/aar_import/aar_import_test.bzl index 1b9216fd0..b38d5246e 100644 --- a/tests/unit/aar_import/aar_import_test.bzl +++ b/tests/unit/aar_import/aar_import_test.bzl @@ -1,4 +1,5 @@ load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("@rules_java//java:defs.bzl", "JavaInfo") def _does_aar_import_have_srcjar_impl(ctx): env = analysistest.begin(ctx) diff --git a/tests/unit/exports/exports_test.bzl b/tests/unit/exports/exports_test.bzl index 9a9cb8f2b..bbbbfc3c7 100644 --- a/tests/unit/exports/exports_test.bzl +++ b/tests/unit/exports/exports_test.bzl @@ -1,6 +1,7 @@ """Unit tests for exports.""" load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("@rules_java//java:defs.bzl", "JavaInfo") load("//private/rules:has_maven_deps.bzl", "MavenInfo", "has_maven_deps") load("//private/rules:maven_project_jar.bzl", "maven_project_jar")