Spring Boot executable jar as Linux service

In this blog post I will describe required steps in order to build with Spring boot a fully executable application as a jar file, deploy over ssh, run it as a System V service using a custom user for this.

I have generated a Spring Boot application using Initializr and configured the build.gradle script to build the project as an executable jar. This configuration will generate so called “fatJar” (a jar archive with all dependencies) and in manifest file will add a line specifying the Main Class:

We don’t want to execute our jar file as jar -jar myApplication.jar, in order to achive this we will add to the build script (see details on Spring Boot docs read more):

In order to deploy over SSH we will use org.hidetake.ssh plugin, will add a remote host with key based authentication (to generate ssh keys use ssh-keygen, I have generated them in ./deploy folder).

Add a task named deploy that will depend on task build, task will be responsible for remote stopping the app, copying new version and start remote service with service foundation start – service will be started as foundation user (build.gradle):

Now if we will try to execute gradle deploy it should build our application, save the ‘fatJar’ into ./build/libs folder and then will try to deploy. Most probably this will fail as we have not created yet the remote user.

In order to create foundation user we should login to our machine and write:

Our gradle deploy command may still fail, a foundation.service file should be created and foundation user should have the correct permissions in order to start this service.

Create /etc/systemd/system/foundation.service with content:

Now you can try to run service foundation start, if it doesn’t ask you for password, then you are ready to go. If it asked you for password as it was in my case, then follow next steps.

In case it is asking for password, we will add foundation user to the sudoers list and will allow execution for several commands without sudo password, lets add a new sudoers file with sudo visudo -f /etc/sudoers.d/foundation:

Now execute gradle deploy on your dev machine, and your application should be deployed.

Some distributions are using PolicyKit for managing unprivileged users permissions. In order to allow to your user foundation to use service foundation start\stop\restart\status command we will create file /etc/polkit-1/localauthority.conf.d/51-foundation.conf with content (don’t forget to replace the service name and username):

At the end don’t forget to restart polkitd with service polkit restart

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest sit folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.