Skip to content

Commit 9e773de

Browse files
committed
feat(java): java implementation of dotprompt
1 parent 267ead1 commit 9e773de

23 files changed

+1696
-47
lines changed

.github/workflows/java.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
name: Java checks
18+
19+
on:
20+
push:
21+
branches:
22+
- main
23+
paths:
24+
- "java/**"
25+
- "spec/**"
26+
- ".github/workflows/java.yml"
27+
pull_request:
28+
paths:
29+
- "java/**"
30+
- "spec/**"
31+
- ".github/workflows/java.yml"
32+
33+
jobs:
34+
tests:
35+
runs-on: ubuntu-latest
36+
strategy:
37+
matrix:
38+
java-version: ['17', '21', '23']
39+
fail-fast: false
40+
41+
name: Java ${{ matrix.java-version }} Tests
42+
steps:
43+
- uses: actions/checkout@v4
44+
45+
- name: Set up JDK ${{ matrix.java-version }}
46+
uses: actions/setup-java@v4
47+
with:
48+
java-version: ${{ matrix.java-version }}
49+
distribution: 'temurin'
50+
51+
- name: Setup Bazel
52+
uses: bazelbuild/setup-bazelisk@v3
53+
54+
- name: Run Tests
55+
# Force Bazel to use the local JDK setup by actions/setup-java
56+
run: |
57+
bazel test \
58+
--java_runtime_version=local_jdk \
59+
--tool_java_runtime_version=local_jdk \
60+
--test_output=errors \
61+
//java/com/google/dotprompt:all \
62+
//java/com/google/dotprompt/smoke:SmokeTest

.github/workflows/publish_java.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
name: Publish Java Package
18+
#
19+
# Requirements for this workflow:
20+
#
21+
# 1. Secrets must be configured in the repository settings:
22+
# - OSSRH_USERNAME: The username for Sonatype OSSRH (Maven Central).
23+
# - OSSRH_TOKEN: The password or token for Sonatype OSSRH.
24+
# - MAVEN_GPG_PRIVATE_KEY: The ASCII-armored GPG private key for signing artifacts.
25+
# - MAVEN_GPG_PASSPHRASE: The passphrase for the GPG private key.
26+
#
27+
# 2. GPG Key Formatting:
28+
# - The private key should be exported using: `gpg --armor --export-secret-keys <ID>`
29+
# - Ensure the key has no empty lines or formatting issues when pasted into GitHub Secrets.
30+
#
31+
32+
on:
33+
release:
34+
types: [created]
35+
36+
jobs:
37+
publish-java:
38+
runs-on: ubuntu-latest
39+
permissions:
40+
contents: read
41+
steps:
42+
- uses: actions/checkout@v4
43+
44+
- name: Set up JDK 21
45+
uses: actions/setup-java@v4
46+
with:
47+
java-version: '21'
48+
distribution: 'temurin'
49+
50+
- name: Setup Bazel
51+
uses: bazelbuild/setup-bazelisk@v3
52+
53+
- name: Import GPG key
54+
id: import_gpg
55+
uses: crazy-max/ghaction-import-gpg@v6
56+
with:
57+
gpg_private_key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
58+
passphrase: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
59+
60+
- name: Publish to Maven Central
61+
env:
62+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
63+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
64+
run: |
65+
./scripts/publish-java.sh \
66+
"${{ steps.import_gpg.outputs.keyring }}" \
67+
"${{ secrets.MAVEN_GPG_PASSPHRASE }}" \
68+
"$MAVEN_USERNAME" \
69+
"$MAVEN_PASSWORD"

docs/contributing/coding_guidelines.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ You are an expert Python developer contributing to the Google Dotprompt project.
77
- **Language**: Python.
88
- **Environment Management**: Use `uv` for packaging and environment management.
99

10+
### Libraries
11+
- **JSON**: Use **Jackson** for JSON parsing and serialization. Avoid Gson.
12+
- **Testing**: Use **Google Truth** (`com.google.truth.Truth`) for assertions. Use JUnit 4/5 for test runners.
13+
- **Async**: Use **Dagger Producers** for asynchronous operations where applicable.
14+
- **Utilities**: Use **Guava** for immutable collections and common utilities.
15+
- **Dependency Injection**: Use **Dagger** for dependency injection.
16+
1017
## 2. Typing & Style
1118
- **Type Unions**: Use the pipe operator `|` (PEP 604) for union types (e.g., `int | str`) instead of `typing.Union`. Use `| None` for optional types.
1219
- **Generics**: Use standard collection generics (PEP 585) like `list`, `dict`, `tuple` (lowercase) for type hints instead of `typing.List`, `typing.Dict`.

java.MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ maven.install(
5050
"com.google.truth:truth:1.4.4",
5151
"com.github.jknack:handlebars:4.4.0",
5252
"com.google.guava:guava:33.4.8-jre",
53+
"com.fasterxml.jackson.core:jackson-databind:2.18.2",
54+
"com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.2",
5355
],
5456
lock_file = "//:maven_install.json",
5557
repositories = [

java/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Deploying Java Dotprompt
2+
3+
This document describes the process for releasing the `dotprompt` Java library to Maven Central.
4+
5+
## Prerequisites
6+
7+
1. **GPG Keys**: You need a GPG key pair to sign artifacts.
8+
* Export the private key: `gpg --armor --export-secret-keys <ID>`
9+
* Note the passphrase.
10+
11+
2. **Sonatype OSSRH Account**: You need an account on [s01.oss.sonatype.org](https://s01.oss.sonatype.org/) (or the legacy server) with access to the `com.google.dotprompt` group ID.
12+
13+
## GitHub Configuration
14+
15+
Configure the following secrets in the GitHub repository settings:
16+
17+
* `OSSRH_USERNAME`: Your Sonatype username.
18+
* `OSSRH_TOKEN`: Your Sonatype password or user token.
19+
* `MAVEN_GPG_PRIVATE_KEY`: The ASCII-armored GPG private key.
20+
* `MAVEN_GPG_PASSPHRASE`: The passphrase for your GPG key.
21+
22+
## Release Process
23+
24+
The release process is automated via GitHub Actions (`.github/workflows/publish_java.yml`).
25+
26+
1. **Create a Release**:
27+
* Go to the "Releases" section on GitHub.
28+
* Draft a new release.
29+
* Create a new tag (e.g., `v0.1.0`).
30+
* Publish the release.
31+
32+
2. **Automation**:
33+
* The `Publish Java Package` workflow will trigger automatically.
34+
* It sets up the JDK and Bazel environment.
35+
* It imports the GPG key.
36+
* It runs `scripts/publish-java.sh`, which invokes `bazel run //java/com/google/dotprompt:dotprompt_pkg.publish`.
37+
38+
3. **Verification**:
39+
* Check the workflow run logs for success.
40+
* Log in to Sonatype OSSRH to verify the staging repository if auto-release is not enabled, or check Maven Central (after a sync delay) if it is.
41+
42+
## Local Testing (Dry Run)
43+
44+
To verifying the build artifacts locally without publishing:
45+
46+
```bash
47+
bazel build //java/com/google/dotprompt:dotprompt_pkg
48+
```
49+
50+
This ensures the POM and JARs are correctly generated.

java/com/google/dotprompt/BUILD.bazel

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,80 @@
1313
# limitations under the License.
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
16+
17+
load("@rules_java//java:defs.bzl", "java_library", "java_test")
18+
load("@rules_jvm_external//:defs.bzl", "java_export")
19+
20+
java_library(
21+
name = "dotprompt",
22+
srcs = glob(
23+
["**/*.java"],
24+
exclude = ["**/*Test.java"],
25+
),
26+
deps = [
27+
"@maven//:com_fasterxml_jackson_core_jackson_annotations",
28+
"@maven//:com_fasterxml_jackson_core_jackson_core",
29+
"@maven//:com_fasterxml_jackson_core_jackson_databind",
30+
"@maven//:com_fasterxml_jackson_dataformat_jackson_dataformat_yaml",
31+
"@maven//:com_github_jknack_handlebars",
32+
"@maven//:com_google_guava_guava",
33+
],
34+
visibility = ["//visibility:public"],
35+
)
36+
37+
java_export(
38+
name = "dotprompt_pkg",
39+
maven_coordinates = "com.google.dotprompt:dotprompt:0.1.0",
40+
pom_template = "pom_template.xml",
41+
visibility = ["//visibility:public"],
42+
runtime_deps = [":dotprompt"],
43+
)
44+
45+
java_test(
46+
name = "DotpromptTest",
47+
srcs = ["DotpromptTest.java"],
48+
test_class = "com.google.dotprompt.DotpromptTest",
49+
deps = [
50+
":dotprompt",
51+
"@maven//:com_google_guava_guava",
52+
"@maven//:com_google_truth_truth",
53+
"@maven//:junit_junit",
54+
],
55+
)
56+
57+
java_test(
58+
name = "SpecTest",
59+
srcs = ["SpecTest.java"],
60+
data = ["//spec"],
61+
test_class = "com.google.dotprompt.SpecTest",
62+
deps = [
63+
":dotprompt",
64+
"@maven//:com_fasterxml_jackson_core_jackson_databind",
65+
"@maven//:com_fasterxml_jackson_dataformat_jackson_dataformat_yaml",
66+
"@maven//:com_google_guava_guava",
67+
"@maven//:com_google_truth_truth",
68+
"@maven//:junit_junit",
69+
],
70+
)
71+
72+
java_test(
73+
name = "PromptLoaderTest",
74+
srcs = ["PromptLoaderTest.java"],
75+
test_class = "com.google.dotprompt.PromptLoaderTest",
76+
deps = [
77+
":dotprompt",
78+
"@maven//:com_google_truth_truth",
79+
"@maven//:junit_junit",
80+
],
81+
)
82+
83+
java_test(
84+
name = "ModelTest",
85+
srcs = ["ModelTest.java"],
86+
test_class = "com.google.dotprompt.ModelTest",
87+
deps = [
88+
":dotprompt",
89+
"@maven//:com_google_truth_truth",
90+
"@maven//:junit_junit",
91+
],
92+
)

0 commit comments

Comments
 (0)