Spring Boot + Gradle:如何构建可执行的jar文件

46

我正在尝试在Spring Boot + Gradle项目中构建可执行的jar文件,但现在什么也不起作用。这是最简单可能的结构。可能在Gradle配置中缺少一些东西。

Gradle:

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': 'com.example.demo.DemoApplication'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
}
主配置文件:
@RestController
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping(value = "/")
    public String index() {
        return "index";
    }
}
当我像java -jar 1.jar这样运行jar文件时,我遇到了这个异常:

enter image description here

[main] ERROR org.springframework.boot.SpringApplication - Applicati
on startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to proces
s import candidates for configuration class [com.example.demo.DemoApplication];
nested exception is java.lang.IllegalArgumentException: No auto configuration cl
asses found in META-INF/spring.factories. If you are using a custom packaging, m
ake sure that file is correct.
        at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:556)
        at org.springframework.context.annotation.ConfigurationClassParser.parse
(ConfigurationClassParser.java:185)
        at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
        at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
        at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.ja
va:272)
        at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
        at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:687)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:525)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationConte
xt.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.
java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringAppli
cation.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
:303)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1107)
        at com.example.demo.DemoApplication.main(DemoApplication.java:13)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes fou
nd in META-INF/spring.factories. If you are using a custom packaging, make sure
that file is correct.
        at org.springframework.util.Assert.notEmpty(Assert.java:277)
        at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.getCandidateConfigurations(AutoConfigurationImportSelector.java:153)
        at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.selectImports(AutoConfigurationImportSelector.java:95)
        at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:547)
        ... 14 common frames omitted

可能出了什么问题?

7个回答

42

在Boot 2.x中,bootJar和bootWar任务负责打包应用程序。

bootJar任务负责创建可执行的jar文件。一旦应用了Java插件,它会自动创建。

如果可执行的jar/war文件没有生成,请手动运行下面的Gradle任务。

$./gradlew bootJar

同样地,bootWar 会生成一个可执行的 war 文件,并在应用 war 插件后创建。

我们可以使用以下方式执行 bootWar 任务:

$./gradlew bootWar

请注意,对于Spring Boot 2.x版本,我们需要使用Gradle 4.0或更高版本。


太好了!谢谢。我正在尝试使用5.6.4(Oracle JDK 1.8)和Lombok;添加spring boot {}会出现问题...但是指定bootJar可以解决。 - user3709525
你能简要介绍一下你使用的启动jar文件的命令吗? - Rajesh
link - undefined

25

我使用你提供的所有源代码创建了一个项目。在终端上运行"gradle build",跳转到/build/libs目录下并且运行"java -jar artifactname"非常好用。

你尝试过清理和重新编译吗?你使用的Gradle版本是哪个?


我不知道我必须使用“gradle build”,而是尝试使用“gradle jar”。现在它可以工作了。 - sva605
如何在Gradle构建命令中提供Spring配置文件。 - Somnath Singh

11

1
executable is outdated. Could not set unknown property 'executable' for extension 'springBoot' of type org.springframework.boot.gradle.dsl.SpringBootExtension. - nabster

6
我最近使用Gradle构建了一个Spring Boot应用程序(版本为2.1.4.Release)进行尝试。
我在Windows CMD中的目录下运行了以下命令。
gradlew clean build

在系统安装所需的JDK8后,我能够看到下面的JAR生成:

<project-directory>/build/libs/<project-name-version.jar>

希望这个能帮到您,尽管它是一个较老的问题。
参考: enter image description here

3
如果您想将您的 .jar 文件作为可执行文件使用,例如在 systemd 服务中使用。您需要编辑 bootJar 任务并启用 launchScript

build.gradle

bootJar {
    launchScript()
}

或者使用 Gradle Kotlin DSL 的 build.gradle.kts 文件。
tasks {
    bootJar {
        launchScript()
    }
}

现在,您应该能够将项目的.jar文件作为可执行文件运行。


2

我的两分意见。

在使用spring-boot时,如果你想要自定义MANIFEST.MF文件,你需要设置bootJar任务,而不能在默认的jar任务中执行。

bootJar {
    manifest {
    attributes 'Start-Class': 'com.baeldung.DemoApplication'
    }
}

0
对我来说,在IntelliJ中点击gradle任务下的"jar",会在./build/libs/目录下创建一个jar文件。

Click jar not bootJar


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