I am trying to build a simple java project with Bazel, using rules_jvm_external. Some of the dependencies are kept in a private maven repository.
My WORKSPACE looks like this:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "3.2"
RULES_JVM_EXTERNAL_SHA = "82262ff4223c5fda6fb7ff8bd63db8131b51b413d26eb49e3131037e79e324af"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
name = "maven_deps",
artifacts = [
"org.projectlombok:lombok:1.18.12",
"mygroup:myartifact:version"
],
repositories = [
"https://repo1.maven.org/maven2",
"https://username:password#my.repo.io/artifactory/repo",
],
)
My BUILD looks like this:
java_library(
name = "mylib",
srcs = glob([
"proj/src/main/java/**/*.java"
]),
deps = [
"#maven_deps//:org_projectlombok_lombok",
"#maven_deps//:mygroup_myartifact",
],
)
When i run bazel build //:mylib the fetching of mygroup:myartifact:version from the private maven repository fails with http error code 403. I hardcoded the username and password for simplicity. The username used is an email so i encoded it, e.g.: me%40gmail.com.
I am using bazel version 3.1.0.
Passing the username and password through env vars produced the same error.
Fetching the same jar using curl works great:
curl -O 'https://me%40gmail:PASSWORD#my.repo.io/artifactory/repo/mygroup/myartifcat-version.jar'
Can anyone see what the problem is?
Thank you in advance!
If I recall correctly, Bazel's maven_install from rules_jvm_external relies on Coursera Coursier* for fetching dependencies. Where I work at, we rely on a property file containing the credentials at the correct location for your OS.
Try setting this:
simple.username=<username>
simple.password=<password>
simple.host=my.repo.io
In either /.config/coursier/credentials.properties (if you are on Linux) or ~/Library/Preferences/Coursier/credentials.properties on OS X.
(*) https://github.com/bazelbuild/rules_jvm_external/blob/master/docs/api.md mentions Coursier indeed
Related
My CustomTest.java has this import:
com.google.protobuf.Timestamp
I'm using java_test_suite to run tests in my BUILD file like so:
java_test_suite(
name = "all-tests",
srcs = glob(["src/test/java/**/*.java"]),
runner = "junit5",
test_suffixes = ["Test.java"],
runtime_deps = JUNIT5_DEPS,
deps = [
":mylib",
"#com_google_protobuf//:timestamp_proto",
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.junit.jupiter:junit-jupiter-params"),
] + deps,
)
However when I run tests on it using:
bazel test //:all-tests
I'm getting this error:
src/test/java/com/x/CustomTest.java:75: error: [strict] Using type com.google.protobuf.Timestamp from an indirect dependency (TOOL_INFO: "#com_google_protobuf//:timestamp_proto wrapped in java_proto_library"). See command below **
private static Timestamp timestampFromMilli(long milli) {
^
** Please add the following dependencies:
#com_google_protobuf//:timestamp_proto to //:src/test/java/com/x/CustomTest
** You can use the following buildozer command:
buildozer 'add deps #com_google_protobuf//:timestamp_proto' //:src/test/java/com/x/CustomTest
What do I need to do exactly? I tried using the buildozer command but all I got was:
rule 'src/test/java/com/x/CustomTest' not found
Where do I need to add this #com_google_protobuf//:timestamp_proto?
Looking at protobuf's build files, it looks like timestamp_proto is a plain proto_library:
https://github.com/protocolbuffers/protobuf/blob/main/BUILD.bazel#L70-L74
https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/BUILD.bazel#L64-L68
and so per the advice here:
https://github.com/protocolbuffers/protobuf/blob/main/BUILD.bazel#L19-L25
you might just need to use java_proto_library to make the java version of the proto:
java_proto_library(
name = "timestamp_java_proto",
deps = ["#com_google_protobuf//:timestamp_proto"],
)
and then use that in the deps of your java_test_suite instead of the timestamp_proto.
Just a guess, but the error message is not very helpful maybe because there happens to be a Timestamp java class in the deps of the plain proto library, and Strict deps is finding that one in the test's indirect dependencies. Might be worth filing a bug about it on https://github.com/bazelbuild/bazel/issues
I want to use a specific Azul Zulu JDK for my Java builds. Therefore I have stored it in my repository locally e.g. under tools/zulu19.30.11-ca-jdk19.0.1-macosx_x64. Now I want to configure a java toolchain target such that I can pass it via --java_toolchain=//tools:my_custom_java_toolchain. I don't want to depend on some remote repositories.
What are the required steps to achieve this?
I have found this repository: https://github.com/salesforce/bazel-jdt-java-toolchain/blob/main/jdt/defs.bzl which defines a target of type default_java_toolchain but I can not derive something useful for my use case. I don't know e.g. what the field header_compiler means. My naive assumption is that I just have to pass some paths to the required tools (such as bin/javac) for java compilation.
My current approach uses the rules java_toolchain and java_runtime. My BUILD file looks like this:
java_runtime(
name = "zulu19.30.11-ca-jdk19.0.1-macosx_x64",
srcs = glob(["zulu19.30.11-ca-jdk19.0.1-macosx_x64/**"]),
java_home = "zulu19.30.11-ca-jdk19.0.1-macosx_x64",
)
java_toolchain(
name = "zulu-19",
source_version = "19",
target_version = "19",
java_runtime = ":zulu19.30.11-ca-jdk19.0.1-macosx_x64",
javabuilder = "",
ijar = "",
singlejar = "",
genclass = "",
)
I am trying to execute the command: bazel build --extra_toolchains="//tools:zulu-19" //:ProjectRunner and it complains about the missing mandatory attributes javabuilder, ijar, singlejar and genclass but I have no idea which are the correct paths or values.
I'm just wondering how bazel knows how to compile the java code with these few information. Why I don't have to specify javac for example?
Basically I came up with the following solution which seems to solve my problem and is inspired by the rule default_java_toolchain. I used the existing targets for javabuilder, ijar, singlejar and genclass which are defined in #bazel_tools//tools/jdk. Note that I had to use BASE_JDK9_JVM_OPTS for a successful build.
load("#bazel_tools//tools/jdk:default_java_toolchain.bzl", "BASE_JDK9_JVM_OPTS")
java_runtime(
name = "zulu19.30.11-ca-jdk19.0.1-macosx_x64",
srcs = glob(["zulu19.30.11-ca-jdk19.0.1-macosx_x64/**"]),
java_home = "zulu19.30.11-ca-jdk19.0.1-macosx_x64",
)
config_setting(
name = "zulu-19-runtime_version_setting",
values = {"java_runtime_version": "19"},
visibility = ["//visibility:private"],
)
toolchain(
name = "zulu-19_runtime_toolchain_definition",
target_settings = ["zulu-19-runtime_version_setting"],
toolchain_type = "#bazel_tools//tools/jdk:runtime_toolchain_type",
toolchain = "zulu19.30.11-ca-jdk19.0.1-macosx_x64",
)
java_toolchain(
name = "zulu-19",
source_version = "19",
target_version = "19",
java_runtime = ":zulu19.30.11-ca-jdk19.0.1-macosx_x64",
jvm_opts = BASE_JDK9_JVM_OPTS,
javabuilder = ["#bazel_tools//tools/jdk:javabuilder"],
ijar = ["#bazel_tools//tools/jdk:ijar"],
singlejar = ["#bazel_tools//tools/jdk:singlejar"],
genclass = ["#bazel_tools//tools/jdk:genclass"],
)
config_setting(
name = "zulu-19_version_setting",
values = {"java_language_version": "19"},
visibility = ["//visibility:private"],
)
toolchain(
name = "zulu-19_toolchain_definition",
toolchain_type = "#bazel_tools//tools/jdk:toolchain_type",
target_settings = ["zulu-19_version_setting"],
toolchain = "zulu-19",
)
The following command now successfully runs without any errors. System.out.println(Runtime.version()); also prints the correct version.
bazel run --extra_toolchains="//tools:zulu-19_toolchain_definition,//tools:zulu-19_runtime_toolchain_definition" --java_language_version="19" --java_runtime_version="19" //:ProjectRunner
But I still have a couple of questions, e.g. why javabuilder, ijar, singlejar and genclass are needed and what is the purpose of those targets? How does the toolchain target know about my javac binary for compiling the java code? Why do I have to use BASE_JDK9_JVM_OPTS for a successful build and can I omit some settings?
I currently have a very trivial JavaFX "Hello, World!" application that I am trying to build and run with Bazel. I am using the maven_install() rule to install the JavaFX dependencies in my WORKSPACE files like so:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "3.2"
RULES_JVM_EXTERNAL_SHA = "82262ff4223c5fda6fb7ff8bd63db8131b51b413d26eb49e3131037e79e324af"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"org.openjfx:javafx-controls:mac:11.0.1",
"org.openjfx:javafx-graphics:mac:11.0.1",
"org.openjfx:javafx-base:mac:11.0.1",
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
And then I try to build a java_binary in the BUILD file like so:
java_binary(
name = "app",
srcs = glob(["src/**/*.java"]),
main_class = "com.dylanpowers.Main",
deps = [
"#maven//:org_openjfx_javafx_controls_mac",
"#maven//:org_openjfx_javafx_graphics_mac",
"#maven//:org_openjfx_javafx_base_mac"
]
)
In this case, Main.java is actually the only file in my application, as I am trying to just get the program to run. The build seems to work fine with bazel build :app, but when I try to run it with bazel run :app, I get the following error:
Error: JavaFX runtime components are missing, and are required to run this application
Can somebody please help me resolve this?
https://github.com/deepinthink-pumpkin/pumpkin-chat-jfx/blob/master/main/BUILD.bazel#L21
Create another main class as javafx application entrance.
I'm working on migrating our project to Bazel.
WORKSPACE
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "2.10"
RULES_JVM_EXTERNAL_SHA = "1bbf2e48d07686707dd85357e9a94da775e1dbd7c464272b3664283c9c716d26"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
name = "maven",
artifacts = [
"com.foo:bar:1.0.0-SNAPSHOT"
"org.apache.commons:commons-lang3:3.9",
],
repositories = [
"https://our-maven-repo",
],
resolve_timeout = 200,
fail_on_missing_checksum = False,
fetch_sources = True
)
BUILD
java_library(
name = "our-name",
srcs = glob([
"src/main/java/**/*.java"
]),
resources = glob([
"src/main/resources/**",
]),
deps = [
"#maven//:com.foo_bar",
"#maven//:org.apache.commons_commons-lang3"
],
)
When I run:
PS> bazel fetch //:our-name
Output
INFO: Call stack for the definition of repository 'maven' which is a coursier_fetch (rule definition at C:/users/name/_bazel_name/73nyktky/external/rules_jvm_external/coursier.bzl:620:18):
- C:/users/name/_bazel_name/73nyktky/external/rules_jvm_external/defs.bzl:89:5
- C:/project/WORKSPACE:19:1
INFO: Repository 'maven' used the following cache hits instead of downloading the corresponding file.
* Hash '8f35f92fb8e021f96b3aa8145c66c3b2e29295baabb28ff50569e613438afcbd' for https://github.com/coursier/coursier/releases/download/v2.0.0-RC3-4/coursier.jar
If the definition of 'maven' was updated, verify that the hashes were also updated.
ERROR: An error occurred during the fetch of repository 'maven':
Error while obtaining the sha256 checksum of v1/https/our-repo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar: java.io.IOException: ERROR: src/main/native/windows/process.cc(199): CreateProcessW("python" C:/users/name/_bazel_name/73nyktky/external/bazel_tools/tools/build_defs/hash/sha256.py C:/users/name/_bazel_name/73nyktky/external/maven/v1/https/ourrepo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar artifact.sha256): The system cannot find the file specified.
(error: 2)
ERROR: C:/project/BUILD:1:1: no such package '#maven//': Error while obtaining the sha256 checksum of v1/https/our-repo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar: java.io.IOException: ERROR: src/main/native/windows/process.cc(199): CreateProcessW("python" C:/users/name/_bazel_name/73nyktky/external/bazel_tools/tools/build_defs/hash/sha256.py C:/users/name/_bazel_name/73nyktky/external/maven/v1/https/ourrepo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar artifact.sha256): The system cannot find the file specified.
(error: 2) and referenced by '//:our-name'
ERROR: Evaluation of query "deps(//:our-name)" failed: errors were encountered while computing transitive closure
Loading: 0 packages loaded
org.apache.commons:commons-lang3:3.9 jar does get download, along with a sha1 and md5 hash. The com.foo:bar:1.0.0-SNAPSHOT jar does NOT get downloaded. The sha1 and md5 do get downloaded.
I think my issue is that our repo does not have any sha256 hashes to download so the fetch (or build) fail with that error. However when i look at the actual rules_jvm_external https://github.com/bazelbuild/rules_jvm_external#checksum-verification it seems that sha256 is not mandatory?
Any ideas on what I'm doing wrong?
Bazel 1.1.0
Windows 10 Enterprise, version 1803, OS build 17134.1069
rules_jvm_external maintainer here.
ERROR: C:/project/BUILD:1:1: no such package '#maven//': Error while obtaining the sha256 checksum of v1/https/our-repo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar: java.io.IOException: ERROR: src/main/native/windows/process.cc(199): CreateProcessW("python" C:/users/name/_bazel_name/73nyktky/external/bazel_tools/tools/build_defs/hash/sha256.py C:/users/name/_bazel_name/73nyktky/external/maven/v1/https/ourrepo/prod/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar artifact.sha256): The system cannot find the file specified.
This is the real error where Windows' CreateProcessW is complaining that python is not available. This is previously reported in this issue as well. We added SHA256 checking in 2.3, and unfortunately this depends on Python.
Do you have python installed? There is an outstanding PR that drops the dependency here.
I have lots of SQLite tables that has now become hard to manage at the app side because of multiple DAO classes. I am using Bazel as my build system but I can't figure out how to use Room DB with Bazel build system.
If you use a Maven artifact resolver like rules_jvm_external, it'll look something like this.
In your WORKSPACE file, add the dependency on the Room compiler:
load("#rules_jvm_external//:specs.bzl", "maven")
maven_install(
name = "maven",
artifacts = [
"androidx.room:room-runtime:2.1.0-alpha04",
"androidx.room:room-compiler:2.1.0-alpha04",
"com.google.guava:guava:28.1-android",
maven.artifact("com.google.auto", "auto-common", "0.10", neverlink = True),
# .. other artifacts
],
repositories = [
"https://maven.google.com",
"https://jcenter.bintray.com",
],
)
In a BUILD file (e.g. <project root>/BUILD), create the java_plugin target to expose the annotation processor for Room:
java_plugin(
name = "androidx_room_room_compiler_plugin",
processor_class = "androidx.room.RoomProcessor",
deps = ["#maven//:androidx_room_room_compiler"],
neverlink = True,
)
java_library(
name = "androidx_room_room_compiler_library",
exports = [
"#maven//:androidx_room_room_compiler",
],
exported_plugins = [
":androidx_room_room_compiler_plugin"
],
)
Finally, in your app's BUILD file, depend on the Room compiler and runtime:
android_library(
name = "lib_prod",
# ...
deps = [
"#maven//:androidx_room_room_runtime",
"//:androidx_room_room_compiler_library",
],
)
I have ported an Android sample app that uses the Room and Lifecycle libraries to build with Bazel here: https://github.com/jin/BasicRxJavaSample-Bazel