Skip to content

Commit db865f5

Browse files
committed
feat: Java bindings
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent a487b68 commit db865f5

15 files changed

Lines changed: 965 additions & 1 deletion

File tree

.github/workflows/build.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,30 @@ jobs:
438438
set -e
439439
yarn test
440440
441+
test-java:
442+
name: Java JNI tests
443+
runs-on: ubuntu-22.04
444+
steps:
445+
- uses: actions/checkout@v4
446+
447+
- run: cargo build --release
448+
working-directory: bindings/java
449+
450+
- name: Set up Java
451+
uses: actions/setup-java@v4
452+
with:
453+
distribution: temurin
454+
java-version: 21
455+
456+
- name: Setup Gradle
457+
uses: gradle/actions/setup-gradle@v4
458+
459+
- name: Run Java tests
460+
working-directory: bindings/java
461+
run: |
462+
echo "nativeLibraryPath=../target/release" >> gradle.properties
463+
gradle clean test --no-daemon
464+
441465
test-python:
442466
strategy:
443467
fail-fast: false

.github/workflows/java-release.yml

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
name: "[Java] Release"
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
tags:
7+
- java-v*
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
env:
18+
JAVA_VERSION: "21"
19+
20+
jobs:
21+
build-native-libs:
22+
strategy:
23+
matrix:
24+
include:
25+
- os: ubuntu-22.04
26+
target: x86_64-unknown-linux-gnu
27+
lib: libcss_inline.so
28+
platform: linux-x86_64
29+
- os: ubuntu-22.04
30+
target: aarch64-unknown-linux-gnu
31+
lib: libcss_inline.so
32+
platform: linux-aarch64
33+
cross: true
34+
35+
- os: macos-13
36+
target: x86_64-apple-darwin
37+
lib: libcss_inline.dylib
38+
platform: darwin-x86_64
39+
- os: macos-14
40+
target: aarch64-apple-darwin
41+
lib: libcss_inline.dylib
42+
platform: darwin-aarch64
43+
44+
- os: windows-2022
45+
target: x86_64-pc-windows-msvc
46+
lib: css_inline.dll
47+
platform: win32-x86_64
48+
49+
runs-on: ${{ matrix.os }}
50+
steps:
51+
- uses: actions/checkout@v4
52+
53+
- uses: dtolnay/rust-toolchain@master
54+
with:
55+
toolchain: stable
56+
targets: ${{ matrix.target }}
57+
58+
- name: Install cross
59+
if: matrix.cross
60+
run: cargo install cross --git https://github.com/cross-rs/cross
61+
62+
- name: Build native library
63+
working-directory: bindings/java
64+
run: |
65+
if [ "${{ matrix.cross }}" = "true" ]; then
66+
cross build --release --target ${{ matrix.target }}
67+
else
68+
cargo build --release --target ${{ matrix.target }}
69+
fi
70+
71+
- name: Upload native library
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: native-${{ matrix.platform }}
75+
if-no-files-found: error
76+
path: bindings/java/target/${{ matrix.target }}/release/${{ matrix.lib }}
77+
78+
build-jar:
79+
runs-on: ubuntu-22.04
80+
needs: build-native-libs
81+
steps:
82+
- uses: actions/checkout@v4
83+
84+
- name: Set up Java
85+
uses: actions/setup-java@v4
86+
with:
87+
distribution: "temurin"
88+
java-version: ${{ env.JAVA_VERSION }}
89+
90+
- name: Download all native libraries
91+
uses: actions/download-artifact@v4
92+
with:
93+
path: native-libs
94+
95+
- name: Assemble JAR with native libraries
96+
working-directory: bindings/java
97+
run: |
98+
# Create resources directory structure
99+
mkdir -p src/main/resources/org/cssinline/native/{linux-x86_64,linux-aarch64,darwin-x86_64,darwin-aarch64,win32-x86_64}
100+
101+
# Copy native libraries to correct locations
102+
cp ../../native-libs/native-linux-x86_64/libcss_inline.so src/main/resources/org/cssinline/native/linux-x86_64/
103+
cp ../../native-libs/native-linux-aarch64/libcss_inline.so src/main/resources/org/cssinline/native/linux-aarch64/
104+
cp ../../native-libs/native-darwin-x86_64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-x86_64/
105+
cp ../../native-libs/native-darwin-aarch64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-aarch64/
106+
cp ../../native-libs/native-win32-x86_64/css_inline.dll src/main/resources/org/cssinline/native/win32-x86_64/
107+
108+
# Extract version from tag
109+
echo "VERSION=${GITHUB_REF#refs/tags/java-v}" >> $GITHUB_ENV
110+
111+
# Build JAR
112+
gradle build -Pversion=${{ env.VERSION }}
113+
114+
- name: Upload JAR artifact
115+
uses: actions/upload-artifact@v4
116+
with:
117+
name: java-jar
118+
if-no-files-found: error
119+
path: bindings/java/build/libs/*.jar
120+
121+
test-jar:
122+
needs: build-jar
123+
strategy:
124+
matrix:
125+
include:
126+
- os: ubuntu-22.04
127+
platform: linux
128+
- os: macos-13
129+
platform: darwin-x86_64
130+
- os: macos-14
131+
platform: darwin-aarch64
132+
- os: windows-2022
133+
platform: windows
134+
135+
runs-on: ${{ matrix.os }}
136+
steps:
137+
- uses: actions/checkout@v4
138+
139+
- name: Set up Java
140+
uses: actions/setup-java@v4
141+
with:
142+
distribution: "temurin"
143+
java-version: ${{ env.JAVA_VERSION }}
144+
145+
- name: Download JAR
146+
uses: actions/download-artifact@v4
147+
with:
148+
name: java-jar
149+
path: jars
150+
151+
- name: Test JAR on ${{ matrix.platform }}
152+
working-directory: bindings/java
153+
run: |
154+
# Copy JAR to build/libs for gradle to find
155+
mkdir -p build/libs
156+
cp ../../jars/*.jar build/libs/
157+
158+
# Run tests to verify native library loads and works
159+
gradle test --info
160+
161+
- name: Integration test
162+
working-directory: bindings/java
163+
run: |
164+
java -cp "build/libs/*:build/classes/java/main" -c "
165+
import org.cssinline.CssInline;
166+
String result = CssInline.inline(\"<style>h1{color:red}</style><h1>Test</h1>\");
167+
if (!result.contains(\"style=\\\"color: red;\\\"\")) {
168+
System.exit(1);
169+
}
170+
System.out.println(\"✓ Integration test passed on ${{ matrix.platform }}\");
171+
"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ into:
4141
- Optionally caches external stylesheets
4242
- Works on Linux, Windows, and macOS
4343
- Supports HTML5 & CSS3
44-
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
44+
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [Java](https://github.com/Stranger6667/css-inline/tree/master/bindings/java), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
4545
- Command Line Interface
4646

4747
## Playground

bindings/java/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gradle/
2+
build/

bindings/java/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "css_inline"
3+
version = "0.15.0"
4+
edition = "2024"
5+
authors = ["Dmitry Dygalo <dmitry@dygalo.dev>"]
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
path = "src/main/rust/lib.rs"
10+
11+
[dependencies]
12+
jni = "0.21.1"
13+
14+
[dependencies.css-inline]
15+
path = "../../css-inline"
16+
version = "*"
17+
default-features = false
18+
features = ["http", "file", "stylesheet-cache"]

0 commit comments

Comments
 (0)