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

     2023-02-26     63

关键词:

【中文标题】使用 Spring Boot Maven 插件时,jar 文件中缺少 Spring Boot 应用程序中的资源【英文标题】:resources in a Spring Boot application are missing from jar file when using Spring Boot Maven Plugin 【发布时间】:2016-01-16 18:51:21 【问题描述】:

我正在使用 Spring-Boot v1.3.0.M5 和 Maven v3.3.3。我以前可以使用这个命令从控制台运行我的 Spring Boot (boot) 应用程序。

mvn clean package spring-boot:run

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

原因:java.lang.NumberFormatException:对于输入字符串:“$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 文件。

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

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

关于我做错了什么有什么想法吗?

【问题讨论】:

【参考方案1】:

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

some.key = @value@

而不是经典:

some.key = $value

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

【讨论】:

【参考方案2】:

由于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.2.RELEASE,现在你提出的解决方案中断了。正在复制属性文件,但 Maven 构建没有替换属性值。你推荐给我的文档甚至说Note that a side effect of using this feature is that filtering of resources at build time will not work.有什么想法吗?我不想发布新问题。 Read the release notes?我认为这与您的问题无关。这只是控制使用 Maven 插件运行应用程序时发生的情况。 我和你有同样的问题,你如何解决这个问题?【参考方案3】:

试试这个:

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

【讨论】:

这是我第一次尝试,但没有成功。有了这个我得到了这个例外:FileNotFoundException: class path resource [config/custom-application.properties] cannot be opened because it does not exist。请注意,除了 Spring Boot application.properties 文件之外,我还有一个附加文件 custom-application.properties 也在 src/main/resources/config 目录中(我实际上是手动加载此属性文件)。谢谢,但这不起作用或没有帮助。同样,src/main/resources 中的任何内容都不会被复制到 jar 或 target/classes 目录中。 这个解决方案对我有用。我有一些想要包含的 .jrxml 文件。 小心如果你有一个覆盖资源/资源/目录有时它禁用默认的,所以你也必须手动将它添加回那里......另请参阅***.com/questions/35417086/…

使用 Spring Boot BOM 管理 Maven 插件版本

】使用SpringBootBOM管理Maven插件版本【英文标题】:ManagingMavenpluginversionswithSpringBootBOM【发布时间】:2021-08-0213:35:58【问题描述】:我有一个用于SpringBoot项目的MavenPOM。POM导入SpringBootBOM。现在我想自定义一个Maven插件的配置,比如m... 查看详情

Spring boot maven插件:包含测试资源

】Springbootmaven插件:包含测试资源【英文标题】:Springbootmavenplugin:includetestresources【发布时间】:2019-04-2522:39:18【问题描述】:我的目录结构如下:src||___main||___test||___resources在pre-integration-test阶段运行启动目标(spring-boot:start,... 查看详情

spring-boot-maven插件repackage(goal)的那些事

前言:在打包Springboot项目成jar包时需要在pom.xml使用spring-boot-maven-plugin来增加Maven功能,在我的上一篇博客<<Maven生命周期和插件的那些事(2021版)>>中已经介绍过Maven和插件的关系,在此不再赘述,... 查看详情

无法理解 Spring Boot 如何使用 maven 管理集成测试。它是不是使用故障安全插件?

】无法理解SpringBoot如何使用maven管理集成测试。它是不是使用故障安全插件?【英文标题】:Can\'tunderstandhowSpringBootmanageintegrationtestswithmaven.Isitusesfailsafepluginornot?无法理解SpringBoot如何使用maven管理集成测试。它是否使用故障安全... 查看详情

使用 spring boot 和 spring-boot-maven-plugin 生成战争时排除 application.properties

】使用springboot和spring-boot-maven-plugin生成战争时排除application.properties【英文标题】:Excludeapplication.propertieswhengeneratingwarusingspringbootandspring-boot-maven-plugin【发布时间】:2014-12-3120:51:54【问题描述】:我正在使用SpringBoot开发一个Web... 查看详情

Maven-在当前项目和插件组中找不到前缀“spring-boot”的插件

...间】:2015-08-3113:55:14【问题描述】:我正在尝试构建一个使用SpringToolsSuite构建的springboot项目。执行$mvnspring-boot: 查看详情

找不到符号 - Spring Boot Maven 兄弟作为依赖

...iblingasdepency【发布时间】:2019-07-1706:32:27【问题描述】:使用Mavenspringboot插件重新打包没有帮助。已尝试清理.m2文件夹。从sts和命令行构建项目。添加了组件扫描器来扫描实体,但在maven构建本身时失败。添加了SpringBootMaven插件... 查看详情

使用 Gradle 和 Spring Boot 时无法发布到本地 maven 存储库

】使用Gradle和SpringBoot时无法发布到本地maven存储库【英文标题】:UnabletopublishtolocalmavenrepowhenusingGradleandSpringBoot【发布时间】:2014-07-0304:14:45【问题描述】:我有一个非常简单的spring-boot(1.0.2)示例(基于http://spring.io/guides/gs/rest-se... 查看详情

Maven Spring Boot 插件:如何从另一个项目运行 Spring Boot

】MavenSpringBoot插件:如何从另一个项目运行SpringBoot【英文标题】:MavenSpringBootPlugin:howtorunspringbootfromanotherproject【发布时间】:2018-11-0714:02:26【问题描述】:https://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html我有一个项目,... 查看详情

Spring Boot Maven 插件 - spring-boot:run 和优雅关闭

】SpringBootMaven插件-spring-boot:run和优雅关闭【英文标题】:SpringBootMavenplugin-spring-boot:runandgracefulshutdown【发布时间】:2018-01-0714:00:27【问题描述】:我可以使用以下Maven命令运行SpringBoot应用程序:mvnspring-boot:run但我不知道如何优雅... 查看详情

maven插件系列之spring-boot-maven-plugin

Maven插件系列之spring-boot-maven-pluginSpringBoot的Maven插件(SpringBootMavenplugin)能够以Maven的方式为应用提供SpringBoot的支持,即为SpringBoot应用提供了执行Maven操作的可能。SpringBootMavenplugin能够将SpringBoot应用打包为可执行的jar或war文件&#... 查看详情

带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)

...序,由于某些限制,我需要取消spring-boot-maven-plugin并需要使用maven-sh 查看详情

Spring boot Flyway Jooq Code gen maven 插件顺序

...强制flyway:migrate始终在jooq:generate-sources之前执行?我正在使用带有SpringBoot的Maven包装器。<plugin><groupId&g 查看详情

使用 AttributeConverter 的 Spring Boot 测试不适用于 Maven 测试

】使用AttributeConverter的SpringBoot测试不适用于Maven测试【英文标题】:SpringboottestwithAttributeConverternotworkingwithMaventest【发布时间】:2021-11-0213:33:14【问题描述】:我有Spring引导单元测试(使用@SpringBootTest注释的类)在使用我的IDE时... 查看详情

使用“spring-boot-starter-parent”时如何在 Maven 中使用较低的 Elastic 搜索版本

】使用“spring-boot-starter-parent”时如何在Maven中使用较低的Elastic搜索版本【英文标题】:HowtouselowerElasticsearchversioninMavenwhenusing"spring-boot-starter-parent"【发布时间】:2017-11-0920:39:18【问题描述】:我正在为Webflux使用版本为“... 查看详情

Spring Boot Maven 插件 - 没有 BOOT-INF 目录

】SpringBootMaven插件-没有BOOT-INF目录【英文标题】:SpringBootMavenPlugin-NoBOOT-INFdirectory【发布时间】:2017-08-2418:59:13【问题描述】:在spring-boot-maven-plugin的1.3.8.RELEASE版本和1.4.0.RELEASE版本之间-生成的包结构发生了变化(如果您提取uber... 查看详情

由于 maven 插件错误,spring boot maven 项目未编译

】由于maven插件错误,springbootmaven项目未编译【英文标题】:springbootmavenprojectisnotcompilingduetomavenpluginerror【发布时间】:2018-03-2000:53:42【问题描述】:我正在尝试在netbeans中运行一个简单的SpringBoot项目,但代码未编译。这是我得... 查看详情

Spring Boot Maven 插件包含资源

】SpringBootMaven插件包含资源【英文标题】:SpringBootMavenPluginIncludeResources【发布时间】:2018-07-3106:46:56【问题描述】:示例是否有可能以某种方式配置springbootmaven插件以包含依赖项中的资源。例如如果在我的SpringBoot项目中我有:&... 查看详情