java 如何从spring boot jar中排除资源文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32840749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exclude resource file from spring boot jar?
提问by Mejmo
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
插件生成 jar。我有多个带有配置 ( 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.
没有它,它工作正常。
回答by jfcorugedo
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
如果您想了解有关配置文件的更多信息,请查看配置文件
回答by Nick A. Watts
The Spring Boot Maven plugin repackages the JAR file created by the Maven JAR Plugin. 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:
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>
回答by Feiyu Zhou
Using maven-resources-plugin, it support excludes tag.
使用 maven-resources-plugin,它支持 excludes 标签。
By the way, why you need to use three yaml files? you can write those configuration in one application.yaml file with "---" and spring.profiles, like:
顺便问一下,为什么需要使用三个 yaml 文件?您可以使用“---”和 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