如何从spring boot jar中排除资源文件? [英] How to exclude resource file from spring boot jar?

查看:1843
本文介绍了如何从spring boot jar中排除资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 maven-spring-boot 插件来生成jar。我有多个配置资源文件( application-production.yml,application-test.yml,application-development.yml )。

I am using maven-spring-boot plugin for jar generating. I have multiple resource files with configuration (application-production.yml, application-test.yml, application-development.yml).

事实上,当我为客户生成版本时,我想排除开发和测试文件。是否可以在 maven-spring-boot 插件中排除资源文件?

Thing is, when I generate the release for our customers, I would like to exclude development and test files. Is it possible to exclude resource file in maven-spring-boot plugin?

我试过这个:

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>application-dev*</exclude>
                        <exclude>application-test*</exclude>
                    </excludes>
                </resource>
            </resources>
        </build>

但maven插件使用自己的脚本进行资源管理(例如@ val @ replacement等)如果将它添加到pom中,则在打包期间失败:

but the maven plugin uses its own scripts for resource management (for example @val@ replacing etc.) and it fails during packaging if it is added to pom:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character @ '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 4, column 18:
    project.version: @project.version@

没有它,它运作正常。

推荐答案

而不是使用maven-spring-boot插件使用maven-resource插件和maven配置文件:

Instead of using maven-spring-boot plugin use maven-resource plugin and maven profiles:

<profiles>
  <profile>
    <id>prod</id>
    <build>
      <resources>
        <resource>
          <filtering>true</filtering>
          <directory>[your directory]</directory>
          <excludes>
            <exclude>[non-resource file #1]</exclude>
            <exclude>[non-resource file #2]</exclude>
            <exclude>[non-resource file #3]</exclude>
            ...
            <exclude>[non-resource file #n]</exclude>
          </excludes>
        </resource>
      </resources>
    </build>
  </profile>
</profiles>

确保指定< filtering> true< / filtering> 选项。

为每个环境创建一个配置文件并过滤这些文件。

Create one profile for each environment and filter those files.

确保使用正确的配置文件执行maven:

Make sure to execute maven with the proper profile:

mvn clean install -P prod

要查看maven-resource插件的更多示例,请查看 maven-resource

To view more examples of maven-resource plugin take a look at maven-resource

如果您想了解更多关于个人资料的信息,请查看个人资料

If you want to learn more about profiles, take a look at profiles

这篇关于如何从spring boot jar中排除资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆