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
resources in a Spring Boot application are missing from jar file when using Spring 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.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.
但是,我不得不修改我的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 package
and look inside the jar
file, the application.properties
file 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.properties
file is notinside the jar. In fact, nothing under src/main/resources
makes 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.
这个问题对我来说有点烦人,因为如果我想从命令行运行我的启动应用程序,我现在必须做两个步骤。
mvn clean package
java -jar ./target/app-0.0.1-SNAPSHOT.jar
mvn clean package
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:run
adds src/main/resources
in 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