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
Related
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 am not able to read test data from outside the src folder of project using java code. I am using bazel for testing java code.
The project hierarchy is as below:
Parent
Project1
Project2
Project3
Subproject1
Subproject2
data
Subfolder
Xyzzy.spi
Xyzzy.spl
BUILD.bazel
Src/main/java
Src/test/java
Com.test.sometest
Test1.java
Test2.java
BUILD.bazel
data/subfolder/BUILD.bazel:
exports_files(["Xyzzy.spi", "Xyzzy.spl"])
src/test/java/Com/test/sometest/BUILD.bazel:
load(
"//bazel:defs.bzl",
"java_library",
"java_test",
"java_binary",
)
java_test(
name = “subproject2”,
test_library = "subproject2_lib",
)
java_binary(
name = "example",
data = [
“//project3/subproject2/data/subfolder: Xyzzy.spi",
“//project3/subproject2/data/subfolder: Xyzzy.spl”,
],
runtime_deps = [":subproject2_lib"],
)
java_library(
name = "subproject2_lib",
testonly = True,
srcs = glob(
[
"*.java",
],
),
deps = [
"//:lombok",
“Path to point to build bazel of main java code”,
"#maven//:com_google_guava_guava",
"#maven//:commons_io_commons_io",
"#bazel_tools//tools/java/runfiles:runfiles",
"#maven//:junit_junit",
],
)
And java code to get path to file:
private static File CURRENT_INDEX_FILE=Paths.get("data/subfolder/Xyzzy.spi").toFile();
I am getting FileNotFoundException when running bazel test. Not sure where am doing it wrong. I tried google about this but no similar cases i found, mostly they have Maven Standard hierarchy but what should i change for this folder structure to work with.
Thanks
I am using the openapi-generator Gradle plugin to generate model files from the open API schema.
With these settings in build.gradle script everything seems ok:
openApiGenerate {
globalProperties = [
apis: "false",
modelDocs: "false",
models: "Pet"
]
generatorName = "java"
generateModelTests = false
inputSpec = "$rootDir/src/main/resources/schema/my_schema.json".toString()
outputDir = "$rootDir".toString()
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8",
serializationLibrary: "jackson",
library: "jersey1"
]
}
And the result classes are generated in the proper package:
The problem is here - I don't need them in my sources, I need them only at compile stage.
I want them to be generated in the build directory, to separate them from other logic.
But when I am changing the output-dir to "$buildDir/generated".toString() this happens:
Is there a way to get rid of the wrong packages "src.main.java"?
You can set the "sourceFolder" option to an empty string.
configOptions = [
sourceFolder: ""
]
This is an option of the generator not of the gradle plugin.
https://openapi-generator.tech/docs/generators/java
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
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.