Skip to content

Commit

Permalink
chore: add container runtime to Java example
Browse files Browse the repository at this point in the history
Demonstrates how to move away from deprecated java_image in rules_docker
  • Loading branch information
alexeagle committed Oct 10, 2023
1 parent 23bc0fa commit 5495022
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
25 changes: 25 additions & 0 deletions java-maven/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("@container_structure_test//:defs.bzl", "container_structure_test")
load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test")
load("@rules_oci//oci:defs.bzl", "oci_image")

package(default_visibility = ["//visibility:public"])

Expand All @@ -24,3 +27,25 @@ java_test(
"@maven//:junit_junit",
],
)

tar(
name = "layer",
srcs = ["java-maven_deploy.jar"],
)

oci_image(
name = "image",
base = "@distroless_java",
entrypoint = [
"java",
"-jar",
"/java-maven-deploy.jar",
],
tars = [":layer"],
)

container_structure_test(
name = "container_test",
configs = ["container-structure-test.yaml"],
image = ":image",
)
11 changes: 11 additions & 0 deletions java-maven/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"Bazel dependencies"

bazel_dep(name = "aspect_bazel_lib", version = "2.0.0-beta1")
bazel_dep(name = "container_structure_test", version = "1.16.0")
bazel_dep(name = "rules_jvm_external", version = "5.3")
bazel_dep(name = "rules_oci", version = "1.2.0")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
Expand All @@ -15,3 +18,11 @@ maven.install(
],
)
use_repo(maven, "maven")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
name = "distroless_java",
digest = "sha256:161a1d97d592b3f1919801578c3a47c8e932071168a96267698f4b669c24c76d",
image = "gcr.io/distroless/java17",
)
use_repo(oci, "distroless_java")
14 changes: 13 additions & 1 deletion java-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Maven Java application
----------------------

This project demonstrates the usage of Bazel to retrieve dependencies from Maven
repositories.
repositories, build a program, and place it in an OCI container.

To build this example, you will need to [install
Bazel](http://bazel.io/docs/install.html).
Expand All @@ -26,3 +26,15 @@ Test the application by running:
```
$ bazel test :tests
```

Create a container image, suitable to push to a remote docker registry:

```
$ bazel build :image
```

Test that the image works when running inside a container runtime:

```
$ bazel test :container_test
```
7 changes: 7 additions & 0 deletions java-maven/container-structure-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://github.com/GoogleContainerTools/container-structure-test#command-tests
schemaVersion: 2.0.0
commandTests:
- name: test
command: java
args: ['-jar', '/java-maven_deploy.jar']
expectedOutput: ['Success: 1']

0 comments on commit 5495022

Please sign in to comment.