简体   繁体   English

如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件?

[英]How to exclude default application.properties add custom properties file using profiles in maven for spring boot project?

I have developed a web application using spring boot. 我已经使用Spring Boot开发了一个Web应用程序。 I have three resource folders in src/main/resources staging,qa,production which consists of application properties and logging configuration for the specific environment. 我在src / main / resources阶段,qa,生产中有三个资源文件夹,其中包括针对特定环境的应用程序属性和日志记录配置。 Along with these folders I have application.properties and logging configuration in resources folder which I use it for dev environment. 除了这些文件夹外,我在资源文件夹中还有application.properties和日志记录配置,可将其用于开发环境。 I want to package war file according to the environment using spring-boot-maven plugin. 我想使用spring-boot-maven插件根据环境打包war文件。 I am new to maven any help would be appreciated? 我是新手,对您的帮助将不胜感激吗?

Convetion is application-{profileName}.properties 对流是application-{profileName}.properties

Point 10 and 11: 点10和11:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

eg application-test.properties it overrides the application.properties Make profile in pom.xml 例如application-test.properties它覆盖了application.propertiespom.xml配置文件

Introduction to profiles: http://maven.apache.org/guides/introduction/introduction-to-profiles.html 个人资料简介: http : //maven.apache.org/guides/introduction/introduction-to-profiles.html

Rather than using Maven to add different folders to your classpath for each environment, you can use Spring profiles. 您可以使用Spring配置文件,而不是使用Maven为每个环境将不同的文件夹添加到类路径中。

First of all create different application.properties for each environment, such as: 首先为每个环境创建不同的application.properties ,例如:

  • application-staging.properties application-staging.properties
  • application-qa.properties application-qa.properties
  • application-production.properties application-production.properties

For the logging you can use the logging.config property. 对于日志记录,可以使用logging.config属性。 So, in application-staging.properties you could use: 因此,在application-staging.properties中,您可以使用:

logging.config=classpath:logback-staging.xml

In the other properties files you can use different logging.config properties. 在其他属性文件中,可以使用其他logging.config属性。

Now just run your application with the spring.profiles.active property. 现在,只需使用spring.profiles.active属性运行您的应用程序。


However, an easier solution would be to use externalized configuration . 但是,更简单的解决方案是使用外部化配置 Rather than having to rebuild each time you want to change configuration for a specific profile, you can externalize it by putting an application.properties file next to your JAR/WAR in the correct environment, rather than on your classpath. 您不必在每次更改特定概要文件的配置时都进行重建,而是可以通过在正确的环境中而不是在类路径上将application.properties文件放在JAR / WAR旁边来对其进行外部化。 Spring boot will pick this up automatically. Spring Boot会自动将其启动。

Now you can also externalize your logging config by placing a logback.xml (or log4j2.xml, ...) file next to your JAR/WAR and just configure your (externalized) application properties with: 现在,您还可以通过在JAR / WAR旁边放置一个logback.xml(或log4j2.xml等)文件来外部化日志记录配置,并使用以下命令配置(外部化的)应用程序属性:

logging.config=file:logback.xml

This allows you to edit your configuration and logging without having to change your JAR/WAR. 这使您可以编辑配置和日志记录,而不必更改JAR / WAR。

暂无
暂无

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

相关问题 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file 如何将 application.properties 与 application-default.properties 合并并包含在 Spring Boot >2.4 的其他测试配置文件中 - How to merge application.properties with application-default.properties and include in other test profiles with Spring Boot >2.4 使用spring boot和spring-boot-maven-plugin生成war时排除application.properties - Exclude application.properties when generating war using spring boot and spring-boot-maven-plugin Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles 如何在 Spring 启动 Maven 项目中移动 application.properties 或执行外部属性 - How to Move application.properties or Do External Properties in Spring Boot Maven Project Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 使用 gradle 在 spring boot jar 中排除 application.properties - Exclude application.properties inside spring boot jar using gradle 如何在 application.properties 文件中覆盖 spring boot starter 的默认属性? - How to override spring boot starter's default properties in application.properties file? 为什么 Spring 引导不从默认的 application.properties 文件中读取 - Why is Spring Boot not reading from default application.properties file Spring Boot 项目中的 application.properties 文件在哪里? - Where is the application.properties file in a Spring Boot project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM