This project represents two basic components of developing a standalone java application with a CLI Interface.
-
It demonstrates the basic design using a singleton to contain and execute a command line argument processor as embodied in
edu.isu.cs.grifisaa.production.CLI.java -
It sets up the ability to construct a shadow jar (a jar file with all the dependencies included). Additionally, as this is a gradle project using the
applicationplugin it provides for the construction of scripts for execution.
The following subsections will describe the general operation of gradle for this project.
The Apache
Commons CLI dependency is added in the dependencies block of
the build.gradle file using the following:
implementation 'commons-cli:commons-cli:1.4'
As typical with Open Source projects it is wise to add a license header to the top of each source file in the project. Towards this end we have used the "Hierynomus" gradle license plugin as part of this project. This adds two additional capabilities
-
To check if any source files are missing their license header, execute the following command:
$ gradle licenseCheck -
To add/update the license header of each source file (based on LICENSE file at the project root) execute the following command:
$ gradle licenseFormat
This project uses the gradle "application" plugin which provides
several benefits, including the ability to generate runnable jar
files. Towards this end, we need to add the following lines of code
to the build.gradle file:
-
In the
pluginsblock we add/verify the following line exists:id 'application' -
Additionally we add the following block:
application { // Define the main class for the application. mainClassName = 'edu.isu.cs.grifisaa.production.App' }Where in the case of this project the main class is
Appin the packageedu.isu.cs.grifisaa.production
As most java projects require a significant number of dependencies on other projects, it becomes difficult to ensure that users will have these dependencies installed on their machine. In order to address this issue we have the capability to include dependencies into our packaged JAR files.
To accomplish this there are several approaches. I suggest using
the Gradle Shadow plugin (see their user guide)
which can be included in the plugins section of a gradle build
file as follows:
id 'com.github.johnrengelman.shadow' version '5.2.0'
This adds several tasks to the process. It should be noted that when you execute the standard build using the following command:
$ gradle build
This will execute the following additional tasks
- shadowJar - constructs and packages the classes and dependencies
in a JAR file in the folder
build/libs, with "-all" appended before the ".jar" extension. - shadowDistZip - constructs a zip distribution of the project in
build/distributions - shadowDistTar - constructs a tar distribution of the project in
build/distributions
It will also create a folder build/scriptsShadow which contains
the linux and windows scripts for running project from the shadowed
JAR.
- For linux visit: java-launcher an example of this can be found in the folder
launcher - For Windows
- Windows: Use IZPack
- Linux: Use the Maven/Gradle Installer Generator