简体   繁体   English

使用属性文件和Maven配置文件外部化Web App Config的最佳方法

[英]Best Way to Externalize Web App Config using Properties file and Maven Profiles

Have a Java Maven web app which has hardcoded properties set in the following configuration files residing in: 拥有一个Java Maven Web应用程序,该应用程序在以下驻留在其中的配置文件中设置了硬编码属性:

src/main/resources/database.xml SRC /主/资源/ database.xml

<beans xmlns="http://www.springframework.org/schema/beans">
      <bean id="aDatasource"  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
      <property name="url"><value>jdbc:mysql://anIpAddress/mydatabase?zeroDateTimeBehavior=convertToNull</value></property>
      <property name="username"><value>root</value></property>
      <property name="password"><value>password</value></property>
   </bean> 
</beans>

src/main/resources/log4.j.properties SRC /主/资源/ log4.j.properties

log4j.appender.RF.File=/opt/apache-tomcat-7.0.78/webapps/myapp/WEB-INF/classes/logs.log

Before I run maven clean install, I am constantly editing these files by replacing inside database.xml: 在运行maven全新安装之前,我将通过替换database.xml内部来不断编辑这些文件:

aniPAddress with localhost
root with my local database's root user
password with my local database's root password

Inside log4.properties: 在log4.properties内部:

/opt/apache-tomcat-7.0.78/ with $CATALINA_HOME

Question(s): 问题(S):

  1. Is there a way to externalize the configuration files so I don't have to explicitly change it everytime I do a fresh Git checkout? 有没有一种方法可以将配置文件外部化,因此我不必在每次进行新的Git检出时都明确地更改它?

eg 例如

should I create an application.properties file located inside src/main/resources containing the following properties: 我应该在src / main / resources内部创建一个application.properties文件,其中包含以下属性:

database.url = localhost
database.user = root
database.password = 
tomcat.home = $CATALINA_HOME or /etc/tomcat7/

How would these be explicitly set inside database.xml and / or log4j.properties? 如何在database.xml和/或log4j.properties中显式设置这些?

  1. Is there a way to use maven profile features to switch config properties? 有没有一种方法可以使用Maven配置文件功能来切换配置属性?

eg 例如

maven clean -install -p dev 
maven clean -install -p prod

How to configure the pom.xml file to reflect the behavior required in question 1? 如何配置pom.xml文件以反映问题1所需的行为?

You can use the resource filtering, see https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html 您可以使用资源过滤,请参阅https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Given the following pom: 鉴于以下pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/filtered-resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <jdbc.url>localhost</jdbc.url>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <jdbc.url>server.prod</jdbc.url>
        </properties>
    </profile>
</profiles>
</project>

And the following content in the file src/main/filtered-resource/application.properties: 文件src / main / filtered-resource / application.properties中的以下内容:

database.url = ${jdbc.url}

with "mvn -Pdev process-resource" the file target/classes/application.properties will contain: 使用“ mvn -Pdev进程资源”文件,目标/类/application.properties将包含:

database.url = localhost

and with "mvn -Pprod process-resource": 并带有“ mvn -Pprod process-resource”:

database.url = server.prod

Found the answer to my first question regarding how to load the Spring datasource database.xml upon server startup based on external properties: 找到了我的第一个问题的答案,该问题关于如何基于外部属性在服务器启动时加载Spring数据源database.xml:

It's as simple as putting this entry inside the database.xml file: 只需将以下条目放入database.xml文件中即可:

<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreResourceNotFound" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="location" value="file:/proplocation/database.properties"></property>
</bean>

No one knows the answer to Queston # 2? 没有人知道Queston#2的答案吗?

Is there a way to use maven profile features to switch config properties? 有没有一种方法可以使用Maven配置文件功能来切换配置属性?

eg 例如

maven clean -install -p dev
maven clean -install -p prod

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

相关问题 Spring Web应用程序:如何外部化属性文件? - Web app with Spring: how externalize properties file? 在 Helidon MP 中外部化 microprofile-config.properties 文件的任何方式 - Any way to Externalize microprofile-config.properties file in Helidon MP 如何外部化Web应用程序的属性文件(WAR)? - How to externalize properties file for web application(war)? 在pom.xml中外部化属性的最佳方法是什么 - What is the best way to externalize properties in pom.xml Spring Boot-外部化配置属性 - Spring boot - Externalize config properties 使用Maven和Spring控制项目:如何使用Maven配置文件设置Spring配置文件? - Controlling a project with Maven and Spring: How to set Spring config file using Maven profiles? 哪种是在桌面应用程序上外部化数据库配置的最佳方法? - which is the best way to externalize a database configuration on a desktop app? 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? 外部化配置文件 Java Spring - Externalize Config File Java Spring 使用命令行 Java 外部化 log4j.properties 文件 - Externalize log4j.properties file using command line Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM