使用fatJar插件和SpringBoot应用程序进行Gradle构建会导致“应用程序启动失败”

阿尔维

使用启动我的应用程序时,一切工作正常Intellij但是当我fatJar使用gradle plugin:创建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制作的jar文件-在META-INF / spring.factories中找不到所有自动配置类,那里的所有答案都指的是with的构建Maven请仅在Gradle此处输入与之相关的答案

基因
gradle clean build
gradle bootRepackage

结果:

在此处输入图片说明

这是我的build.gradle文件:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Gradle构建JavaFX应用程序

重新启动应用程序会导致NSInvalidArgumentException

Gradle插件2.1.0:应用程序在启动时崩溃

从finder启动时Python构建应用程序失败

Kotlin应用程序与Gradle应用程序插件

添加依赖项时,SpringBoot应用程序启动失败

任务应用程序的Gradle构建失败:processReleaseResources

Flutter应用程序Gradle构建异常失败

gradle 构建成功后更新 Google 服务插件和无法安装应用程序

使用firebase-auth时在ios上构建应用程序会导致抖动问题

使用Simple Injector构建基于插件的应用程序

应用程序输出失败,成功部署和构建

Gradle应用程序插件:如何使用-javaagent选项运行jvm应用程序?

使用Play Framework和Guice进行依赖注入会在应用程序启动时导致NullPointerException

Proguard 构建应用程序失败:transformClassesAndResourcesWithProguardForDebugAndroidTest

Wikipedia Android应用程序构建失败

对休眠的应用程序使用@Transactional注释会导致错误

使用 JSON 会导致我的应用程序崩溃

使用 .tpk 文件会导致应用程序崩溃

在springboot应用程序启动线程

使用“gradle-3.5”构建Web应用程序但在JPA中总是失败

应用程序启动失败-Spring Boot

Heroku启动失败的应用程序错误

使用Maven构建Spark应用程序失败

尝试使用口味时Flutter应用程序构建失败

使用Azure DevOps构建电子应用程序失败

如何使用 gradle kotlin dsl 为简单的控制台应用程序生成“fatjar”

使用ng build --prod进行生产构建后Angular5应用程序失败

如何调试使用Gradle构建的Play 2应用程序