We want to package our above application into an executable Jar that we can then deploy to the production server to run. Executable jars, sometimes referred to as “fat jars,” contain all of your compiled classes and all of the jars you need to rely on to run those classes. In other words, package all your dependencies and your code into an executable file that you can run directly from the command line without deploying the JAR to a server or container.

Executable jars and Java

Java does not provide a standard way to load all the required JAR files (sometimes jars themselves contain other jars). When you try to distribute jars that can be run automatically, you may encounter problems that cause the jar to not run and start properly. application.

To solve this problem, many app developers use “Uber” jars. An Uber JAR package mediation includes all the classes and dependent classes that the application needs to run, and then packages these compiled classes into a separate archive. Deploying your references in this way makes it very difficult when you try to see what packages or Jars are introduced into the classes you need from the deployed application. At the same time, there are other potential problems with this approach, such as classes with the same name in different packages. This can lead to compile failures or missing classes.

Spring Boot uses other methods as well as giving you access to the Jars directory.

To create an executable JAR in Spring Boot, we need to add the plug-in spring-boot-Maven-plugin to our pom.xml file. Insert the following after the Dependencies section:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

Update-boots-starter -parent POM with

configured to bind repackage. If you are not using parent POM, you need to declare this configuration yourself. Please refer to the plug-in documentation for more details.

Save the updated pom.xml file and run the MVN package command from the command line. You should see the following output from the command line:

$ mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myproject 0.0.1 - the SNAPSHOT [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (INFO)... . [INFO] -- Maven-jar-plugin :2.4:jar (default-jar) @myproject -- [INFO] Building JAR: / Users/developer/example/spring - the boot - example/target/myproject - 0.0.1 - the SNAPSHOT. Jar [INFO] - [INFO] Spring-boot-maven-plugin :2.5.0 -snapshot :repackage (default) @myproject -- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------Copy the code

If you are building for the first time, you will download the required plug-ins, which may take a little time.

When the build is complete, visit the target directory, where you should be able to find a myproject-0.0.1-snapshot.jar file. This file should be about 10 MB in size. If you want to see what’s in this file, you can do so by running jar TVF:

$jar TVF target/myproject - 0.0.1 - the SNAPSHOT. The jarCopy the code

After running the above command, you should see the small file named myproject-0.0.1- snapshot.jar.original in the target directory. These files are created by the Maven command. Spring Boot repackages the files created above.

You can run your application by running the java-jar command. After typing the above command, your console should have the following output:

$Java - jar target/myproject - 0.0.1 - the SNAPSHOT. Jar. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ |' () _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |)))) 'there comes | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = | ___ / = / _ / _ / _ / : : Spring the Boot: : (v2.5.0 - the SNAPSHOT)... . . . . (log output here) ....... . Started Example in 2.536 seconds (JVM running for 2.864)Copy the code

You can exit the currently running application by clicking Ctrl-C, just as you did with the previous command.

www.ossez.com/t/spring-bo…