My favourite tool is pico-cli, for the ease of configuring my runner classes. On the minus side, all those annotations introduce dependency on the framework… but meh, it is good enough for a fast test with cli.
Another important factor to package applications is the use of the maven-assembly-plugin , that will effectively produce the so called uber jar. The structure ofan assembly file is found here
In this example I am going to make a cli rest “client”, using spring-security-kerberos, because it is often useful to authenticate with keytabs, in enterprise environments
Annotating a class with picocli
The executor class is a normal main class for java, with picocli annotations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package net.endeios.kerberos;
import org.springframework.security.kerberos.client.KerberosRestTemplate;
import picocli.CommandLine;
@CommandLine.Command
public class App implements Runnable{
@CommandLine.Option(names = "--keytabPath", description = "the path to the Keytab file", required = true)
String keytabPath;
@CommandLine.Option(names = "--principal", description = "the principal to use", required = true)
String principal;
@CommandLine.Option(names = "--url", description = "the ural to call", required = true)
String url;
public static void main(String[] args) {
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
System.setProperty("javax.security.debug","gssloginconfig,configfile,configparser,logincontext");
System.setProperty("sun.security.krb5.debug","true");
new CommandLine(new App()).execute(args);
System.out.println("Consider using \n-Djava.security.auth.login.config=<path to jaas.conf>\n-Djava.security.krb5.conf=<path to krb5.conf>");
}
@Override
public void run() {
KerberosRestTemplate restTemplate = new KerberosRestTemplate(keytabPath, principal);
String response = restTemplate.getForObject(url,String.class);
System.out.println(response);
}
}
It executes a simple rest call, returning string, with the provided parameters
Explanation of the Maven Assembly Plugin for creating an Uber JAR
To create an Uber JAR using Maven, you can utilize the Maven Assembly Plugin. The relevant configuration for creating an Uber JAR is shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.6.0</version>
<executions>
<execution>
<id>id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>net.endeios.kerberos.App</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven Assembly Plugin allows you to package your application and its dependencies into a single, executable JAR file, commonly referred to as an Uber JAR or Fat JAR. It collects all the required dependencies and merges them into a single JAR file, making it easier to distribute and run your application.
In the provided configuration, the plugin is specified using its artifactId as maven-assembly-plugin
. The groupId
indicates the Maven group that provides the plugin, which is org.apache.maven.plugins
. The version
specifies the version of the plugin being used, in this case, 3.6.0
.
The configuration is nested within the <executions>
element, which defines the execution of the plugin. Inside <executions>
, there is a single execution specified with an <id>
of id
. The <phase>
element sets the phase at which the plugin will be executed, in this case, during the package
phase.
The <goals>
element specifies the goal to be executed, which is single
in this case. The single
goal is responsible for creating the assembly and packaging the application.
The <configuration>
element provides additional configuration for the plugin. Inside <configuration>
, the <archive>
element is used to configure the archive settings for the Uber JAR. The <manifest>
element within <archive>
allows you to specify the main class of the application that will be executed when the JAR is run.
Lastly, the <descriptors>
element is used to specify the assembly descriptor file. In this example, the assembly.xml
descriptor file is referenced. The assembly descriptor file contains the instructions for packaging the dependencies and resources into the Uber JAR.
By using this Maven Assembly Plugin configuration, Maven will create an Uber JAR that includes all the necessary dependencies and resources, making it a self-contained executable JAR file for your application.