Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a spring boot executable jar in a Production environment?

Spring boot's preferred deployment method is via a executable jar file which contains tomcat inside.

It is started with a simple java -jar myapp.jar.

Now, I want to deploy that jar to my linux server on EC2, am I missing something or do I really need to create a init script to properly start the application as a daemon?

If I simply call java -jar the application dies when I log out.

I could start it in screen or nohup but that is not very elegant and a restart in my server would force me to log in and start the process manually.

So, is there something already for the task in spring boot?

like image 664
Cleber Goncalves Avatar asked Apr 05 '14 19:04

Cleber Goncalves


People also ask

How do you run a spring boot executable jar?

The Spring Boot Maven Pluginimplement a custom ClassLoader to locate and load all the external jar libraries now nested inside the package. automatically find the main() method and configure it in the manifest, so we don't have to specify the main class in our java -jar command.

Can SpringBoot be used in production?

Spring Boot has revolutionized the way production-ready applications are developed, allowing developers to focus more on the application logic rather than spending time on boilerplate code to handle the necessary configurations and dependencies to run the application.


1 Answers

Please note that since Spring Boot 1.3.0.M1, you are able to build fully executable jars using Maven and Gradle.

For Maven, just include the following in your pom.xml:

<plugin>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-maven-plugin</artifactId>     <configuration>         <executable>true</executable>     </configuration> </plugin> 

For Gradle add the following snippet to your build.gradle:

springBoot {     executable = true } 

The fully executable jar contains an extra script at the front of the file, which allows you to just symlink your Spring Boot jar to init.d or use a systemd script.

init.d example:

$ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp 

This allows you to start, stop and restart your application like:

$/etc/init.d/yourapp start|stop|restart 

Or use a systemd script:

[Unit] Description=yourapp After=syslog.target  [Service] ExecStart=/var/yourapp/yourapp.jar User=yourapp WorkingDirectory=/var/yourapp SuccessExitStatus=143  [Install] WantedBy=multi-user.target 

More information at the following links:

  • Installation as an init.d service
  • Installation as a systemd service
like image 74
corleone Avatar answered Sep 22 '22 15:09

corleone