Rely on maven to read the dev test prod environment configuration according to the profile configuration in the pom.xml file

http://blog.csdn.net/mayi92/article/details/77892809

quote


To explain, many projects couple the configuration file with the project, such as various private configuration information with third parties are coupled with the project, what results are caused, any developer of the project can know the various aspects of the production environment. Configuration, and developers usually copy the project on their own hard disk after leaving, various private configuration information is easily leaked.

A good architecture will decouple the configuration file from the project, and the configuration file will be maintained by different personnel (the development environment is maintained by developers, and the test and production are maintained by operation and maintenance). The production must be operated and read by operation and maintenance professionals. Pick. There are many ways to do it, isn't it just to read the configuration file at the specified location. You can do this, a plug-in config that manages configuration files, config provides api to the outside world, api uses @Component, a configuration file defines a javabean, when the project is started and initialized, xml is converted to java, and other places in the project want to read the configuration and refer to it config plugin. The configuration file can be placed anywhere, remote or local, possibly in a location /var/projectname/config/ in the root directory where the server (tomcat) is located. This

is a maven project. Use spring's @value to inject properties, and the value specified by @value is in env.properties. Method 1: Full replacement of property configuration files, using maven's copy command It is recommended to look at method 2 // step 1 Such a class @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("initialization completed");
    }
}
1
2
3
4
5
6
7
8
9
10
// step 2
// under src/main/resources There is a configuration file env.properties, the content of the file is as follows
account.source=123456
1
2
3
//step 3
/*
There are 3 configuration files dev.properties, uat.properties, prd.properties, file content under src/main/filters both
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
Method 1 complete

Method 2

// step 1 Such a class
@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("initialization completed");
    }
}
1
2
3
4
5
6
7
8
9
10
// step 2 All variables here use ${variable name} as placeholders, and maven replaces them with real values ​​at build time
//src/main/resources directory or subdirectory There are multiple configuration files
//File 1 env.properties, the content of the file is as follows
account.source=${account.source}
/*
File 2 src/main/resources/spring contains applicationContext.xml, applicationContext-shiro.xml, spring -mvc.xml, etc., where the content is
*/
jdbcUrl=${jdbcUrl}
shiro_loginUrl=${shiroLoginUrl}
cache=${cache}
1
2
3
4
5
6
7
8
9
10
//step 3
/*
There are 3 configuration files dev.properties, uat.properties, product.properties under src/main/filters, the contents of the files are
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 The main content of pom.xml is different from method 1
<!-- Select one of the <id>xx< /id>, if not selected, use the default, specified by <activeByDefault>true</activeByDefault>-->
<!-- Use <properties> to customize variables in pom-->
<profiles>
    <profile>
        < id>dev</id>
        <!-- Custom variable env value is 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>         <!-- Custom variable product value is product -->
        <id>product</id>

        <properties>
            <env>product</env>
        </properties>
    </profile>
</profiles>

<build>
    <!-- java source files, the standard maven project directory contains src/mainjava src/main/resource src/ main/filters src/main/test-->
    <sourceDirectory>src/main/java</sourceDirectory>
    <!-- multi-environment configuration file ${env} env is the variable defined above -->
    <!-- take The configuration file for this environment is used as a filter file -->
    <filters>
        <filter>src/main/filters/${env}.properties</filter>
    </filters>
    <resources>
        <!-- define resources-->
        <resource>
            <!-- Specify the directory where the resource is located (configuration file, Such as spring/springmvc, etc.) file directory -->
            <directory>src/main/resources</directory>
            <!-- including all subdirectories under the configuration file-->
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
        < !-- Define resource two-->
        <resource>
            <directory>src/main/resources</directory>
            <!-- This resource includes xml files and properties files of all directories-->
            <includes>
                <include>** /*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <!-- Use the above filter file filter to replace this ${xxx} with true for the files included in this resource value -->
            <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
Complete
the execution of the maven build command in eclipse
Right-click the project, select "Debug As" or "Run As", and then select "Maven build"
Enter compile Profiles in Goals
Enter dev or sit or product
maven common commands http://blog.csdn.net/u011939453/article/details/43017865


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326134084&siteId=291194637