簡體   English   中英

如何構建一個可以直接作為服務執行的Spring Boot jar文件?

[英]How do I build a Spring Boot jarfile that systemd can execute directly as a service?

如何構建一個Spring引導jar文件systemd可以直接執行作為一種服務?

按照安裝中作為systemd服務的示例,我創建了以下systemd服務,該服務直接執行Spring Boot jarfile

[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不可執行:

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'.

jar文件的權限是755 (可由所有人執行):

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文件?

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而是嘗試直接運行jar文件,而是使用Java腳本(JVM)從shell腳本啟動它 ,則此服務會成功運行:

[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:

#!/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直接執行jarfile?

您應該指示Spring Boot將項目重新打包為完全可執行的形式:

springBoot {
    executable = true
}

此功能僅適用於Spring Boot 1.4.0+。

有關更多信息,請參閱http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration

從Spring Boot 2.X +開始 ,使用:

  bootJar {
      launchScript()
  }

來源: 可執行的Spring Boot 2 jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM