From db05eaa4e5f22f9f4e47c85648d3acb41599dad5 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 16 Jan 2025 20:57:11 +0800 Subject: [PATCH] Add second graal doc example (#4341) extracted from https://github.com/com-lihaoyi/mill/pull/4340 --- .../8-native-image-config/build.mill | 36 +++++++++++++++++++ .../foo/src/foo/Foo.java | 34 ++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 example/javalib/publishing/8-native-image-config/build.mill create mode 100644 example/javalib/publishing/8-native-image-config/foo/src/foo/Foo.java diff --git a/example/javalib/publishing/8-native-image-config/build.mill b/example/javalib/publishing/8-native-image-config/build.mill new file mode 100644 index 00000000000..b4ee04314fe --- /dev/null +++ b/example/javalib/publishing/8-native-image-config/build.mill @@ -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 +

hello-world

+ +*/ diff --git a/example/javalib/publishing/8-native-image-config/foo/src/foo/Foo.java b/example/javalib/publishing/8-native-image-config/foo/src/foo/Foo.java new file mode 100644 index 00000000000..26a6c1174e4 --- /dev/null +++ b/example/javalib/publishing/8-native-image-config/foo/src/foo/Foo.java @@ -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("

", 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"))); + } +}