MAVEN profile Dynamic Selection configuration file

Source: Internet
Author: User

I. BACKGROUND

In the development process, our software will face different operating environments , such as the development environment, test environment, production environment, and our software in different environments, some configuration may not be the same, such as data source configuration, log file configuration, as well as some basic configuration of software running process, Every time we deploy the software to a different environment, we need to modify the corresponding configuration file, so that it can be changed back and forth, is error-prone, and wastes labor.

MAVEN provides a convenient solution to this problem, which is the profile feature.

Ii. profile

Profile allows us to define a series of configuration information and then specify its activation criteria. This allows us to define multiple profiles, and then each profile corresponds to different activation conditions and configuration information to achieve the effect of using different configuration information in different environments.

Location of profile Definition

(1) The profile configuration for a particular project can be defined in the pom.xml of the project . (for example, this is the way)

(2) for a specific user profile configuration , we can define the profile in the user's settings.xml file. The file is under the ". M2" directory in the user's home directory.

(3) Global profile configuration . The global profile is defined in the "Conf/settings.xml" file in the Maven installation directory.

Third, configure dynamic packaging

1. Configure profile

Add the following profile configuration to the project's profiles:

<Profiles>    < Profile>        <!--Local development environment -        <ID>Dev</ID>        <Properties>            <profiles.active>Dev</profiles.active>        </Properties>        <activation>            <!--set the default activation for this configuration -            <Activebydefault>True</Activebydefault>        </activation>    </ Profile>    < Profile>        <!--Publishing Environment -        <ID>Release</ID>        <Properties>            <profiles.active>Release</profiles.active>        </Properties>    </ Profile>    < Profile>        <!--test Environment -        <ID>Beta</ID>        <Properties>            <profiles.active>Beta</profiles.active>        </Properties>    </ Profile></Profiles>

There are three environments defined, namely dev (development environment), Beta (test environment), release (release environment), where the development environment is activated by default (Activebydefault is true), so that if the profile is not specified, the default is the development environment. Also in the package when the display specifies which development environment you want to select, see the details later.

2. Configuration files

For different environments, we have defined different configuration files, and the file directories are as follows:

, the configuration files for the development environment, test environment, and production environment are placed under the Config folder in the Src/main/resources directory, respectively.

Config file with more than one environment, the naming convention is the application-environment name. Properties.

Processing process:

1) Select the environment you want to use by profile

2) through the Package command, the environment variables are injected into the application.properties (this way, those common environment variables are not configured in each environment's configuration file)

3) Load Application.xml file in Project

Load configuration file

<location= "Classpath:application.properties"/>

Example:

The Application-beta.properties file has the following elements:

Env.datasource.jdbcurl=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=UTF-8 &rewritebatchedstatements=true&autoreconnect=true&zerodatetimebehavior =roundenv.datasource.username=rootenv.datasource.password=12233

Application.properties has some of the following content to inject:

datasource.jdbcurl=${env.datasource.jdbcurl}datasource.username=${env.datasource.username}datasource.password= ${env.datasource.password}

3. MAVEN resource plug-in configuration

Under the Build node for Pom.xml, configure the location of the resource file as follows:

<Build>        <Finalname>Seewo-admin</Finalname>        <!--defines the address of the variable configuration file -        <Filters>            <Filter>Src/main/resources/config/application-${env}.properties</Filter>        </Filters>        <Resources>            <Resource>                <Directory>Src/main/resources</Directory>                <filtering>True</filtering>            </Resource>        </Resources>        <Plugins>            <plugin>                <groupId>Org.apache.maven.plugins</groupId>                <Artifactid>Maven-war-plugin</Artifactid>            </plugin>        </Plugins>    </Build>

Note here that a parameter <filtering>true</filtering> must be set to true. This will overwrite the original with the configuration file in the corresponding ENV directory.

4. Activate profile

1) The default activation

The default activation environment is set in the profile configuration above. As shown below

< Activebydefault >true</activebydefault>

2) Activate a profile using the-p parameter display

When we do the MAVEN operation, we can use the-p parameter to specify which profile is currently active. For example, we need to use the ID dev profile when we package the project, and we can do this:

MVN Package–pdev

This assumes that Dev is the active profile using the dev tag in settings.xml, so when we use "-p!profile" it means that the profile will not be active in the current operation.

5. Call

Private Logger Logger = Logmanager.getlogger (MyApp.  Class. GetName ());

The other uses are the same as the log4j.

Iv. the pits encountered

The @ keyword does not appear in the Application.xml file, even if you annotate it. When the @ is present, all subsequent environment variables will not be injected

Such as:

Thanks: Thank you for reading!

MAVEN profile Dynamic Selection configuration file

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.