Как создать Spring загрузочный jarfile, который systemd может выполнять непосредственно как сервис? - программирование

Как создать Spring загрузочный jarfile, который systemd может выполнять непосредственно как сервис?

Как мне создать файл Spring Boot jarfile, который systemd может напрямую выполняться как служба?

Следуя примеру Установка в качестве службы systemd, я создал следующую службу systemd, которая напрямую выполняет загрузку Spring 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'.

Разрешения jarfile: 755 (исполняемые всеми):

[email protected]:~$ 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 build script, чтобы создать исполняемый файл jar для службы systemd?

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 вместо запускает его с помощью виртуальной машины Java (JVM) из оболочки 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 script /opt/opes/crs/selfcertification/startCrsSelfCertification вызывает jarfile с помощью 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 напрямую выполнять jarfile?

4b9b3361

Ответ 1

Вы должны указать 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 банка