可执行的 Spring Boot 2 jar

6

我正在尝试安装我的Spring Boot应用程序。 作为第一步,我尝试按照这里描述的方式创建可执行jar文件: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

但是,如果我在我的Gradle脚本中添加以下行(我使用的是Gradle 4.4):

springBoot {
    executable = true
}

然后构建失败并出现错误:

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\spring\app\build.gradle' line: 15

* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method springBoot() for arguments [build_3mwux4us8m2jn9yfcogqkbm4y$_run_closure1@506b241f] on root project 'app' of type org.gradle.api.Project.

我的构建脚本如下:

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

springBoot {
    executable = true
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


group = 'com.test'
version = '0.0.3'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url 'https://repo.spring.io/libs-release' }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.data:spring-data-envers:2.0.2.RELEASE')
    compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
}
2个回答

15
您链接的是Spring Boot 1.x的参考文档,但您正在使用Spring Boot 2.x,其中Gradle插件已经得到了相当大的更新。现在它有自己单独的参考文档
在该参考文档中,它描述了如何创建一个包括预置启动脚本的完全可执行的jar文件:

Spring Boot provides support for fully executable archives. An archive is made fully executable by prepending a shell script that knows how to launch the application. On Unix-like platforms, this launch script allows the archive to be run directly like any other executable or to be installed as a service.

To use this feature, the inclusion of the launch script must be enabled:

bootJar {
    launchScript()
}

Spring Boot Gradle的链接非常有帮助。 - Eugene Gr. Philippov

0

尝试手动执行以下Gradle任务:

  • gradlew jar
  • gradlew bootRepackage

之后,您应该拥有打包的fat Jar。(这就是我解决问题的方法)


3
@BaurMarius 这个解决方案仅适用于Spring Boot 1.x,因为在Spring Boot 2.0的gradle插件中,bootRepackage任务已被bootJar替换。Spring Boot 2.0迁移指南解释了这个变化。 - Phil Haigh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接