如何构建一个Spring Boot jar文件,systemd可以直接作为服务执行? [英] How do I build a Spring Boot jarfile that systemd can execute directly as a service?
问题描述
如何构建一个Spring引导jar文件, systemd 可以直接作为服务执行?
How do I build a Spring Boot jarfile that systemd can directly execute as a service?
按照 作为systemd服务安装 ,我创建了以下systemd服务直接执行Spring Boot jar文件:
[Unit]
Description=CRS Self-certification Service
Documentation=
Requires=postgresql.service
After=postgresql.service
[Service]
Environment=LOADER_PATH='lib/,config/,/etc/opes/crs/selfcertification'
ExecStart=/opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
Restart=always
RestartSec=10
User=crs
[Install]
WantedBy=multi-user.target
但是,当启动此服务时,systemd会抱怨jarfile不可执行:
However, when starting this service, systemd complains that the jarfile is not executable:
Nov 29 10:57:59 ubuntu systemd[24109]: selfcertification.service: Failed at step EXEC spawning /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar: Exec format error
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Main process exited, code=exited, status=203/EXEC
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Unit entered failed state.
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Failed with result 'exit-code'.
jarfile的权限是 755
(可由所有人执行):
The permissions of the jarfile are 755
(executable by all):
administrator@ubuntu:~$ ls -la /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
-rwxr-xr-x 1 crs selfcertification 35978778 Nov 22 17:16 /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
我必须对以下Gradle构建脚本进行哪些更改才能为systemd服务构建可执行jar文件?
What changes must I make to the following Gradle build script in order to build an executable jarfile for the systemd service?
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'crs-selfcertification'
version = '1.0.0-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClass = "com.opessoftware.crs.selfcertification.Application"
layout = "ZIP"
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-mail")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1208.jre7'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
compile group: 'junit', name: 'junit', version: '4.12'
}
请注意,如果不是尝试运行,此服务会成功运行jarfile直接,systemd而不是从shell脚本使用Java虚拟机(JVM)启动它:
Note that this service runs successfully if instead of trying to run the jarfile directly, systemd instead launches it using the Java Virtual Machine (JVM) from a shell script:
[Unit]
Description=CRS Self-certification Service
Documentation=
Requires=postgresql.service
After=postgresql.service
[Service]
Environment=LOADER_PATH='lib/,config/,/etc/opes/crs/selfcertification'
#ExecStart=/opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
ExecStart=/opt/opes/crs/selfcertification/startCrsSelfCertification
Restart=always
RestartSec=10
User=crs
[Install]
WantedBy=multi-user.target
Shell脚本 / opt / opes / crs / selfcertification / startCrsSelfCertification
使用JVM调用jarfile:
Shell script /opt/opes/crs/selfcertification/startCrsSelfCertification
invokes the jarfile using the JVM:
#!/bin/sh
java -Dloader.path='lib/,config/,/etc/opes/crs/selfcertification' -jar /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
Spring Boot jarfile中可能缺少哪些东西阻止systemd直接执行jar文件?
What might be missing from the Spring Boot jarfile that prevents systemd from executing the jarfile directly?
推荐答案
您应该指示Spring Boot将您的项目重新打包为完全可执行的形式:
You should instruct Spring Boot to repackage your project into the fully executable form:
springBoot {
executable = true
}
此功能仅适用于Spring Boot 1.4.0 +。
and this feature is only available for Spring Boot 1.4.0+.
这篇关于如何构建一个Spring Boot jar文件,systemd可以直接作为服务执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!