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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33197230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:26:02  来源:igfitidea点击:

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

javaspringmavenspring-bootspring-boot-maven-plugin

提问by Jane Wayne

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.

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

mvn clean package spring-boot:run

mvn clean package spring-boot:run

However, I've had to revise my pom.xmlto 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.

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

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

引起:java.lang.NumberFormatException:对于输入字符串:“${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.

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

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

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

然后在 my 中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>

I noticed that if I type in mvn clean packageand look inside the jarfile, the application.propertiesfile is inside the jar.

我注意到,如果我输入mvn clean package并查看jar文件内部,则该application.properties文件位于 jar 中。

However, if I type in mvn clean package spring-boot:run, then the applications.propertiesfile is notinside the jar. In fact, nothing under src/main/resourcesmakes it into the jar file.

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

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
  1. mvn clean package
  2. java -jar ./target/app-0.0.1-SNAPSHOT.jar

Any ideas on what I am doing wrong?

关于我做错了什么的任何想法?

采纳答案by Stephane Nicoll

As described in the documentationmvn spring-boot:runadds src/main/resourcesin front of your classpath to support hot reload by default. You can turn this off easily

文档中所述,在您的类路径前面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>

回答by question_maven_com

Try just this :

试试这个:

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

回答by Nikita Bosik

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

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

some.key = @value@

instead of classic:

而不是经典:

some.key = ${value}

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

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