【问题标题】:Gradle build with fatJar plugin and SpringBoot application gives 'Application startup failed'使用胖 Jar 插件和 Spring Boot 应用程序构建的 Gradle 给出“应用程序启动失败”
【发布时间】:2017-09-08 00:38:04
【问题描述】:

使用Intellij 启动我的应用程序时一切正常。但是当我制作fatJar(使用gradle插件:eu.appsatori.fatjar)并执行时:

java -jar myapp.jar

我得到了这样的东西:

11:41:01.224 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [my.testing.MyAppMain]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
        at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:482)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184)
...

META-INF/spring.factories 中似乎没有找到自动配置类。

如何添加这个文件?它的内容应该是什么?

我有以下构建脚本:

apply plugin: "java";
apply plugin: "idea";
apply plugin: 'application'
apply plugin: 'eu.appsatori.fatjar'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}

buildscript {
    ext {
        springBootVersion = '1.4.3.RELEASE'
    }

    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath "eu.appsatori:gradle-fatjar-plugin:0.3"
    }
}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }

        resources {
            srcDir 'src/main/resources'
        }
    }

    test {
        java {
            srcDir 'src/test/java'
        }
    }
}

fatJar {
    manifest {
        attributes("Main-Class": 'my.testing.MyAppMain')
    }

    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.RSA'
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-jdbc'
    runtime 'mysql:mysql-connector-java'

    testCompile 'org.springframework.boot:spring-boot-starter-test'

}

我的示例代码是:

package my.testing;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class MyAppMain {
    private ConfigurableApplicationContext springContext;

    @Autowired
    private SimpleDao dao;

    public static void main(String[] args) throws Exception {
        MyAppMain test = new MyAppMain();
        try {
            test.init();
            test.doWhatYouGotToDo();
        } finally {
            test.stop();
        }
    }

    private void doWhatYouGotToDo() {
        System.out.println("Auto-wired dao: " + dao.hashCode());
        System.out.println("Auto-wired jdbcTemplate: " + dao.jdbcTemplate.hashCode());
    }

    private void init() throws Exception {
        springContext = SpringApplication.run(MyAppMain.class);
        springContext.getAutowireCapableBeanFactory().autowireBean(this);
    }

    private void stop() throws Exception {
        springContext.close();
    }
}

@Component
class SimpleDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
}

application.properties文件:

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/some_db?useSSL=false&serverTimezone=UTC
spring.datasource.username = some_user
spring.datasource.password = some_pass

注意:此问题基于SpringBoot - making jar files - No auto configuration classes found in META-INF/spring.factories 所有答案都指使用Maven 构建。请在此处仅填写与Gradle 相关的答案。

【问题讨论】:

  • 不要使用fatjar 插件... Spring Boot 已经创建了一个胖 jar... 您现在有 2 种竞争机制...。
  • 那么我怎样才能在没有那个插件的情况下构建一个胖罐子呢? IE。如何在 IntelliJ 之外成功运行上述应用程序?
  • 好的。 Gradle bootRepackage 任务做到了。谢谢!你可以形成一个答案,所以我可以接受它......

标签: gradle spring-boot jar


【解决方案1】:

虽然我主要使用 Maven for Spring 和 Gradle for Android,但这里是 Spring 项目的 gradle 方式:

gradle clean build
gradle bootRepackage

结果:

这是我的 build.gradle 文件:

【讨论】:

  • 请分享整个 build.gradle。我被困住了 2 天
  • 如果您想查看一个空的 gradle 文件,请从这里选择一个 Gradle 项目:start.spring.io
猜你喜欢
  • 2017-05-12
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2019-03-15
  • 2021-01-14
  • 1970-01-01
相关资源
最近更新 更多