Skip to content

Latest commit

 

History

History
81 lines (63 loc) · 2.58 KB

bazel_maven_workflow.md

File metadata and controls

81 lines (63 loc) · 2.58 KB

Working across Bazel and Maven

Some users may need to work across Bazel and Maven projects. There are two common use cases:

Use jars produced by Maven in a Bazel project

  1. Copy or link the jar to same directory the BUILD file lives in (or a subdirectory)
  2. Create a java_import rule that points to the jar
  3. Replace the maven_install based dependency (starting with @) with the java_import target (starting with //)
  4. Build as usual

Example

Copy the jar.

cd $BUILD_DIR
cp ~/.m2/repository/org/slf4j/slf4j-api/1.7.29/slf4j-api-1.7.30-SNAPSHOT.jar .

Or symlink to it. The advantage of using a link is that the jar can be updated by Maven without having to re-copy it.

cd $BUILD_DIR
ln -s ~/.m2/repository/org/slf4j/slf4j-api/1.7.29/slf4j-api-1.7.30-SNAPSHOT.jar .

Create java_import rule in the BUILD file. In this example, we'll use slf4j-api. This assumes that the jar the has been copied or symlinked to the same directory as the BUILD file.

java_import(
    name = "org_slf4j_slf4j_api",
    jars = [":slf4j-api-1.7.30-SNAPSHOT.jar"],
)

Then, in the deps attribute(s) replace the maven_install rule for slf4j-api with the java_import rule that was just created.

java_library(
    name = "fruit-api",
    srcs = glob(["src/main/java/**/*.java"]),
    visibility = ["//visibility:public"],
    deps = [
      ...
      ":org_slf4j_slf4j_api", # instead of "@maven//:org_slf4j_slf4j_api"
      ...
    ],
)

Build jars with Bazel and use them in a Maven project

Similar to above, we can build jars using Bazel, so that they can be used in Maven projects. See below for detailed steps.

  1. Make code changes to the library which you want to release a jar for.
  2. Build the library by running bazel build path/to/library/....
  3. Run bazel run @pomgen//maven -- -a pomgen,install -l path/to/library to generate poms and install the jars to ~/.m2/repository.
  4. Update the pom.xml in the consuming Maven project to use the right jar version

Example

Make some code changes in examples/hello-world/healthyfoods/fruit-api.

Run bazel build.

bazel build //examples/hello-world/healthyfoods/...

Generate pom(s) and install artifacts into ~/.m2/repository:

bazel run @pomgen//maven -- -a pomgen,install -l examples/hello-world/healthyfoods

Then, update the pom.xml of the consuming Maven project.

<dependency>
    <groupId>com.pomgen.example</groupId>
    <artifactId>fruit-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Now you can compile your Maven project, it will use the latest jars produced by Bazel.