Building native Java CLIs with GraalVM, Picocli, and Gradle

Background

Project Setup with Gradle

$ mkdir mycli && cd mycli$ gradle init \
--type java-application \
--dsl groovy \
--test-framework junit-jupiter \
--project-name mycli \
--package com.mitchseymour.mycli
$ tree ..
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── mitchseymour
│ │ └── mycli
│ │ └── App.java
│ └── resources
└── test
├── java
│ └── com
│ └── mitchseymour
│ └── mycli
│ └── AppTest.java
└── resources
$ ./gradlew run

Hello world.

Adding picocli

$ ./gradlew run

Hello CLI :)
$ ./gradlew run --args="--help"

Usage: mycli [-hV]
Says hello
-h, --help Show this help message and exit.
-V, --version Print version information and exit.
./mycli
./gradlew run --args="--help"

Building a native image

./gradlew nativeImage
$ ./gradlew nativeImage -P com.palantir.graal.cache.dir=/tmp
$ cd ./build/graal# no args example
$ ./mycli
# output
Hello CLI :)
# args example
$ ./mycli --help
# output
Usage: mycli [-hV]
Says hello
-h, --help Show this help message and exit.
-V, --version Print version information and exit.

Summary

Software engineer @ Mailchimp

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store