Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to dynamically select a profile for mavenprofile

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about how to dynamically select configuration files for mavenprofile, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. background

In the process of development, our software will face different running environments, such as development environment, test environment and production environment, while some configurations of our software may be different in different environments. for example, data source configuration, log file configuration, and some basic configurations in the running process of the software, so every time we deploy the software to a different environment, we need to modify the corresponding configuration file. If you modify it back and forth in this way, it is easy to make mistakes and a waste of labor.

Maven provides a convenient solution to this problem, which is the profile function.

II. Brief introduction of profile

Profile allows us to define a series of configuration information and then specify its activation conditions. In this way, we can define multiple profile, and then each profile corresponds to different activation conditions and configuration information, thus achieving the effect that different environments use different configuration information.

Location defined by profile

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

(2) for the profile configuration of a specific user, we can define 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.

Third, configure dynamic packaging

1. Configure profile

Add the following profile configuration to the profile of the project:

Dev dev true release release beta beta

Three environments are defined here, namely, dev (development environment), beta (test environment) and release (release environment), in which the development environment is activated by default (activeByDefault is true), so that if you do not specify profile by default, it will also be displayed when package specifies which development environment you want to choose. For more information, see below.

2. Configuration file

Different configuration files are defined for different environments, and the file directories are as follows:

As shown in the figure, the configuration files for the development environment, test environment, and production environment are placed in the config folder under the src/main/resources directory.

There are configuration files for multiple environments under config, and the naming convention is application- environment name.properties.

Process:

1) Select the environment you want to use through profile

2) inject environment variables into application.properties through the package command (so that those common environment variables do not have to be configured in the configuration files of each environment)

3) load the application.xml file in the project

Load configuration file

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

Part of application.properties is used to inject the above content:

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 of pom.xml, configure the location of the resource file as follows:

Seewo-admin src/main/resources/config/application-$ {env} .properties src/main/resources true org.apache.maven.plugins maven-war-plugin

Note here that a parameter true must be set to true. Only in this way will the original one be overwritten with the configuration file in the corresponding env directory.

4. Activate profile

1) default activation

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

True

2) activate a profile using the-P parameter display

When we do the Maven operation, we can use the-P parameter display to specify which profile is currently active. For example, if we need to use profile with id as dev when packaging the project, we can do this:

Mvn package-Pdev

Assuming that dev is an active profile marked with dev in settings.xml, then 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 those of log4j.

IV. The pit encountered

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

The above is how to dynamically select configuration files for mavenprofile. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report