How can I run a JavaFX program using Bazel? - java

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.

Related

how to read test data file from outside src folder in java test via bazel

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

openapi-generator gradle plugin output directory

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

In Bazel, fetching jar from private maven repository fails with 403

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

Bazel sha256 checksum

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.

How to use Room Database in Android with Bazel?

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

Categories