Skip to content

Commit 5f7b180

Browse files
committed
fix: remove jcommander dep, fix native image generation, tentative github action
1 parent c57ceb6 commit 5f7b180

File tree

9 files changed

+406
-95
lines changed

9 files changed

+406
-95
lines changed

.github/workflows/native.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: GraalVM Native Image builds
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
name: HelloWorld on ${{ matrix.os }}
6+
runs-on: ${{ matrix.os }}
7+
strategy:
8+
matrix:
9+
os: [macos-latest, windows-latest, ubuntu-latest]
10+
steps:
11+
- uses: actions/checkout@v4
12+
13+
- uses: graalvm/setup-graalvm@v1
14+
with:
15+
java-version: '22'
16+
distribution: 'graalvm'
17+
github-token: ${{ secrets.GITHUB_TOKEN }}
18+
native-image-job-reports: 'true'
19+
20+
- name: Build and run HelloWorld.java
21+
run: |
22+
./create-image.sh
23+
24+
# - name: Upload binary
25+
# uses: actions/upload-artifact@v4
26+
# with:
27+
# name: helloworld-${{ matrix.os }}
28+
# path: helloworld*

create-image.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# Copyright 2024 Nacho Brito
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
#see: https://graalvm.github.io/native-build-tools/latest/maven-plugin-quickstart.html#_build_a_native_executable_by_detecting_resources_with_the_agent
18+
mvn clean package
19+
20+
#todo: fix this: the native tests execution fails with "Try runing with '--enable-preview' error message
21+
# mvn -Pnative -Dagent test
22+
mvn -Pnative -Dagent exec:exec@java-agent
23+
mvn -DskipTests=true -Pnative -Dagent package

pom.xml

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
<version>1.0-SNAPSHOT</version>
2525

2626
<name>json-schema-compiler</name>
27-
<!-- FIXME change it to the project's website -->
28-
<url>http://www.example.com</url>
27+
<url>https://github.com/NachoBrito/json-schema-compiler</url>
2928

3029
<properties>
3130
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3231
<maven.compiler.release>22</maven.compiler.release>
3332
<json-schema-ref-parser-jvm.version>0.8.7</json-schema-ref-parser-jvm.version>
3433
<jcommander.version>2.0</jcommander.version>
34+
<native.maven.plugin.version>0.10.4</native.maven.plugin.version>
35+
<mainClass>es.nachobrito.jsonschema.compiler.application.cli.App</mainClass>
36+
<imageName>json-schema-compiler</imageName>
3537
</properties>
3638

3739
<dependencyManagement>
@@ -43,16 +45,26 @@
4345
<type>pom</type>
4446
<scope>import</scope>
4547
</dependency>
48+
<dependency>
49+
<groupId>org.yaml</groupId>
50+
<artifactId>snakeyaml</artifactId>
51+
<version>2.2</version>
52+
</dependency>
4653
</dependencies>
4754
</dependencyManagement>
4855

4956
<dependencies>
57+
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-nop</artifactId>
61+
<version>2.0.13</version>
62+
</dependency>
5063
<dependency>
5164
<groupId>org.junit.jupiter</groupId>
5265
<artifactId>junit-jupiter-api</artifactId>
5366
<scope>test</scope>
5467
</dependency>
55-
<!-- Optionally: parameterized tests support -->
5668
<dependency>
5769
<groupId>org.junit.jupiter</groupId>
5870
<artifactId>junit-jupiter-params</artifactId>
@@ -62,11 +74,12 @@
6274
<groupId>io.github.zenwave360</groupId>
6375
<artifactId>json-schema-ref-parser-jvm</artifactId>
6476
<version>${json-schema-ref-parser-jvm.version}</version>
65-
</dependency>
66-
<dependency>
67-
<groupId>org.jcommander</groupId>
68-
<artifactId>jcommander</artifactId>
69-
<version>${jcommander.version}</version>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>org.slf4j</groupId>
80+
<artifactId>slf4j-api</artifactId>
81+
</exclusion>
82+
</exclusions>
7083
</dependency>
7184
</dependencies>
7285

@@ -142,7 +155,7 @@
142155
<manifest>
143156
<addClasspath>true</addClasspath>
144157
<classpathPrefix>lib/</classpathPrefix>
145-
<mainClass>es.nachobrito.jsonschema.compiler.application.cli.App</mainClass>
158+
<mainClass>${mainClass}</mainClass>
146159
</manifest>
147160
</archive>
148161
</configuration>
@@ -153,7 +166,7 @@
153166
<configuration>
154167
<archive>
155168
<manifest>
156-
<mainClass>es.nachobrito.jsonschema.compiler.application.cli.App</mainClass>
169+
<mainClass>${mainClass}</mainClass>
157170
</manifest>
158171
</archive>
159172
<descriptorRefs>
@@ -170,6 +183,94 @@
170183
</execution>
171184
</executions>
172185
</plugin>
186+
<plugin>
187+
<groupId>org.codehaus.mojo</groupId>
188+
<artifactId>exec-maven-plugin</artifactId>
189+
<version>3.1.1</version>
190+
<executions>
191+
<execution>
192+
<id>java-agent</id>
193+
<goals>
194+
<goal>exec</goal>
195+
</goals>
196+
<configuration>
197+
<executable>java</executable>
198+
<workingDirectory>${project.build.directory}</workingDirectory>
199+
<arguments>
200+
<argument>--enable-preview</argument>
201+
<argument>-classpath</argument>
202+
<classpath/>
203+
<argument>${mainClass}</argument>
204+
<argument>--output</argument>
205+
<argument>${project.build.directory}/kk</argument>
206+
<argument>--package-name</argument>
207+
<argument>es.nachobrito.sample</argument>
208+
<argument>${project.basedir}/src/test/resources/test-schemas/Person.json</argument>
209+
</arguments>
210+
211+
</configuration>
212+
</execution>
213+
<execution>
214+
<id>native</id>
215+
<goals>
216+
<goal>exec</goal>
217+
</goals>
218+
<configuration>
219+
<executable>${project.build.directory}/${imageName}</executable>
220+
<workingDirectory>${project.build.directory}</workingDirectory>
221+
</configuration>
222+
</execution>
223+
</executions>
224+
</plugin>
173225
</plugins>
174226
</build>
227+
<profiles>
228+
<profile>
229+
<id>native</id>
230+
<dependencies>
231+
<dependency>
232+
<groupId>org.junit.platform</groupId>
233+
<artifactId>junit-platform-launcher</artifactId>
234+
<version>1.11.0</version>
235+
<scope>test</scope>
236+
</dependency>
237+
</dependencies>
238+
<build>
239+
<plugins>
240+
<plugin>
241+
<groupId>org.graalvm.buildtools</groupId>
242+
<artifactId>native-maven-plugin</artifactId>
243+
<version>${native.maven.plugin.version}</version>
244+
<extensions>true</extensions>
245+
246+
<executions>
247+
<execution>
248+
<id>build-native</id>
249+
<goals>
250+
<goal>compile-no-fork</goal>
251+
</goals>
252+
<phase>package</phase>
253+
</execution>
254+
<execution>
255+
<id>test-native</id>
256+
<goals>
257+
<goal>test</goal>
258+
</goals>
259+
<phase>test</phase>
260+
</execution>
261+
</executions>
262+
<configuration>
263+
<fallback>false</fallback>
264+
<agent>
265+
<enabled>true</enabled>
266+
</agent>
267+
<buildArgs>
268+
<buildArg>--enable-preview</buildArg>
269+
</buildArgs>
270+
</configuration>
271+
</plugin>
272+
</plugins>
273+
</build>
274+
</profile>
275+
</profiles>
175276
</project>

src/main/java/es/nachobrito/jsonschema/compiler/application/cli/App.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616

1717
package es.nachobrito.jsonschema.compiler.application.cli;
1818

19-
import com.beust.jcommander.JCommander;
20-
import es.nachobrito.jsonschema.compiler.application.jcommander.Params;
2119
import es.nachobrito.jsonschema.compiler.domain.Compiler;
2220
import es.nachobrito.jsonschema.compiler.infrastructure.jsonrefparser.JsonSchemaReader;
2321

24-
import java.net.URI;
25-
2622
public class App {
2723
public static void main(String[] args) {
28-
var params = new Params();
29-
JCommander.newBuilder()
30-
.addObject(params)
31-
.build()
32-
.parse(args);
24+
var params = InputParameters.of(args);
3325

3426
var compiler = new Compiler(params, new JsonSchemaReader());
35-
var uri = URI.create(params.getSchemaFile());
36-
compiler.compile(uri);
27+
var jsonSchemaFile = params.getJsonSchemaFile();
28+
var jsonSchemaCode = params.getJsonSchemaCode();
29+
if (jsonSchemaFile.isPresent()) {
30+
compiler.compile(jsonSchemaFile.get().toUri());
31+
} else if (jsonSchemaCode.isPresent()) {
32+
compiler.compile(jsonSchemaCode.get());
33+
} else {
34+
System.err.println(
35+
"Nothing to compile. Provide a path to a json schema file as an argument, or the JSON code through stdin");
36+
System.exit(1);
37+
}
3738
}
3839
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2024 Nacho Brito
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.nachobrito.jsonschema.compiler.application.cli;
18+
19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStreamReader;
22+
import java.nio.file.Path;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.Optional;
26+
import java.util.stream.Collectors;
27+
28+
/**
29+
* Parses allowed input parameters and implements {@link
30+
* es.nachobrito.jsonschema.compiler.domain.InputParameters}
31+
*/
32+
public class InputParameters implements es.nachobrito.jsonschema.compiler.domain.InputParameters {
33+
private static final String JSON_SCHEMA_FILE = "JSON_SCHEMA_FILE";
34+
private static final String PACKAGE = "PACKAGE";
35+
private static final String OUTPUT = "OUTPUT";
36+
37+
private static final String[] PARAM_PACKAGE = new String[] {"-p", "--package-name"};
38+
private static final String[] PARAM_OUTPUT = new String[] {"-o", "--output"};
39+
40+
private static final Map<String, String> PARAMS_TO_KEYS = buildParamsToKeys();
41+
42+
private static Map<String, String> buildParamsToKeys() {
43+
var map = new HashMap<String, String>(PARAM_OUTPUT.length + PARAM_PACKAGE.length);
44+
for (String k : PARAM_PACKAGE) map.put(k, PACKAGE);
45+
for (String k : PARAM_OUTPUT) map.put(k, OUTPUT);
46+
return map;
47+
}
48+
49+
private final Map<String, String> arguments;
50+
51+
private InputParameters(Map<String, String> arguments) {
52+
this.arguments = arguments;
53+
}
54+
55+
@Override
56+
public Optional<String> getPackageName() {
57+
return Optional.ofNullable(arguments.get(PACKAGE));
58+
}
59+
60+
@Override
61+
public Path getOutputFolder() {
62+
return Path.of(arguments.getOrDefault(OUTPUT, "."));
63+
}
64+
65+
@Override
66+
public Optional<String> getJsonSchemaCode() {
67+
try {
68+
return readInput();
69+
} catch (IOException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
private Optional<String> readInput() throws IOException {
75+
if (System.in.available() == 0) {
76+
return Optional.empty();
77+
}
78+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
79+
var content = reader.lines().collect(Collectors.joining("\n"));
80+
return Optional.of(content);
81+
}
82+
83+
@Override
84+
public Optional<Path> getJsonSchemaFile() {
85+
return Optional.ofNullable(arguments.get(JSON_SCHEMA_FILE)).map(Path::of);
86+
}
87+
88+
public static InputParameters of(String[] args) {
89+
var arguments = new HashMap<String, String>();
90+
String k = null;
91+
for (String argument : args) {
92+
if (argument.charAt(0) == '-') {
93+
if (k != null) {
94+
arguments.put(k, "true");
95+
}
96+
k = argument;
97+
continue;
98+
}
99+
if (k != null) {
100+
saveParameter(arguments, k, argument);
101+
k = null;
102+
} else {
103+
arguments.putIfAbsent(JSON_SCHEMA_FILE, argument);
104+
}
105+
}
106+
return new InputParameters(arguments);
107+
}
108+
109+
private static void saveParameter(
110+
HashMap<String, String> arguments, String name, String argument) {
111+
var key = PARAMS_TO_KEYS.get(name);
112+
if (arguments.containsKey(key)) {
113+
throw new IllegalArgumentException(
114+
"Argument %s detected multiple times: '%s','%s'"
115+
.formatted(key, arguments.get(key), argument));
116+
}
117+
arguments.put(key, argument);
118+
}
119+
}

0 commit comments

Comments
 (0)