[英]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):
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中显式设置这些?
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.