简体   繁体   English

如何从spring boot jar中排除资源文件?

[英]How to exclude resource file from spring boot jar?

I am using maven-spring-boot plugin for jar generating.我正在使用maven-spring-boot插件来生成 jar。 I have multiple resource files with configuration ( application-production.yml, application-test.yml, application-development.yml ).我有多个带有配置的资源文件( application-production.yml, application-test.yml, application-development.yml )。

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?是否可以在maven-spring-boot插件中排除资源文件?

I tried this:我试过这个:

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

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

without it, it works ok.没有它,它工作正常。

Instead of using maven-spring-boot plugin use maven-resource plugin and maven profiles:而不是使用 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>

Make sure you specify <filtering>true</filtering> option inside resource element.确保在资源元素中指定<filtering>true</filtering>选项。

Create one profile for each environment and filter those files.为每个环境创建一个配置文件并过滤这些文件。

Make sure to execute maven with the proper profile:确保使用正确的配置文件执行 maven:

mvn clean install -P prod

To view more examples of maven-resource plugin take a look at maven-resource要查看 maven-resource 插件的更多示例,请查看maven-resource

If you want to learn more about profiles, take a look at profiles如果您想了解有关配置文件的更多信息,请查看配置文件

The Spring Boot Maven plugin repackages the JAR file created by the Maven JAR Plugin. Spring Boot Maven 插件重新打包了 Maven JAR 插件创建的 JAR 文件。 So you also have the option to simply exclude files when the JAR is first built, which keeps the Spring Boot Maven plugin from finding them in the first place:因此,您还可以选择在首次构建 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>

Using maven-resources-plugin, it support excludes tag.使用 maven-resources-plugin,它支持排除标签。

By the way, why you need to use three yaml files?顺便问一下,为什么需要使用三个 yaml 文件? you can write those configuration in one application.yaml file with "---" and spring.profiles, like:您可以使用“---”和 spring.profiles 在一个 application.yaml 文件中编写这些配置,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM