使用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 Boot(启动)应用程序.
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
但是,我不得不修改我的pom.xml
以解决不同的环境构建问题.特别是,我正在使用Maven配置文件来修改启动应用程序的属性文件.现在,当我运行前面提到的命令时,启动应用程序将无法运行,并出现以下异常.
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.
原因:java.lang.NumberFormatException:对于输入字符串:"$ {MULTIPART.MAXREQUESTSIZE}"
Caused by: java.lang.NumberFormatException: For input string: "${MULTIPART.MAXREQUESTSIZE}"
我有一个位于src/main/resources/config/application.properties
的属性文件.而且此属性文件具有一堆键/值对,如下所示.
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}
然后在我的pom.xml
中,我的构建定义如下.
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>
我注意到,如果我键入mvn clean package
并查看jar
文件内部,则application.properties
文件位于jar内.
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 spring-boot:run
,则applications.properties
文件在罐子内不是.实际上,src/main/resources
下没有任何内容可以将其放入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.
-
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?
推荐答案
作为 mvn spring-boot:run
默认情况下在类路径的前面添加src/main/resources
以支持热重载.您可以轻松关闭
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>
这篇关于使用Spring Boot Maven插件时jar文件中缺少Spring Boot应用程序中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!