使用 Spring Boot Maven 插件时,jar 文件中缺少 Spring Boot 应用程序中的资源

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

我正在使用 Spring-Boot v1.3.0.M5 和 Maven v3.3.3。我曾经能够使用此命令从控制台 运行 我的 Spring 启动(启动)应用程序。

mvn clean package spring-boot:run

但是,我不得不修改我的 pom.xml 以说明不同的环境构建。特别是,我使用 Maven 配置文件来修改启动应用程序的属性文件。现在,当我 运行 前面提到的命令时,启动应用程序无法 运行 并抱怨以下异常。

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

我有一个位于 src/main/resources/config/application.properties 的属性文件。这个属性文件有一堆键值对,如下所示。

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

然后在我的pom.xml中,我的构建定义如下

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

我注意到,如果我输入 mvn clean package 并查看 jar 文件,application.properties 文件就在 jar 中。

但是,如果我输入 mvn clean package spring-boot:run,则 applications.properties 文件在 jar 中 而不是 。事实上,src/main/resources 下的任何内容都无法进入 jar 文件。

这个问题对我来说有点烦人,因为如果我想 运行 从命令行启动我的应用程序,我现在必须执行两个步骤。

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

对我做错了什么有什么想法吗?

试试这个:

 <resources>
        <resource>
            <directory>src/main/resources/config</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>  
</resources>

As described in the documentation mvn spring-boot:run 在类路径前添加 src/main/resources 默认支持热重载。您可以轻松关闭此功能

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

Spring Boot 1.3 以来,为了按预期过滤资源,我们必须使用新的 @@ 格式:

some.key = @value@

而不是经典:

some.key = ${value}

相关 spring- 引导问题:https://github.com/spring-projects/spring-boot/issues/980