【问题标题】:Spring Boot + Gradle: how to build executable jarSpring Boot + Gradle:如何构建可执行jar
【发布时间】:2018-04-06 23:00:28
【问题描述】:

我正在尝试在 Spring Boot + Gradle 项目中构建一个可执行 jar,但目前没有任何效果。这是最简单的结构。 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 文件时,我得到了这个异常:

[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

可能出了什么问题?

【问题讨论】:

    标签: java spring-boot gradle


    【解决方案1】:

    在 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 是有效的。
    • 你能简单介绍一下你用来启动jar文件的命令吗
    【解决方案2】:

    我使用您提供的所有资源创建了一个项目。从终端运行“gradle build”,切换到/build/libs,然后运行“java -jar artifactname”就可以了。

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

    【讨论】:

    • 我不知道我必须使用“gradle build”,而是尝试使用“gradle jar”。现在可以了
    • 如何使用 gradle build 命令提供 spring 配置文件
    【解决方案3】:

    在spring boot中可以直接创建可执行jar文件

    springBoot { 
        executable = true 
    }
    

    请尝试

    jar{
        baseName = 'myapp' 
        version = 'version'
    }
    

    它将创建名为 myapp-version.jar 的 jar 从命令行执行./myapp-version.jar。它将执行

    有关更多信息,请参阅以下链接。 https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

    【讨论】:

      【解决方案4】:

      我最近刚刚尝试了一个带有 2.1.4.Release 和 Gradle 构建的 Spring Boot 应用程序。

      我从 Windows CMD 的目录中运行了以下命令。

      gradlew clean build
      

      (系统需要安装JDK8),我可以看到生成的JAR,

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

      希望这对老问题有所帮助。

      参考:

      【讨论】:

        【解决方案5】:

        我的两分钱。

        使用spring-boot时如果要自定义MANIFEST.MF文件,需要设置bootJar任务,默认的jar任务是不行的。

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

        【讨论】:

          【解决方案6】:

          如果您尝试使您的 .jar 文件可执行,例如在 systemd 服务中使用。您必须编辑bootJar 任务并启用launchScript

          build.gradle

          bootJar {
              launchScript()
          }
          

          或使用 Gradle Kotlin DSL build.gradle.kts

          tasks {
              bootJar {
                  launchScript()
              }
          }
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-01-06
            • 2019-01-09
            • 1970-01-01
            • 2017-08-10
            • 2014-12-18
            • 1970-01-01
            • 2014-04-18
            • 2020-07-25
            相关资源
            最近更新 更多