[Turn] use maven of resources, filter and profile to achieve different environments using different profiles

https://www.cnblogs.com/wangyang108/p/6030735.html

The basic concepts described (resources, filter and profile): 
1.profiles defines various environment variables ID 
2.filters address of the variable defined in the configuration file, wherein the address is in the environment variables above defined profile value 
3.resources in What is the definition files in the directory will be replaced with variables defined in the configuration file, we will project the general configuration file in the src / main / resources, like db, bean and other variables which will be used during packaging according to the variable filter configured to replace a fixed value 


in our usual java development, will often use a lot of configuration files (xxx.properties, xxx.xml), and when our local development (dev), test environment test (test ), when the production line using a (product), need to keep to modify these configuration files, often more than one, very troublesome. Now, with the maven of the filter and the profile function, we can achieve simply specify a parameter at compile time can be switched formulated to improve efficiency, not prone to error, explain below. 

First, the principle: 

    the use of filters to filter realize resource file (Resouces) 

Maven filter available xxx.properties specified key = value corresponding to the resource file $ {key} to be replaced, the final file resource your username = $ {key} replace username = value 

    using the profile switched environment 

Maven profile can use the operating system information, whether jdk information file exists, attribute values, etc. as a basis, to activate the corresponding profile, but also in the compilation phase, by mvn -PprofileId command with parameters corresponding to the use to manually activate profile 
Combined filter and profile, we can easily use in different environments different formulation 

II preparation: 
Add 3 configuration file in the root directory of the project: 

    config-dev.properties - when developing with 
    config-test.properties - with test 
    config-product.properties - with production 

Add pom file in the root directory of the project in the following settings: 

Copy the code

<build>
<resources>
    <!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
    </resource>
    <!-- 设置对auto-config.properties,jdbc.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>auto-config.properties</include>
            <include>jdbc.properties</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>
</build>

<profiles>
<profile>
    <id>dev</id>

    <!-- 默认激活开发配制,使用config-dev.properties来替换设置过虑的资源文件中的${key} -->
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <build>
        <filters>
            <filter>config-dev.properties</filter>
        </filters>
    </build>
</profile>
<profile>
    <id>test</id>
    <build>
        <filters>
            <filter>config-dev.properties</filter>
        </filters>
    </build>
</profile>
<profile>
    <id>product</id>
    <build>
        <filters>
            <filter>config-product.properties</filter>
        </filters>
    </build>
</profile>
</profiles> 

Copy the code

 Third, use: 

    development environment: 

filter replacement is misplaced in the implementation of the compile stage maven, so long as the trigger action can be compiled, if the normal use of the same as before the replacement is not found, then manually clean it works (eclipse -> Project -> Clean) [here you should install the maven plugin, maven because the replacement is done, do not eclipse, so clean here should be triggered compile maven's], and then on the Tomcat also right -> Clean what you can, then you go to the tomcat directory under $ {key} will find the value of config-xx resource file inside your project is replaced by the corresponding 
If the above does not work, then use maven maven compiler plugin or play a command console manual it 
because the pom.xml provided dev is activated by default, so the default config-dev will be used to replace $ {key} 

    test environment 

manual translation, packaging: maven clean install -Ptest - activation id = "test" of profile 

    production environment 

manually compile, package: maven clean install -Pproduct - activation id = "product" of the profile 

Guess you like

Origin blog.csdn.net/kingdelee/article/details/85489716