增加Maven构建profile配置
在项目最顶层的pom.xml添加common和release两个profile,并声明${app.run.env}作为环境切换变量
<profiles>
<profile>
<id>common</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<app.run.env>common</app.run.env>
</properties>
</profile>
<profile>
<id>release</id
<properties>
<app.run.env>release</app.run.env>
</properties>
</profile>
</profiles>
注意在修改profile时,需要确定现有的properties和profile配置,避免冲突或者遗漏
使用Maven filter
在war工程pom文件或顶层父pom文件中增加resources filter配置<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
修改输出的war包名称为应用的名称,如basis,这个名称要严格对应到应用名和tomcat的contextpath
<build>
//..
<finalName>basis</finalName>
</build>
内嵌配置文件
- 拷贝线下配置文件:src/main/resources/META-INF/config/env-common/app-config.properties
- 拷贝线上配置文件:src/main/resources/META-INF/config/env-release/app-config.properties
修改配置文件位置
- 文件web.xml中可能存在配置文件初始化,注意替换
- 文件src/main/resources/META-INF/spring/applicationContext.xml
<!-- 配置参数 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:META-INF/config/env-${app.run.env}/app-config.properties</value>
</property>
</bean>
日志配置修改
- web.xml中配置log4j.xml或logback
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:META-INF/config/env-${app.run.env}/log4j.xml</param-value> </context-param> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:META-INF/config/env-${app.run.env}/logback.xml</param-value> </context-param>
- 拷贝log4j.xml或logback.xml文件到src/main/resources/META-INF/config/env-common或env-release下面
- 修改日志文件编码为UTF-8
- 修改日志文件目录为/opt/logs/${project.build.finalName}/${project.build.finalName}.log
- 删除原先划分的各种appender,所有日志输出合并到同一个文件即可 /opt/logs/${project.build.finalName}/${project.build.finalName}.log
若两个环境的log配置文件没有差别,可以直接放在META-INF/config下,或者使用maven filter来替换文件中的部分变量,如level
tomcat jdbc连接池配置(如果系统没有使用tomcat的jdbc连接,忽略此配置)
- context.xml拷贝, 如果系统没有使用tomcat的jdbc连接,忽略此配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>assembly-deploy-package</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<copy file="${basedir}/src/main/resources/META-INF/config/env-${app.run.env}/context.xml"
tofile="${project.build.directory}/${project.build.finalName}/META-INF/context.xml" overwrite="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
-
拷贝线下context文件, 如果系统没有使用tomcat的jdbc连接,忽略此配置:/src/main/resources/META-INF/config/env-common/context.xml
-
拷贝线上context文件, 如果系统没有使用tomcat的jdbc连接,忽略此配置:/src/main/resources/META-INF/config/env-release/context.xml