倚靠 maven根据 pom.xml文件里的 profile配置来对 dev test prod环境配置读取

倚靠 maven根据 pom.xml文件里的 profile配置来对 dev test prod环境配置读取

依靠 maven根据 pom.xml文件里的 profile配置来对 dev test prod环境配置读取
http://blog.csdn.net/mayi92/article/details/77892809

引用


说明一下,很多项目把配置文件与项目偶合在一起,比如与第三方的各种私密配置信息都与项目耦合在一起,导致什么结果,任何一个该项目的开发人员都能知道生产环境的各种配置,而且开发人员离职后一般都会把项目copy在自己的硬盘上,各种私密的配置信息很容易泄露。

好的架构,会把配置文件从项目中解耦,配置文件由各自不同的人员维护(开发环境有开发者维护,测试和生产由运维维护),生产的必须有运维专业人员操作和读取。做法有很多,不就是要读取指定位置的配置文件么。可以这么做,一个专门管理配置文件的插件config,config对外提供api,api使用@Component,一个配置文件定义一个javabean,在项目启动初始化的时候xml转java,项目中其它位置想读取配置时引用config插件。配置文件可以放在任何地方,远程或本地,可能放在服务器(tomcat)所在根目录的一个位置/var/项目名称/config/。

好开始正题
这是一个maven项目
使用spring的@value注入属性,@value 指定的值在env.properties中
方法一:属性配置文件的全量替换,使用maven的copy命令
建议先看方法二

// step 1 这样一个类
@Service("commonService")
public class CommonService implements org.springframework.beans.factory.InitializingBean{
    @Value("${account.source}")
    private String source;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化完成");
    }
}
1
2
3
4
5
6
7
8
9
10
// step 2
//src/main/resources下有一个配置文件env.properties,文件内容如下
account.source=123456
1
2
3
//step 3
/*
src/main/filters 下有3个配置文件dev.properties,uat.properties,prd.properties,文件内容都是
account.source=wqeir231234
*/
1
2
3
4
5
maven自带属性 http://www.xuebuyuan.com/2038385.html

//step 4
//项目的pom.xml中有 主要使用了copy命令-文件全量copy
<profiles>
        <profile>
            <id>local</id>
            <properties>
                <env>local</env>
                <env.overwrite>false</env.overwrite>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
                <env.overwrite>true</env.overwrite>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <env>uat</env>
                <env.overwrite>true</env.overwrite>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
                <env.overwrite>true</env.overwrite>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <!--<phase>generate-sources</phase>-->
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <!--<target >-->
                            <!--<echo message="开始拷贝${env}环境配置文件" />-->
                            <!--</target>-->

                            <target name="env-target" if="${env.overwrite}">
                                <!--<delete file="${basedir}/target/classes/env.properties" verbose="true" deleteonexit="true"/>-->
                                <echo message="开始拷贝${env}环境配置文件:从${basedir}/src/main/filters/${env}.properties 拷贝到 ${basedir}/target/classes/env.properties" />
                                <copy file="${basedir}/src/main/filters/${env}.properties" tofile="${basedir}/target/classes/env.properties" overwrite="true" force="true"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>     
        </plugins>
    </build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
方法一完成

方法二

// step 1 这样一个类
@Service("commonService")
public class CommonService implements org.springframework.beans.factory.InitializingBean{
    @Value("${account.source}")
    private String source;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化完成");
    }
}
1
2
3
4
5
6
7
8
9
10
// step 2 这儿所有变量都使用${变量名}作为占位符,maven在构建的时候替换为真正的值
//src/main/resources目录或子目录下有多个配置文件
//文件一 env.properties,文件内容如下
account.source=${account.source}
/*
文件二 src/main/resources/spring下有applicationContext.xml、applicationContext-shiro.xml、spring-mvc.xml等,其中内容分别是
*/
jdbcUrl=${jdbcUrl}
shiro_loginUrl=${shiroLoginUrl}
cache=${cache}
1
2
3
4
5
6
7
8
9
10
//step 3
/*
src/main/filters 下有3个配置文件dev.properties,uat.properties,product.properties,文件内容都是
account.source=wqeir231234
jdbcUrl=www.baidu.com/mysql
shiro_loginUrl=www.baidu.com/login
cache=www.baidu.com/cache
*/
1
2
3
4
5
6
7
8
//step 4 pom.xml主要内容与方法一的不同
<!-- 在maven构建的时候 选择其中一个<id>xx</id>,不选择则使用默认的,由<activeByDefault>true</activeByDefault>指定 -->
<!-- 使用<properties>可以在pom中随便自定义变量 -->
<profiles>
    <profile>
        <id>dev</id>
        <!-- 自定义变量 env 值为dev -->
        <properties>
            <env>dev</env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>sit</id>
        <!-- 自定义变量 sit 值为sit -->
        <properties>
            <env>sit</env>
        </properties>
    </profile>
    <profile>
        <id>uat</id>
        <!-- 自定义变量 uat 值为uat -->
        <properties>
            <env>uat</env>
        </properties>
    </profile>
    <profile>
        <id>product</id>
        <!-- 自定义变量 product 值为product -->
        <properties>
            <env>product</env>
        </properties>
    </profile>
</profiles>

<build>
    <!-- java源文件 ,标准的maven项目目录包含src/mainjava src/main/resource src/main/filters src/main/test-->
    <sourceDirectory>src/main/java</sourceDirectory>
    <!-- 多环境配置文件 ${env} env是上面自定义的变量 -->
    <!-- 取这一个环境的配置文件作为过滤文件-->
    <filters>
        <filter>src/main/filters/${env}.properties</filter>
    </filters>
    <resources>
        <!-- 定义资源 -->
        <resource>
            <!-- 指定资源所在目录(配置文件,比如spring/springmvc等等)文件目录 -->
            <directory>src/main/resources</directory>
            <!-- 包括配置文件下所有的子目录 -->
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
        <!-- 定义资源二 -->
        <resource>
            <directory>src/main/resources</directory>
            <!-- 这个资源包括所有目录的xml文件和properties文件 -->
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <!-- 对这个资源包含的文件使用上面的过滤文件过滤 把这个${xxx}替换为真实的值 -->
            <filtering>true</filtering>
        </resource>
    </resources>
    <!-- 构建完成文件输出位置-->
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.26</version>
            <configuration>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>80</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
                <webAppConfig>
                    <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
                </webAppConfig>
                <stopPort>9966</stopPort>
                <stopKey>jetty-stop</stopKey>
                <scanIntervalSeconds>10</scanIntervalSeconds>
            </configuration>
        </plugin>
    </plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
完成
eclipse中执行maven构建命令
右击工程,选择”Debug As”或”Run As”,再选择”Maven build”
在Goals中输入compile
Profiles输入dev或sit或product
maven常用命令http://blog.csdn.net/u011939453/article/details/43017865