Maven profile dynamically selects the configuration file, mavenprofile

Source: Internet
Author: User

Maven profile dynamically selects the configuration file, mavenprofile

I. background

During the development process, our software will faceDifferent runtime EnvironmentsFor example, in the development environment, test environment, and production environment, some configurations of our software may be different in different environments, for example, data source configuration, log file configuration, and some basic configurations during software running, each time we deploy the software in different environments, we need to modify the corresponding configuration file, in this way, it is easy to make mistakes and waste labor.

Maven provides a convenient solution to solve this problem, namely the profile function.

Ii. profile Introduction

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

Location defined by profile

(1) The profile configuration for a specific project can be defined inProjectIn pom. xml. (The following is an example)

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

(3) global profile configuration. The global profile is defined in the conf/settings. xml file under the Maven installation directory.

3. dynamic configuration Packaging

1. Configure profile

Add the following profile configuration to the Project profile:

<Profiles> <profile> <! -- Local development environment --> <id> dev </id> <properties> <profiles. active> dev </profiles. active> </properties> <activation> <! -- Set to activate this configuration by default --> <activeByDefault> true </activeByDefault> </activation> </profile> <! -- Release environment --> <id> release </id> <properties> <profiles. active> release </profiles. active> </properties> </profile> <! -- Test environment --> <id> beta </id> <properties> <profiles. active> beta </profiles. active> </properties> </profile> </profiles>

Three environments are defined here, namely dev (Development Environment), beta (test environment), and release (release Environment). The development environment is activated by default (activeByDefault is true ), in this way, if no profile is specified, the development environment is used by default, and the development environment to be selected is displayed during package. For details, see the following section.

2. Configuration File

For different environments, we define different configuration files. The file directory is as follows:

 

, The configuration files of the development environment, test environment, and production environment are put under the config folder under the src/main/resources Directory respectively.

Config contains configuration files for multiple environments. The naming rule is application-Environment name. properties.

Processing Process:

1) use profile to select the environment you want to use

2) use the package command to inject environment variables into application. properties (in this way, public environment variables do not need to be configured in the configuration files of each environment)

3) load the application. xml file in the project

Load the configuration file

<context:property-placeholder location="classpath:application.properties"/>

Example:

The application-beta.properties file has the following parts:

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 the following content used to inject the above content:

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

3. Configure maven resource plug-in

Under the build node of 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>

HereNote that one parameter<filtering>true</filtering>Must be set to true.In this way, the original configuration file under the corresponding env directory will be overwritten.

4. Activate profile

1) default Activation

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

<activeByDefault>true</activeByDefault> 

2) use the-P parameter to display the activation of a profile

When performing Maven operations, you can use the-P parameter to specify which profile is currently activated. For example, we need to use the dev profile when packaging the project. We can do this:

mvn package –Pdev

Assume that dev is an active profile marked with dev in settings. xml. When we use "-P! Profile indicates that the profile is not activated in the current operation.

5. Call

private Logger logger = LogManager.getLogger(MyApp.class.getName());

Other applications are the same as those of log4j.

Iv. pitfalls encountered

The @ keyword cannot appear in the application. xml file, even if you comment it. When @ appears, all environment variables will not be injected.

For example:

 

Thank you!

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.