-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add second graal doc example (#4341)
extracted from #4340
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
example/javalib/publishing/8-native-image-config/build.mill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
*/ |
34 changes: 34 additions & 0 deletions
34
example/javalib/publishing/8-native-image-config/foo/src/foo/Foo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |