Skip to content

Commit

Permalink
Better Bazel jar -> Maven example (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
simontoens authored Feb 6, 2025
1 parent c18f67c commit 1fc8cb2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
28 changes: 2 additions & 26 deletions docs/bazel_maven_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,11 @@ java_library(

## 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.
Similar to above, we can build jars using Bazel, so that they can be used in Maven projects:

1. Make code changes to the library which you want to release a jar for.
1. Build the library by running `bazel build path/to/library/...`.
1. Run `bazel run @pomgen//maven -- -a pomgen,install -l path/to/library` to generate poms and install the jars to `~/.m2/repository`.
1. 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.
See [this example](../examples/hello-world#installing-maven-artifacts-into-the-local-maven-repository) for more information.
10 changes: 9 additions & 1 deletion examples/hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ The Maven Artifacts generated by pomgen can be installed into `~/.m2/repository`
bazel run @pomgen//maven -- -a install -l examples/hello-world/juicer
```

Note that before running `-a install`, pom.xml files must have been generated: `-a pomgen` must have run.

If you see this type of error:

```
Expand All @@ -94,8 +96,14 @@ This error means that pomgen did not find the jar file for the juicer module - t
- Run `bazel build examples/hello-world/...`
- If there are many upstream libraries in a large repository, it may be helpful to ask pomgen to build all upstream libraries - use the `build` action: `bazel run @pomgen//maven -- -a build -l examples/hello-world/juicer`

Finally, note that `-a` can take multiple actions, so the above can also be run like this:

```
bazel run @pomgen//maven -- -a pomgen,build,install -l examples/hello-world/juicer
```


### Using pomgen libraries in a Maven projects
### Referencing Bazel-built jars in a Maven projects

Once the `install` action ran successfully for `examples/hello-world/juicer`, you can use the `juicer` artifact in a Maven project. Try this pom.xml:

Expand Down

0 comments on commit 1fc8cb2

Please sign in to comment.