Skip to content

Commit

Permalink
Add second graal doc example (#4341)
Browse files Browse the repository at this point in the history
extracted from #4340
  • Loading branch information
lihaoyi authored Jan 16, 2025
1 parent 1d12ccb commit db05eaa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
36 changes: 36 additions & 0 deletions example/javalib/publishing/8-native-image-config/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package build
import mill._, javalib._
import mill.define.ModuleRef

object foo extends JavaModule with NativeImageModule {
def ivyDeps = Agg(
ivy"net.sourceforge.argparse4j:argparse4j:0.9.0",
ivy"org.thymeleaf:thymeleaf:3.1.1.RELEASE",
ivy"org.slf4j:slf4j-nop:2.0.7"
)

def zincWorker = ModuleRef(ZincWorkerGraalvm)

def nativeImageOptions = Seq(
"--no-fallback",
"-H:IncludeResourceBundles=net.sourceforge.argparse4j.internal.ArgumentParserImpl"
)
}

object ZincWorkerGraalvm extends ZincWorkerModule {
def jvmId = "graalvm-community:17.0.9"
}

// This example shows how to generate native images for projects using third-party
// libraries, in this case ArgParse4J and Thymeleaf. ArgParse4J does use some dynamic
// resource loading and reflection, and so we need to pass the `-H:IncludeResourceBundles`
// flag to `nativeImageOptions` in order to be compatible.

/** Usage

> ./mill show foo.nativeImage

> ./out/foo/nativeImage.dest/native-executable --text hello-world
<h1>hello-world</h1>

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package foo;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.Namespace;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

public class Foo {
public static String generateHtml(String text) {
Context context = new Context();
context.setVariable("text", text);
return new TemplateEngine().process("<h1 th:text=\"${text}\"></h1>", context);
}

public static void main(String[] args) {
ArgumentParser parser = ArgumentParsers.newFor("template")
.build()
.defaultHelp(true)
.description("Inserts text into a HTML template");

parser.addArgument("-t", "--text").required(true).help("text to insert");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}

System.out.println(generateHtml(ns.getString("text")));
}
}

0 comments on commit db05eaa

Please sign in to comment.