Maven uses Profile to build deployment packages for different environments

Original address: http://www.cnblogs.com/yjmyzz/p/3941043.html      This article is part of it

 

After the project is developed, it is usually deployed in multiple environments, like our company has as many as 5 environments: local environment ( local ), (self-testing within the development team) development environment ( dev ), (provided to the testing team) Test environment ( test ), pre-release environment ( pre ), and formal production environment ( prod ), each environment has its own configuration parameters, such as database connection, ws address for remote calls, and so on. If you manually modify these parameters before building each environment, it is obviously not fashionable.

 

Maven has long considered these issues, see the following pom snippet:

<profiles>
        <profile>
            <!-- local environment -->
            <id>local</id>
            <properties>                
                <db-url>jdbc:oracle:thin:@localhost:1521:XE</db-url>
                <db-username>***</db-username>
                <db-password>***</db-password>
            </properties>
        </profile>
        <profile>
            <!-- Development Environment-->
            <id>dev</id>
            <properties>                
                <db-url>jdbc:oracle:thin:@172.21.129.51:1521:orcl</db-url>
                <db-username>***</db-username>
                <db-password>***</db-password>
            </properties>
            <!-- This environment is activated by default -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        ...
    </profiles>

 In the profiles node, two environments are defined: local and dev (the dev environment is activated by default). You can add the required property values ​​in the respective environments. Next, modify the build node. Refer to the following example:

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

 The resource node is the key, it indicates which directory the configuration file (whether it is the xml configuration file or the properties property file) needs to replace the property value according to the profile environment. Usually the configuration file is placed in the resources directory, and the files in this directory are automatically copied to the class directory during build.

The content of spring-database.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="${db-url}" />
        <property name="username" value="${db-username}" />
        <property name="password" value="${db-password}" />        
    </bean>
</beans>

The value of each attribute node is occupied by the placeholder " ${attribute name} ". When maven is in the package, it will automatically replace these placeholders with the actual attribute value according to the environment of the profile.

by default: 

maven package

It will be packaged with the default activated profile environment, or you can manually specify the environment, such as:

maven package -P dev

The deployment package that will be automatically packaged into the dev environment (Note: the parameter P is capitalized)

 

The values ​​of these attribute nodes can also be sent to the Java code through the configuration of spring bean. An example is given in the attachment.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040905&siteId=291194637