Maven defines parameter variables for different environments

Source: Internet
Author: User
Application ScenariosWhen we develop, we will encounter the need to distinguish between formal environment, test environment, development environment using different parameters, such as database username and password. You can then use spring's propertyplaceholderconfigurer to configure variables that are affected by the environment, the way you use multiple profiles, but I don't think it's flexible enough, so I use Maven's profiles to do this, When packaged, Maven writes the file according to the specified configuration parameters. Configuration is divided into two types, one is the global configuration that is, in the. M2/conf/setting.xml configuration to achieve global, one is in the project Pom.xml configuration, here I do not use the global approach, not detailed, we mainly introduce the project configuration. Implement 1.pom.xml Configuration
    <build> <!--Configure the configuration file using variables--> <filters> <filter>src/main/resources/a
            Pplication.properties</filter> </filters> <resources> <!--configuration file path--> <resource> <directory>src/main/resources</directory> <filte ring>true</filtering> </resource> </resources> </build> <profile s> <profile> <id>local</id> <properties> <po
            M.env>dev</pom.env> <pom.ver>1.2.4</pom.ver> </properties> <activation> <!----> <activebydefault>true</activebydefau for the default-effective configuration group lt> </activation> </profile> <profile> &LT;ID&GT;DEV&LT;/ID&G
            T <propeRties> <pom.env>dev</pom.env> <pom.ver>1.3.3</pom.ver>
            </properties> </profile> <profile> <id>pro</id> 
            <properties> <pom.env>pro</pom.env> <pom.ver>1.3.5</pom.ver>
 </properties> </profile> </profiles>
2.application.properties Configuration
ENV=${POM.ENV}
Ver=${pom.ver}
3. Implementation

When executed, add-P ${profile.id}, use the default can not add, I use the IDE with the tool to select the effective parameter group, in the profiles select the parameter group

MVN Clean Install-p Local
4. Summary

The steps are as follows: Pom.xml Add, Parameter el Expression in configuration file call variable execution is-p development parameter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.