【问题标题】:How to exclude resource file from spring boot jar?如何从spring boot jar中排除资源文件?
【发布时间】:2015-09-29 09:43:15
【问题描述】:

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

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

我试过了:

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

但是 maven 插件使用自己的脚本进行资源管理(例如@val@ 替换等),如果添加到 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@

没有它,它工作正常。

【问题讨论】:

  • 为什么要排除它们?是因为您不希望他们能够看到这些文件是因为它们导致错误吗? Spring boot 默认支持多个application-{profile}.yml 文件,所以这应该不是问题。
  • 是的,我不希望每个版本都删除客户不应该看到的文件(test/dev db 的密码等)。另一种选择是使用参数运行 spring boot jar,从而设置所有必需的值 - 但不是绝对舒服。
  • 您还可以拥有一个外部application.properties 文件(与您的应用程序 JAR/WAR 位于同一文件夹中)。
  • 我确信通过一些 Maven 调整可以实现您的目标,但我认为您不应该以这种方式创建工件。生成的 jar 不应该包含生产属性,而是应该在 jar 外部,如果您遵循 these 约定,Spring Boot 可以自动获取这些属性
  • @g00glen00b,您想要排除它们的一个原因是要将开发/测试工件排除在生产交付之外。许多代码扫描器会认为它们的存在是一种安全发现。

标签: java maven spring-boot


【解决方案1】:

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

<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>

确保在资源元素中指定 &lt;filtering&gt;true&lt;/filtering&gt; 选项。

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

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

mvn clean install -P prod

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

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

【讨论】:

  • maven-resource 插件不使用属性值的就地编辑。这就是你不能在属性文件中使用@project.version@ ​​的原因。
  • 确保您已指定&lt;filtering&gt;true&lt;/filtering&gt;
  • 谢谢你。最后我回到这个线程,真的,你解决了..太棒了!
【解决方案2】:

Spring Boot Maven 插件重新打包由 Maven JAR 插件创建的 JAR 文件。因此,您还可以选择在首次构建 JAR 时简单地排除文件,这可以防止 Spring Boot Maven 插件首先找到它们:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <excludes>
             <exclude>application-dev*</exclude>
             <exclude>application-test*</exclude>
        </excludes>
    </configuration>
</plugin>

【讨论】:

  • 恕我直言,这是最好和最干净的解决方案,允许继续使用 spring boot maven 插件。
  • 这个答案不包括配置文件。通常,资源被排除在每个配置文件中,并且在答案中不包括配置文件。
  • 我喜欢通用方法,因为我希望它适用于所有配置文件。
【解决方案3】:

使用maven-resources-plugin,支持排除标签。

对了,为什么要使用三个 yaml 文件?你可以在一个 application.yaml 文件中使用“---”和 spring.profiles 编写这些配置,例如:

memcached.addresses: test03:11211
spring.profiles.active: dev
---
# dev
spring:
    profiles: dev
logging.access.enabled: false
---
# test
spring:
    profiles: test
logging.config: classpath:log4j.test.properties
logging.access.dir: /home/shared/log
---
# online
spring:
    profiles: online
logging.config: classpath:log4j.online.properties
logging.access.dir: /home/shared/log

【讨论】:

  • Spring boot 还支持开箱即用的多个application-{profile}.yml 文件。
  • maven-resource-plugin 不支持就地替换@value@ 标签。引导插件使所有这些
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2019-08-12
  • 1970-01-01
相关资源
最近更新 更多