Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resources in a Spring Boot application are missing from jar file when using Spring Boot Maven Plugin

I am using Spring-Boot v1.3.0.M5 with Maven v3.3.3. I used to be able to run my Spring Boot (boot) application from the console with this command.

mvn clean package spring-boot:run

However, I've had to revise my pom.xml to account for different environment builds. In particular, I am using Maven profiles to modify the properties files of boot application. Now when I run the previously mentioned command, the boot application fails to run and complains with the following exception.

Caused by: java.lang.NumberFormatException: For input string: "${MULTIPART.MAXREQUESTSIZE}"

I have a properties file located at src/main/resources/config/application.properties. And this properties file has a bunch of key-value pairs which looks like the following.

multipart.maxFileSize=${multipart.maxFileSize}
multipart.maxRequestSize=${multipart.maxRequestSize}

Then in my pom.xml, my build is defined as follows.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<profiles>
    <!-- development -->
    <profile>
        <id>env-dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <multipart.maxFileSize>250MB</multipart.maxFileSize>
            <multipart.maxRequestSize>250MB</multipart.maxRequestSize>
        </properties>
    </profile>
    <!-- staging -->
    <profile>
        <id>env-stg</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>env</name>
                <value>stg</value>
            </property>
        </activation>
        <properties>
            <multipart.maxFileSize>500MB</multipart.maxFileSize>
            <multipart.maxRequestSize>500MB</multipart.maxRequestSize>
        </properties>
    </profile>
<profiles>

I noticed that if I type in mvn clean package and look inside the jar file, the application.properties file is inside the jar.

However, if I type in mvn clean package spring-boot:run, then the applications.properties file is not inside the jar. In fact, nothing under src/main/resources makes it into the jar file.

This problem is a little annoying for me because if I want to run my boot application from the command line, I have to do two steps now.

  1. mvn clean package
  2. java -jar ./target/app-0.0.1-SNAPSHOT.jar

Any ideas on what I am doing wrong?

like image 979
Jane Wayne Avatar asked Oct 18 '15 11:10

Jane Wayne


People also ask

What are the Maven resource filtering issues with Spring Boot?

46 Maven Resource Filtering with Spring Boot: Could not resolve placeholder 0 Spring boot application not able to detect table defined in sqlite db file -2 Runnable jar unable to load application.properties file?

How to create a JAR file of Your Spring Boot application?

Spring Boot provides spring-boot-maven-plugin to create or build an executable JAR of your Spring Boot application. Step 1: Go to your Spring Boot application and open the pom.xml file. and add the below spring-boot-maven-plugin code just below the closing dependencies </dependencies> tag. Step 3: Now run the mvn clean package command.

How does Maven Spring Boot work with Java?

Inside the War Manifest As mentioned before, the Maven Spring Boot plugin finds the main class and generates the configuration needed for running the java command. The resulting MANIFEST.MF has some additional lines: In particular, we can observe that the last one specifies the Spring Boot class loader launcher to use. 4.5. Inside a Jar File

What are external resources in Spring Boot?

Frequently, we need to read external resources into our Spring application. Examples of external resources are text files, XML files, properties files, and image files. These resources may be present at different locations.


1 Answers

As described in the documentation mvn spring-boot:run adds src/main/resources in front of your classpath to support hot reload by default. You can turn this off easily

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.2.7.RELEASE</version>
      <configuration>
        <addResources>false</addResources>
      </configuration>
    </plugin>
    ...
  </plugins>
  ...
</build>
like image 61
Stephane Nicoll Avatar answered Sep 24 '22 15:09

Stephane Nicoll