3

Everything was working fine when starting my app using Intellij. But when I made fatJar (with gradle plugin: eu.appsatori.fatjar) and execute:

java -jar myapp.jar

I'm getting something like this:

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)
...

It looks like it didn't found auto configuration classes in META-INF/spring.factories.

How to add this file? And what should be the content of it?

I've got following build script:

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'

}

And my example code is:

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 file:

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

NOTE: This question is based on SpringBoot - making jar files - No auto configuration classes found in META-INF/spring.factories where are all answers are referring to building with Maven. Please put only answers related to Gradle here.

Community
  • 1
  • 1
alwi
  • 431
  • 6
  • 21
  • 1
    Don't use the `fatjar` plugin... Spring Boot already creates a fat jar... You now have 2 competing mechanisms.... – M. Deinum Apr 12 '17 at 10:14
  • So how can I build into a fat jar without that plugin? I.e. how to successfully run above application outside of IntelliJ? – alwi Apr 12 '17 at 10:17
  • 4
    Ok. Gradle `bootRepackage` task does it. Thanks! You could form an answer so I can accept it... – alwi Apr 12 '17 at 10:33

1 Answers1

5

Although I mostly use Maven for Spring and Gradle for Android, but here is the gradle way for a Spring project:

gradle clean build
gradle bootRepackage

Result:

enter image description here

Here is my build.gradle file:

enter image description here

Gene
  • 10,819
  • 1
  • 66
  • 58