简体   繁体   English

如何构建一个可以直接作为服务执行的Spring Boot jar文件?

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

How do I build a Spring Boot jarfile that systemd can directly execute as a service? 如何构建一个Spring引导jar文件systemd可以直接执行作为一种服务?

Following the example in Installation as a systemd service , I created the following systemd service that directly executes a Spring Boot jarfile : 按照安装中作为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

However, when starting this service, systemd complains that the jarfile is not executable: 但是,在启动此服务时,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'.

The permissions of the jarfile are 755 (executable by all): 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

What changes must I make to the following Gradle build script in order to build an executable jarfile for the systemd service? 我必须对以下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'

}

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 : 请注意,如果不是尝试直接运行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 script /opt/opes/crs/selfcertification/startCrsSelfCertification invokes the jarfile using the JVM: 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

What might be missing from the Spring Boot jarfile that prevents systemd from executing the jarfile directly? Spring Boot jarfile中可能缺少哪些东西阻止systemd直接执行jarfile?

You should instruct Spring Boot to repackage your project into the fully executable form: 您应该指示Spring Boot将项目重新打包为完全可执行的形式:

springBoot {
    executable = true
}

and this feature is only available for Spring Boot 1.4.0+. 此功能仅适用于Spring Boot 1.4.0+。

Refer to http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration for more information. 有关更多信息,请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration

From Spring Boot 2.X+ , use: 从Spring Boot 2.X +开始 ,使用:

  bootJar {
      launchScript()
  }

Source: Executable Spring Boot 2 jar 来源: 可执行的Spring Boot 2 jar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置可执行的Spring Boot jarfile的JVM属性? - How do I set the JVM properties of an executable Spring Boot jarfile? 如何使用 Dockerfile 构建 Spring Boot 应用程序 (Maven)? - How do I build a Spring Boot app (Maven) with a Dockerfile? 如何在 Spring Boot 2.6.4 版的另一个 Spring Boot 服务中使用 Spring Boot 2.1.4.RELEASE 版的 AOP 服务 - How I can consume an AOP service of Spring Boot version 2.1.4.RELEASE in another spring boot service of Spring Boot version 2.6.4 当我用Jenkins用Maven构建Spring Boot应用程序并执行JAR文件时,找不到类定义? - Can not find class define, when I build a Spring Boot application with Maven by Jenkins and execute JAR file? 如何使用 Spring Boot 构建两个重新打包的 jar - How can I build two repackaged jars with Spring Boot 如何在春季启动时从wsdl生成肥皂服务? - How can i generate soap service from wsdl in spring boot? 如何在linux中直接执行jar? - How do I directly execute a jar in linux? pid 文件未创建,将 Spring Boot 应用程序作为 systemd 服务运行 - pid file not created running spring boot application as systemd service 带有 Logback 的 Spring Boot 应用程序不会作为 Systemd 服务登录到指定位置 - Spring Boot App with Logback does not log to specified location as Systemd Service 如何在没有任何端口的情况下直接使用 url 访问我的 spring 启动 web 应用程序? - How can I directly use url to access my spring boot web application without any port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM