Antonio La Marra
Antonio La Marra

Reputation: 7479

Springboot application.properites not read

I've developed a springboot application with Maven in Eclipse. The class annotated with @SpringBootApplication reads the application.properties inside src/main/resources. Inside Eclipse everything works fine. Using Maven I've generated a fat jar, this is the plugin I'm using:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

In the target folder 2 jars are generated, one named fatjar-exec.jar and the other fatjar.jar. When I run the command java -jar fatjar-exec.jar an exception is thrown since the application is not able to read the application.properties file. I have also unzipped the jar and correctly the applciation.properties is located under BOOT-INF/classes folder. Any hints?

Upvotes: 1

Views: 866

Answers (2)

DEBENDRA DHINDA
DEBENDRA DHINDA

Reputation: 1193

This is working for me fine too. My POM.xml is as follows :

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>xxx</groupId>
    <artifactId>yyy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>   
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

mvn package -DskipTests

java -jar xxx-0.0.1-SNAPSHOT.jar

Upvotes: 0

Lesiak
Lesiak

Reputation: 26074

Please compare the contents of generated fatjars. The regular one (without exec) has only one copy of springboot classes, while the one generated with the clasifier has two.

  • one under /org/springframework/boot/loader (expected)
  • second under /BOOT-INF/classes/org/springframework/boot/loader

Probably the order of classpath search causes the file from the unexpected location to be picked up, and it cannot find the properties in /BOOT-INF/classes

IMHO the simplest version works best:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Check Custom repackage classifier for details how to configure maven if you want to keep the origial file (you were missing <id>repackage</id>).

Upvotes: 2

Related Questions