SpringBoot - making jar files - No auto configuration classes found in META-INF/spring.factories

106,589

Solution 1

I had the same problem and just solved it.

At first, I was generating the fat jar with the maven-assembly-plugin, which created a file called mavenproject1-0.0.1-SNAPSHOT-jar-with-dependencies.jar. This file gave the same problem as you mentioned when I tried running it.

I think that because it's a Spring Boot application, you need to use their plug-in. I changed my packaging to the spring-boot-maven-plugin and it generates two files: mavenproject1-0.0.1-SNAPSHOT.jar and mavenproject1-0.0.1-SNAPSHOT.jar.original. Just try java -jar target/mavenproject1-0.0.1-SNAPSHOT.jar, and it will hopefully work. :-)

For reference, here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.company.common</groupId>
            <artifactId>common-files</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies> 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                  <executions>
                    <execution>
                      <goals>
                        <goal>repackage</goal>
                      </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.2-beta-5</version>
              <configuration>
                <archive>
                  <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.company.mavenproject1.MainClass</mainClass>
                  </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
              </configuration>
              <executions>
                <execution>
                  <id>assemble-all</id>
                  <phase>package</phase>
                  <goals>
                    <goal>single</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
        </plugins>
    </build>
</project>

Solution 2

This is the solution :

  1. add only plugin in POM.xml

    <build>
    <plugins>
       <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <mainClass>${start-class}</mainClass>
            </configuration>
              <executions>
                <execution>
                  <goals>
                    <goal>repackage</goal>
                  </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    

  2. One property in pom.xml

    <properties>
      <start-class>com.xxx.yyy.MainClass</start-class>
    </properties>
    
  3. Run command

    mvn clean package spring-boot:repackage
    
  4. Now run your jar (it includes all dependencies now) created inside target folder

    java -jar my-service-0.0.1-SNAPSHOT.jar
    

Solution 3

Alternatively, if you cannot use the spring boot plugin for whatever reason, you can include the following file in your application. It should be called src/main/resources/META-INF/spring.factories:

https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

This will work for spring boot 1.x and is helpful if you want to use spring managed beans in a REPL.

Solution 4

This configuration works for me. I execute my JAR as a service but the line is:

javaw -jar MY_JAR.jar

POM:

<properties>
        <!-- Spring boot main class -->
        <start-class>com.PATH_TO_MAIN.Main</start-class>
</properties>
...
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

With Spring Boot, it is no longer necessary to use the assembly-plugin. Do not forget to call the package goal.

Here is the documentation example.

Solution 5

I have added this configuration in my pom and the issue got resolved:

<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.2.RELEASE</version>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>
Share:
106,589

Related videos on Youtube

Defozo
Author by

Defozo

E-mail: [email protected]

Updated on August 01, 2021

Comments

  • Defozo
    Defozo almost 3 years

    Everything was working fine when starting my app using IntelliJ. But when I made a fat jar and executed it with

    java -jar myapp.jar
    

    I got this:

    16:37:44.194 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [pl.mypackage.Main]; 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:489)
            at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191)
            at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321)
            at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
            at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
            at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
            at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
            at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
            at pl.mypackage.Main.main(Main.java:39)
    Caused by: 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.util.Assert.notEmpty(Assert.java:276)
            at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getCandidateConfigurations(EnableAutoConfigurationImportSelector.java:145)
            at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:84)
            at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:481)
            ... 14 common frames omitted
    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [pl.mypackage.Main]; 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:489)
            at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191)
            at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321)
            at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
            at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
            at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
            at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
            at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
            at pl.mypackage.Main.main(Main.java:39)
    Caused by: 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.util.Assert.notEmpty(Assert.java:276)
            at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getCandidateConfigurations(EnableAutoConfigurationImportSelector.java:145)
            at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:84)
            at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:481)
            ... 14 more
    

    It looks like it doesn't found auto configuration classes in META-INF/spring.factories. How to add it, and what should be the content of this file?

    • Andy Wilkinson
      Andy Wilkinson almost 8 years
      How did you make your fat jar?
    • Simon Stastny
      Simon Stastny almost 8 years
      I had the same problem because I removed the spring-boot-starter-parent from my pom.xml and was making the jar using the assembly plugin like I am used to. Mea culpa. Using spring-boot-starter-parent works like a charm and find the correct main class automatically.
  • rtoip
    rtoip over 7 years
    It's worth noting that <packaging>jar</packaging> in pom.xml is necessary. Without it you can still run the application in your IDE, but .jar will not work.
  • shimatai
    shimatai over 7 years
    Worked for me! I'm using Spring Boot 2.0
  • Stefan Falk
    Stefan Falk over 7 years
    This does not seem to work if the spring application is just another dependency within another standalone jar.
  • peterh
    peterh over 6 years
    The file doesn't exist any more.
  • cscan
    cscan over 6 years
    @peterh as of what version? It is present as of 1.5.4.
  • cscan
    cscan over 6 years
    Thanks @peterh. Looks like they removed that file from spring boot 2, I've updated the answer to reflect that.
  • PGMacDesign
    PGMacDesign almost 6 years
    What is the build command you are using after making these changes? mvn clean install? mvn package? Or something else?
  • Nobita
    Nobita over 5 years
    @Benoit Larochelle what's the gradle version solution for this issue?
  • Yash Digant Joshi
    Yash Digant Joshi about 5 years
  • balsick
    balsick about 5 years
    what if I cannot use spring-boot-starter-parent as parent?
  • hila shmueli
    hila shmueli almost 5 years
    @Benoit Larochelle Is it just me, or do you need to define the start-class property to make this work?
  • Sagar Joon
    Sagar Joon about 4 years
    Build command to use here : mvn clean package spring-boot:repackage
  • Aliaksandr Arashkevich
    Aliaksandr Arashkevich over 3 years
    How do we add it?
  • Raj Kumar
    Raj Kumar over 3 years
    i am using java 8 only still this error is coming. please let me know if you have some updates regarding this.
  • Raj Kumar
    Raj Kumar over 3 years
    @Nobita have you resolved your issue ? i am also facing the same issue
  • Robert Gabriel
    Robert Gabriel over 3 years
    Sorry Raj, haven't touch Java recently, so I don't have any update.
  • Adam Burley
    Adam Burley over 3 years
    I'm not exporting anything from Eclipse, only using the command-line mvn and still get this error. Also it's bad practice to do mvn clean install just to build something, especially for snapshot versions as you will get lots of issues with libraries picking up older versions. That command is used to install something into your local repository; if you are just taking it from the target folder then I don't know why you would need to install it.
  • Adam Burley
    Adam Burley over 3 years
    I think most people are trying to do this from the command line, without Eclipse. for example to share as technical instructions with people who may or may not have Eclipse installed.
  • Adam Burley
    Adam Burley over 3 years
    This is about the command line (terminal) so not sure where you are clicking something...
  • Adam Burley
    Adam Burley over 3 years
    Why would you want to put addClasspath=true here? I would have thought you wouldn't want to add the classpath to the manifest if you are building a fat jar?
  • NBJack
    NBJack about 3 years
    Let's not answer questions without reading them. This has nothing to do with Eclipse.
  • nikolai.serdiuk
    nikolai.serdiuk about 3 years
    Tried to add spring.factories file, but in resulted jar file it is not included, may I miss something?
  • panoet
    panoet almost 3 years
    In my case, this is the answer.
  • kohane15
    kohane15 over 2 years
    This works for me too. In other words, you want to build the jar with Maven, not with Intellij. You may achieve the same result with just building the jar with the maven command mvn package in your project directory.