In this blog post, we will learn how to create a simple quick start project which can be build into a jar file, upon it’s’ execution will call a java class with main method.
Create maven quick start project
mvn archetype:generate -DgroupId=com.test -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
JAR Plugin
The Maven’s jar plugin will create jar file and we need to define the main class that will get executed when we run the jar file.
<plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.test.App</mainClass> </manifest> </archive> </configuration> </plugin>
Create a main class with name APP and the below sample will create a random hash code string and print it. So when we execute the jar file, this class will be called, and that is what we have defined under the plugin section in the pom above, under the tag <mainClass>com.test.App</mainClass>
public class App { public static void main(String args[]){ String str= generateSHA512hash("lav"); System.out.println("Random String is: "+str); } }
Maven Package
Maven package will create the executable jar file
The jar file should be ready under the target
directory
mvn package
Execute the jar file using the command below,
java -jar test-1.0-SNAPSHOT.jar
Result
ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff