Skip to content

Add maven examples for Java API - #3783

Merged
csukuangfj merged 5 commits into
k2-fsa:masterfrom
csukuangfj:refactor-jar
Jul 22, 2026
Merged

csukuangfj merged 5 commits into
k2-fsa:masterfrom
csukuangfj:refactor-jar

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2439
Fixes #2665

Note that we use jitpack.

cc @Chamukuy

Summary by CodeRabbit

  • New Features

    • Added a Maven example project for building and running Sherpa-ONNX Java applications.
    • Added support for platform-specific native libraries and all-in-one Maven dependency packaging.
    • Added an executable example that displays version and build information.
  • Documentation

    • Added Maven setup, build, run, packaging, and verification instructions.
  • Tests

    • Expanded automated Maven testing across supported operating systems and architectures.
    • Added validation that packaged JARs include the required native libraries.

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds Maven publication setup and a cross-platform Maven example, introduces matrix-based Maven packaging tests, updates JitPack artifact installation, and changes Java jar versioning and release handling. The previous Java API Maven metadata and guide files are removed.

Changes

Maven Java packaging

Layer / File(s) Summary
Artifact installation
jitpack.yml
JitPack downloads and installs the AAR, JVM jar, and platform-specific native library jars with explicit Maven coordinates.
Maven example project
java-api-examples/maven-examples/*
Adds a Maven project, executable VersionTest entry point, dependency options, shading configuration, build instructions, output examples, and generated-file exclusions.
Matrix Maven validation
.github/workflows/run-java-test.yaml
Adds two OS and architecture matrix jobs that build Maven jars, verify embedded native binaries, and execute the examples using alternative dependency strategies.
Versioned jar release handling
.github/workflows/jar.yaml
Uses unprefixed versions for jar names, updates inspection patterns, changes the release tag to v1.13.4, and conditionally uploads JVM jars on the ARM runner.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MavenCI
  participant MavenExample
  participant JitPack
  participant ShadedJar
  MavenCI->>MavenExample: select platform dependency and run mvn package
  MavenExample->>JitPack: resolve Sherpa-ONNX JVM and native artifacts
  JitPack-->>MavenExample: return Maven dependencies
  MavenExample->>ShadedJar: create shaded executable jar
  ShadedJar-->>MavenCI: verify native binaries and execute VersionTest
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main addition: Maven examples for the Java API.
Linked Issues check ✅ Passed The changes add Maven examples, JitPack publishing, and artifact/version handling that match issues #2439 and #2665.
Out of Scope Changes check ✅ Passed The diff stays focused on Maven packaging, publishing, tests, and docs related to the Java API.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Jul 22, 2026
@csukuangfj
csukuangfj merged commit a3b58a1 into k2-fsa:master Jul 22, 2026
1 check was pending
@csukuangfj
csukuangfj deleted the refactor-jar branch July 22, 2026 10:47

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
.github/workflows/run-java-test.yaml (3)

432-432: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

New checkout steps don't set persist-credentials: false.

Both new jobs check out the repo and then run mvn, resolving dependencies from a third-party JitPack repository, while the default GitHub token remains persisted in .git/config for the rest of the job.

🔒 Suggested fix
       - uses: actions/checkout@v4
+        with:
+          persist-credentials: false

Also applies to: 547-547

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/run-java-test.yaml at line 432, Update both new
actions/checkout@v4 steps in the affected jobs to set persist-credentials to
false, ensuring the GitHub token is not retained in the repository’s Git
configuration while subsequent Maven dependency resolution runs.

Source: Linters/SAST tools


445-492: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Regex-based pom.xml mutation is brittle.

The python script matches native-lib dependency blocks purely on the exact 8-space indentation and comment formatting of pom.xml. Any future reformatting of pom.xml will silently fail to uncomment the target block (no error raised), leaving the build without a native-lib dependency for that platform. Consider using Maven profiles (selected via -P<profile>) activated per platform instead of text-mangling the pom.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/run-java-test.yaml around lines 445 - 492, Replace the
regex-based pom.xml mutation in the “Update pom.xml for this platform” workflow
step with Maven profile selection using -P<profile> for matrix.native_lib.
Define or reuse platform-specific profiles so the selected profile activates its
native-lib dependency without relying on indentation or comment formatting, and
remove the Python text-mangling logic.

560-634: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Approach‑1 pom.xml is duplicated inline instead of reused.

This heredoc re-declares the compiler/jar/shade plugin versions that already exist in java-api-examples/maven-examples/pom.xml's Approach‑1 comment block. Any future plugin-version bump in the checked-in pom.xml won't propagate here, and the two will drift.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/run-java-test.yaml around lines 560 - 634, Replace the
duplicated heredoc in the “Write pom.xml for Approach 1” workflow step with
reuse of the checked-in Approach-1 configuration from
java-api-examples/maven-examples/pom.xml. Ensure the generated pom preserves the
existing dependency and build behavior while sourcing compiler, jar, and shade
plugin versions from the canonical file so future updates stay synchronized.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/run-java-test.yaml:
- Around line 515-517: Update the native-library verification commands near the
“Native libs in jar” checks to fail when the archive contains no .so, .dylib, or
.dll entries. Remove the unconditional `|| true` suppression and add an explicit
non-empty match assertion, preserving successful execution when at least one
native library is found; apply the same change to both verification locations.

In `@jitpack.yml`:
- Around line 4-20: Update the before_install downloads and all mvn
install:install-file commands to use JitPack’s provided version variable instead
of hardcoded 1.13.4. Set the installed artifacts’ groupId to
com.github.k2-fsa.sherpa-onnx so they match the example Maven dependencies, and
ensure the Approach 1 sherpa-onnx dependency declares type aar.

---

Nitpick comments:
In @.github/workflows/run-java-test.yaml:
- Line 432: Update both new actions/checkout@v4 steps in the affected jobs to
set persist-credentials to false, ensuring the GitHub token is not retained in
the repository’s Git configuration while subsequent Maven dependency resolution
runs.
- Around line 445-492: Replace the regex-based pom.xml mutation in the “Update
pom.xml for this platform” workflow step with Maven profile selection using
-P<profile> for matrix.native_lib. Define or reuse platform-specific profiles so
the selected profile activates its native-lib dependency without relying on
indentation or comment formatting, and remove the Python text-mangling logic.
- Around line 560-634: Replace the duplicated heredoc in the “Write pom.xml for
Approach 1” workflow step with reuse of the checked-in Approach-1 configuration
from java-api-examples/maven-examples/pom.xml. Ensure the generated pom
preserves the existing dependency and build behavior while sourcing compiler,
jar, and shade plugin versions from the canonical file so future updates stay
synchronized.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dc653307-bdec-4e76-926f-1d17f9c12537

📥 Commits

Reviewing files that changed from the base of the PR and between 2d8286d and c4da2fb.

📒 Files selected for processing (10)
  • .github/workflows/jar.yaml
  • .github/workflows/run-java-test.yaml
  • java-api-examples/maven-examples/.gitignore
  • java-api-examples/maven-examples/README.md
  • java-api-examples/maven-examples/pom.xml
  • java-api-examples/maven-examples/src/main/java/com/k2fsa/sherpa/onnx/example/VersionTest.java
  • jitpack.yml
  • sherpa-onnx/java-api/pom.xml
  • sherpa-onnx/java-api/readme.md
  • sherpa-onnx/java-api/readme.zh.md
💤 Files with no reviewable changes (3)
  • sherpa-onnx/java-api/pom.xml
  • sherpa-onnx/java-api/readme.md
  • sherpa-onnx/java-api/readme.zh.md

Comment on lines +515 to +517
echo "=== Native libs in jar ==="
unzip -l target/sherpa-onnx-maven-example-1.0-SNAPSHOT.jar | grep -E "\.so$|\.dylib$|\.dll$" || true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Native-lib verification step can't actually fail.

grep ... || true means this step always exits 0 even when zero native binaries are found in the jar, so it silently passes regardless of whether the jar actually bundles the expected .so/.dylib/.dll. Since the stated purpose of this step is to verify native binaries are present, an empty match should fail the job (e.g., assert non-zero line count) rather than be swallowed.

✅ Suggested fix
-          echo "=== Native libs in jar ==="
-          unzip -l target/sherpa-onnx-maven-example-1.0-SNAPSHOT.jar | grep -E "\.so$|\.dylib$|\.dll$" || true
+          echo "=== Native libs in jar ==="
+          unzip -l target/sherpa-onnx-maven-example-1.0-SNAPSHOT.jar | grep -E "\.so$|\.dylib$|\.dll$"

Also applies to: 657-659

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/run-java-test.yaml around lines 515 - 517, Update the
native-library verification commands near the “Native libs in jar” checks to
fail when the archive contains no .so, .dylib, or .dll entries. Remove the
unconditional `|| true` suppression and add an explicit non-empty match
assertion, preserving successful execution when at least one native library is
found; apply the same change to both verification locations.

Comment thread jitpack.yml
Comment on lines 4 to +20
before_install:
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-1.13.4.aar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-jvm-1.13.4.jar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-native-lib-linux-aarch64-1.13.4.jar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-native-lib-linux-x64-1.13.4.jar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-native-lib-osx-aarch64-1.13.4.jar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-native-lib-osx-x64-1.13.4.jar
- wget https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.13.4/sherpa-onnx-native-lib-win-x64-1.13.4.jar

install:
- FILE="-Dfile=sherpa-onnx-1.13.4.aar"
- mvn install:install-file $FILE -DgroupId=com.k2fsa.sherpa.onnx -DartifactId=sherpa-onnx -Dversion=1.13.4 -Dpackaging=aar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-1.13.4.aar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx -Dversion=1.13.4 -Dpackaging=aar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-jvm-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-jvm -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-native-lib-linux-aarch64-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-native-lib-linux-aarch64 -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-native-lib-linux-x64-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-native-lib-linux-x64 -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-native-lib-osx-aarch64-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-native-lib-osx-aarch64 -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-native-lib-osx-x64-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-native-lib-osx-x64 -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true
- mvn install:install-file -Dfile=sherpa-onnx-native-lib-win-x64-1.13.4.jar -DgroupId=com.github.k2-fsa -DartifactId=sherpa-onnx-native-lib-win-x64 -Dversion=1.13.4 -Dpackaging=jar -DgeneratePom=true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win

Align the published Maven coordinates with the example dependencies.

  • jitpack.yml installs sherpa-onnx-jvm and sherpa-onnx-native-lib-* under com.github.k2-fsa, but java-api-examples/maven-examples/pom.xml expects com.github.k2-fsa.sherpa-onnx.
  • The Approach 1 sherpa-onnx dependency also needs type=aar; without it, Maven will resolve a JAR and miss the published artifact.
  • Hardcoding 1.13.4 here makes the install script diverge from the requested ref/version; use the JitPack-provided version variable instead.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@jitpack.yml` around lines 4 - 20, Update the before_install downloads and all
mvn install:install-file commands to use JitPack’s provided version variable
instead of hardcoded 1.13.4. Set the installed artifacts’ groupId to
com.github.k2-fsa.sherpa-onnx so they match the example Maven dependencies, and
ensure the Approach 1 sherpa-onnx dependency declares type aar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

java-api的示例里找不到1.0.1版本的依赖 独立 java-api 部分为 maven工程 推送到maven库

1 participant