Skip to content

Commit

Permalink
Example with a java library that doesn't contain any sources
Browse files Browse the repository at this point in the history
Merge-request: BAZEL-MR-1033
Merged-by: Tomasz Pasternak <Tomasz.Pasternak@jetbrains.com>
  • Loading branch information
tpasternak authored and qodana-bot committed Apr 15, 2024
1 parent 2483bab commit ac69b36
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions example/animals/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load(":generator.bzl", "animals_library_source")

java_library(
name = "consumer",
srcs = glob(["src/java/org/jetbrains/bsp/example/animals/consumer/LibraryConsumer.java"]),
deps = [":animals_lib"],
)

# This is a jvm library that doesn't have file sources. We must ensure we import it as a library, not as sources
# even if it is directly referred in projectview's targets section.
java_library(
name = "animals_lib",
srcs = [":animals"],
)

animals_library_source(
name = "animals",
)
24 changes: 24 additions & 0 deletions example/animals/generator.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def _animals_library_source_impl(ctx):
tc = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"]
cat_java = ctx.actions.declare_file("Cat.java")
ctx.actions.write(cat_java, """
package org.jetbrains.bsp.example.animals;
public class Cat {};
""")
src_jar = ctx.actions.declare_file("animals.srcjar")
java_common.pack_sources(ctx.actions, sources = [cat_java], java_toolchain = tc.java, output_source_jar = src_jar)

return [
DefaultInfo(files = depset([src_jar])),
]

animals_library_source = rule(
implementation = _animals_library_source_impl,
fragments = ["java"],
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
attrs = {
"_java_toolchain": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jetbrains.bsp.example.animals.consumer;

class LibraryConsumer {
org.jetbrains.bsp.example.animals.Cat cat;
}

0 comments on commit ac69b36

Please sign in to comment.