Skip to content

Add native-image support and test #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/native-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: native image build

on:
workflow_dispatch:
schedule:
- cron: '40 1 1 1 6'

jobs:
build:
runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v3
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
cache: 'maven'
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Versions
run: |
echo "GRAALVM_HOME: $GRAALVM_HOME"
echo "JAVA_HOME: $JAVA_HOME"
java --version
native-image --version
- name: Build with Maven
run: |
mvn clean install -DskipTests
cd test-native-image
mvn clean package -Pnative
./target/test-native-image
78 changes: 78 additions & 0 deletions test-native-image/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-parent</artifactId>
<version>1.2-SNAPSHOT</version>
</parent>

<groupId>org.example</groupId>
<artifactId>test-native-image</artifactId>

<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.plugin.nativeimage>0.9.27</version.plugin.nativeimage>
<mainClass>org.example.Main</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>validator-constraints</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-generator</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${version.plugin.nativeimage}</version>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
<configuration>
<buildArgs>
<buildArg>--no-fallback</buildArg>
<buildArg>-H:IncludeLocales=de,en</buildArg>
</buildArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
27 changes: 27 additions & 0 deletions test-native-image/src/main/java/org/example/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.example;


import io.avaje.validation.constraints.NotBlank;
import io.avaje.validation.constraints.Valid;

@Valid
public class Customer {

@NotBlank
final String name;
@NotBlank
final String email;

public Customer(String name, String email) {
this.name = name;
this.email = email;
}

public String name() {
return name;
}

public String email() {
return email;
}
}
32 changes: 32 additions & 0 deletions test-native-image/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.example;

import io.avaje.validation.ConstraintViolation;
import io.avaje.validation.Validator;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;

public class Main {

public static void main(String[] args) {
Validator validator = Validator.builder()
.addLocales(Locale.GERMAN)
.build();

var customer = new Customer("hello", "");
System.out.println("violations EN - " + validator.check(customer));
System.out.println("violations DE - " + validator.check(customer, Locale.GERMAN));

if (!"must not be blank".equals(first(validator.check(customer)).message())) {
System.exit(1);
}
if (!"darf nicht leer sein".equals(first(validator.check(customer, Locale.GERMAN)).message())) {
System.exit(1);
}
}

private static ConstraintViolation first(Set<ConstraintViolation> check) {
return new ArrayList<>(check).get(0);
}
}
22 changes: 22 additions & 0 deletions test-native-image/src/test/java/org/example/CustomerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.example;

import io.avaje.validation.ConstraintViolation;
import io.avaje.validation.Validator;
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.Set;

class CustomerTest {

@Test
void test() {
Validator validator = Validator.builder()
.addLocales(Locale.GERMAN)
.build();

var customer = new Customer("hello", "");
System.out.println("violations EN - " + validator.check(customer));
System.out.println("violations DE - " + validator.check(customer, Locale.GERMAN));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bundles": [
{
"name": "io.avaje.validation.Messages"
}
]
}