How to Run a Spring Boot Executable Jar in a Production Environment

Spring Boot runnable Jar file for production?

You can use the .jar for production too. Jars are easy to create. Spring boot jar can simply be managed automatically by the production server, i.e. restart the jar and etc.

It also has embedded Tomcat and the Tomcat configuration can be modified and optimized using the .properties file or inside Java if you need that.

Generally, there are almost no reason to use .war over .jar, but it is up to you what you prefer.

Deploying Webflux application Jar file

There are two ways you could deploy a spring-boot application.

  • Running as a standalone JAR file
  • Deploy as a WAR file into a tomcat (For Spring WebFlux application this is not supported).

Deploy as a standalone JAR

To generate an executable JAR we could either use spring-boot maven plugin or spring-boot gradle plugin depending on your use case.

Spring Boot Maven Plugin

  • Add following to the pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
  • Run following command to generate the executable.
mvn clean package spring-boot:repackage
  • Then run the generated JAR file with following command.
java -jar <path-to-generated-jar>/<app-name>.jar

Spring Boot Gradle Plugin

  • Run following command to generate the executable.
./gradlew bootJar
  • Then run the generated JAR file with following command.
java -jar <path-to-generated-jar>/<app-name>.jar

Spring boot running a fully executable JAR and specify -D properties

There are two ways to configure properties like that:

1:

By specifying them in a separate configuration file. Spring Boot will look for a file named like JARfilename.conf which should be stored in the same folder like the JAR file. There you can add the environment variable JAVA_OPTS:

JAVA_OPTS="-Dpropertykey=propvalue"

2:

Or you can just specify the value for the environment variable in the shell before you execute the application:

JAVA_OPTS="-Dpropertykey=propvalue" ./myapp.jar

Have a look at the documentation for the complete list of available variables: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#deployment-service

Regarding your second question: To execute a JAR, you don't need a JDK, a JRE is sufficient (but you need at least that, if you don't have any java installed on the server, the application won't run).



Related Topics



Leave a reply



Submit