怎么基于Java编写一个CLI工具?( 三 )


生成exe如果你看过之前的文章,关于GraalVM的使用,按照文档下载并配置好运行环境后,可以通过下面的命令对上一步的jar文件进一步处理
native-image -jar [jar] -o [name]
native-image -jar ./target/calendar-jar-with-dependencies.jar -o calendar通过上面的命令会生成一个calendar.exe文件,这样将其加入到环境变量后 , 则可以在windows平台终端上使用了
对于不喜欢直接使用命令的 , 当然这里也可以使用插件exec-maven-plugin,在maven生命周期package阶段,自动执行上面的命令,这样整个过程只需要执行mvn clean package即可
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>3.1.0</version><executions><execution><id>native-image-App</id><phase>package</phase><goals><goal>exec</goal></goals><configuration><environmentVariables></environmentVariables><!-- native-image -jar ./target/tool-jar-with-dependencies.jar -o tool --><executable>native-image</executable><arguments><argument>-jar</argument><argument>${project.basedir}/target/${project.build.finalName}-jar-with-dependencies.jar</argument><argument>-o</argument><argument>${project.build.finalName}</argument></arguments></configuration></execution></executions></plugin>测试exe在终端执行下面的命令接口看到预期的结果:
calendar.exe -O yyyy-MM-dd总结总的来说,Apache Commons CLI是一个非常强大的工具,可以帮助你轻松地处理命令行参数 。无论你的应用程序需要处理多少个参数,或者这些参数的类型是什么, Commons CLI都可以提供帮助 。

【怎么基于Java编写一个CLI工具?】


推荐阅读