您的位置:首页 > 其它

使用maven管理不同环境的配置文件

2015-05-12 14:20 489 查看
1.使用maven中properties标签定义变量

(1)引入

在pom.xml中添加依赖时语法如下:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>1.2.6</version>
</dependency>
以上配置存在一个问题,就是在spring依赖中,我们需要引入一系列版本的spring,如版本1.2.6.每次都写不利于维护

(2)解决

在pom.xml中定义properties标签

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>1.2.6</spring.version>
<developer.organization><![CDATA[xy公司]]></developer.organization>
</properties>
那么上面的配置就可以写成:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>


2.使用maven profile中的filter

(1)总体概览

实际上filter是表示存储属性值的文件在哪(相当于from);

profile一般也就是个文件名,不同环境配置不同的;

<resource>中表示哪些文件需要替换,可以是目录

(2)profile

一个项目可以部署在不同的环境中,maven的profile针对不同的环境指定各自的编译方法。在pom.xml的profile中,可以根据不同的环境定制以下内容:

<profiles>
<profile>
<id>dev</id>
<activation> <!--默认激活的配置 -->
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
</profiles>

如果需要在执行的时候指定使用哪个profile,可以通过-P传入。如:maven package –P dev ,就可以使用dev的配置来打包了

(2)build配置项

build配置项可以出现在两处:

<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"> ...
<!-- 项目级别的构建,基础配置 -->
<build>...</build>

<profiles>
<profile>
<!-- 特殊的构建 -->
<build>...</build>
</profile>
</profiles>
</project>


(3)filter规则

maven通过过滤器来修改部署时的不同配置。

部署时所有资源的配置,如果根据环境不同,有不同的配置,则需要在资源中加上以下形式的标记表示替换

${要替换的属性名称}
如在spring.xml中要配置上传文件的路径

<beans>
<bean id="uploadService" class="com.oist.project.service.UploadServiceImpl">
<property name="uploadDir" value="${spring.uploadDir}"/>
</bean>
</beans>
在pom.xml中进行以下配置

<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"> ...
<build>
<filters> <!-- 指定 filter -->
<filter>src/main/filters/${deploy.env}.properties</filter>
</filters>
<resources>
<resource> <!-- spring.xml 应该在 src/main/resources 目录下 -->
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build>

<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<propertys>
<deploy.env>develop</deploy.env>
</propertys>
</profile>

<profile>
<id>test</id>
<propertys>
<deploy.env>test</deploy.env>
</propertys>
</profile>

<profile>
<id>production</id>
<propertys>
<deploy.env>production</deploy.env>
</propertys>
</profile>

</profiles>
</project>


然后就可以针对不同的环境设置不同的目录了:

src/main/filters/develop.properties文件

# 上传路径:
spring.uploadDir=c:/uploadDir
src/main/filters/test.properties文件

# 上传路径:
spring.uploadDir=/tmp/upload_dir
src/main/filters/production.properties文件

# 上传路径:
spring.uploadDir=/app/project/upload_dir
如果配置了多个filter,并且2个filter中有相同的key,则后面的value为最终取值

<build>
<filters>
<filter>src/main/filters/production.properties</filter>
<filter>src/main/filters/test.properties</filter>
</filters>
</build>
因为test.properties在最后,因为spring.uploadDir为test.properties的取值/tmp/upload_dir

实际上顺序就是:resource中找到需要匹配的文件->从需要属性的文件开始-->filter->properties中的值(profile在这里实际上就是作为参数,根据不同环境为filter赋值)

3.maven的properties加载顺序

(1)<build><filters>中的配置

(2)pom.xml中的<properties>

(3)mvn -Dproperty=value中定义的property

相同key的property,以最后一个文件中的配置为最终配置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐