Bazel - Align package groups into modules

Answered

Is there a way to group a set of packages in a Bazel workspace like they were traditional Java packages? I have seen some organization use rules_gradle to map collections of directory-nested targets into Modules as divisible by the IntelliJ windows tab. The biggest issue with the current setup is importing dependencies is a exercise in frustration. I need to EXPLICITLY add each dependency i want to use to the BUILD file, resync, then use it. Finding relevant code is impossible under this setup, unless i import every single JAR I have, then perform cleanup. I understand that the traditional Java package structure is pretty irrelevant in the Bazel world, but it's seems that IntelliJ features are deeply tied to that mindset still.

I would think that project views could handle this, but it's failed to do so, or at least there is no good example repo. Lot of bazel setup's are internal repo's, with little knowledge flowing back into the community. If anyone has good idea's on how to do this, that would be great.

Related Tickets

0
3 comments

Hi Mark Vibbix 

Answering your question, you cannot make Bazel treat a directory tree like a single Java module that “just sees” everything without declaring deps. Bazel requires explicit dependencies for builds.

  • Alternatively, to give IntelliJ a more modular feel and decrease jar replacement frequency, you can:
    • Create umbrella/alias targets that export curated groups of libraries (e.g., //deps:logging, //deps:http). Your code depends on a few aliases, not many jars.
    • Using exports (not just deps) on those umbrellas so IntelliJ gets transitive navigation/completion.
    • Partitioning code with visibility/package_group to mimic module boundaries.
    • Keeping your .bazelproject focused: list only relevant directories and key aggregate targets; consider derive_targets_from_directories.
    • Centralizing third-party deps (rules_jvm_external) and re-exporting them via grouped targets.

Result: You still declare deps, but at a higher level and in fewer places; IntelliJ navigation works via exported transitive deps, approximating traditional modules without fighting Bazel’s model. I have a very basic example of it, let me know if you want to take a look at it.

1

Thanks for the quick response Monica.
A very basic example would be great. As of now, i am I am using the https://github.com/bravit/bazel-java-kotlin-sample example that was mentioned in the release notes for the Bazel plugin, but it's flexible layout seems to break the maven-style package auto-detection feature in the newer releases of the Bazel plugin.

I guess what best describes what I am looking for is IDE-Scoped / Developer-scoped dependencies. An example of this would be Spring Boot Tools or Reactor Debug. They exist only when running in the IDE.

It seems some parts of BazelProject get close to this.

0

Hi Mark Vibbix 

Here's the project I have: https://github.com/moniss/BazelAlikeJava.git. You can check against BazelProject explanation, as the project I'm sharing makes use of *.bazelproject. 

I hope it helps!

0

Please sign in to leave a comment.