Introduction
After completing application development or in the middle of development in some cases we need executable jar file. Jar file is a combined version of class files and dependencies of application along with resources. To create jar file we are using maven instead of java jar file system , because java jar system can not allow to load nested jar files.
Java does not provide a standard way to load nested jar files (jar files that are themselves contained within a jar). This can be problematic if you are looking to distribute a self-contained application.
To solve this problem, many developers use “uber” jars. An uber jar packages all the classes from all the application’s dependencies into a single archive. The problem with this approach is that it becomes hard to see which libraries are in your application. It can also be problematic if the same filename is used (but with different content) in multiple jars.Executable jars and Java
To solve this problem, many developers use “uber” jars. An uber jar packages all the classes from all the application’s dependencies into a single archive. The problem with this approach is that it becomes hard to see which libraries are in your application. It can also be problematic if the same filename is used (but with different content) in multiple jars.
Update pom file
To generate executable jar file we must add the following into pon.xml file after dependencies tag. Check pom.xml file whether the build tag for maven plugin added or not, if already added then no need to add it again.<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Run maven package command
After adding maven plugin, we have to run the following maven package command to generate .jar file.Go to the location ( root folder ) of application then run the following command
$ mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] .... .. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject --- [INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.2.6.RELEASE:repackage (default) @ myproject --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------If there is running smooth then you can get BUILD SUCCESS message at the end as show in the above snippet.
After getting BUILD SUCCESS message one new folder will be created named as target, jar file will be located in this location.
How to run application by using jar file
Run the following command from Application root directory by providing created jar file full namejava -jar target/myproject-0.0.1-SNAPSHOT.jar
Understanding jar file name structure
Maven generated jar file has a name with two components taking from pom.xml file.artifactID-version.jar is the file format of the jar file created. ArtifactID and version numbers are taking from pom.xml file as shown in the following snipper
<artifactId>jai43</artifactId> <version>0.0.1-SNAPSHOT</version>
With the above two commands from pom.xml file jar file name would be jai43-0.0.1-SNAPSHOT.jar
Post a Comment
Post a Comment