20

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?

Jane Wayne
  • 8,205
  • 17
  • 75
  • 120

3 Answers3

12

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>
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • I tried to upgrade my spring-boot version to 1.3.2.RELEASE, and now the solution you proposed breaks. The properties files are being copied over but the Maven build is not replacing the properties values. The documentation you referred me to even says `Note that a side effect of using this feature is that filtering of resources at build time will not work.` Any ideas? I didn't want to post a new question. – Jane Wayne Jan 05 '16 at 11:05
  • [Read the release notes](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering)? I don't think this has anything to do with your problem. That's only controlling what happens when you run the app using the Maven plugin. – Stephane Nicoll Jan 05 '16 at 12:02
  • i find the same question with you and how do you fix that? – Chevalier Mar 09 '16 at 08:42
2

Try just this :

 <resources>
        <resource>
            <directory>src/main/resources/config</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>  
</resources>
question_maven_com
  • 2,457
  • 16
  • 21
  • That was the first try I made, and it didn't work. With that I get this exception: `FileNotFoundException: class path resource [config/custom-application.properties] cannot be opened because it does not exist`. Please note that in addition to the Spring Boot `application.properties` file, I have one more additional file `custom-application.properties` also in the `src/main/resources/config` directory (I am actually loading this properties file up manually). Thanks, but that doesn't work or help. Again, nothing in `src/main/resources` is being copied into the jar or the `target/classes` dir. – Jane Wayne Oct 18 '15 at 11:46
  • This solution worked for me. I had some .jrxml files that I wanted to include. – Fernando Aug 14 '17 at 13:37
  • Be careful if you have an override resoureces/resource/directory sometimes it *disables* the default one, so you have to manually add that back in there too...see also https://stackoverflow.com/questions/35417086/maven-resource-filtering-with-spring-boot-could-not-resolve-placeholder – rogerdpack Oct 24 '18 at 22:34
0

Since Spring Boot 1.3, in order to have resources filtered as expected, we have to use new @@ format:

some.key = @value@

instead of classic:

some.key = ${value}

Relevant spring-boot issue: https://github.com/spring-projects/spring-boot/issues/980

Nikita Bosik
  • 851
  • 1
  • 14
  • 20