Maven Profile

What is a MavenProfile

In our ordinary Java development, we often use a lot of configuration files (xxx.properties, XXX.xml), and when we develop locally (dev), test environment (test), and use online production (product), we need to constantly modify these configuration files. Quite troublesome. Using Maven’s Filter and Profile features, we can now switch configurations at compile time by simply specifying a parameter, making it more efficient and less error-prone.

Profiles allow us to define a set of configurations and then specify their activation conditions. This allows us to define multiple profiles, each with different activation conditions and configuration information, to achieve the effect of using different configuration information for different environments.

Introduction to Maven Profile

Modify pom.xml in Pinyougou-page-web

<properties> <port>9105</port> </properties> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> < artifactId > tomcat7 -- maven plugin < / artifactId > < version > 2.2 < / version > < configuration > <! ${port}</port> <! - request path - > < path > / < / path > < / configuration > < / plugin > < / plugins > < / build >Copy the code

Run Tomcat7 :run and find the same result because port is a variable and the value of the variable is defined as 9105. This is actually the maven variable we learned about earlier.

Now let’s think about what if this port is 9105 at development time and 9205 in production (or other environments)? How do you solve the dynamic switching of values?

At this point we modify pom.xml to add a profile definition

  <profiles>
  	<profile>
  		<id>dev</id>
  		<properties>
  			<port>9105</port>
  		</properties>
  	</profile>
  	<profile>
  		<id>pro</id>
  		<properties>
  			<port>9205</port>
  		</properties>
  	</profile>  
  </profiles>
Copy the code

Run tomcat7:run -p pro to discover that port 9205 is started

Tomcat7 :run -p dev Port 9105 is used for discovery

-p is followed by the PROFILE ID

If we just execute tomcat7:run, we will also start with 9105, because the variable value we initially define is 9105, which is the default value without specifying the profileID.

 

Switch database connection configuration

Write configuration files for different environments

< SRC /main/resources > create filter folder in pinyougou-DAO project

Create db_dev.properties in the filter folder to configure the database used by the development environment

env.jdbc.driver=com.mysql.jdbc.Driverenv.jdbc.url=jdbc:mysql://localhost: 3306 /pinyougoudb? characterEncoding=utf-8env.jdbc.username=rootenv.jdbc.password=123456

Create db_pro.properties in the filter folder

env.jdbc.driver=com.mysql.jdbc.Driverenv.jdbc.url=jdbc:mysql://localhost:3306/pinyougoudb_pro? characterEncoding=utf-8env.jdbc.username=rootenv.jdbc.password=123456

(4) Modify db.properties under properties

jdbc.driver=${env.jdbc.driver}jdbc.url=${env.jdbc.url}jdbc.username=${env.jdbc.username}jdbc.password=${env.jdbc.passwor d}

Define the Profile

Modified pom. XML

  <properties>
  		<env>dev</env>
  </properties>
  <profiles>
  	<profile>
  		<id>dev</id>
  		<properties>
  			<env>dev</env>
  		</properties>
  	</profile>    
  	<profile>
  		<id>pro</id>
  		<properties>
  			<env>pro</env>
  		</properties>
  	</profile>
  </profiles>
Copy the code

Here we define two profiles, development environment and production environment

Resource filtering and variable replacement

Modify pom.xml to add the following configuration to the build node

   <filters>   <filter>src/main/resources/filters/db_${env}.properties</filter>   </filters>   <resources>   <resource>   <directory>src/main/resources</directory>   <filtering>true</filtering>   </resource>     </resources>

Maven filter can replace ${key} in resource file with key=value in XXX. Properties. Finally replace username=${key} in your resource file with username=value

packaging

In the Pinyougou-dao project, run the package -p pro command to decompress the generated JAR package and observe that the db.properties configuration file has been replaced with the value in the production environment.

In the Pinyougou-Sellergoodsservice project, run the pageage command to decompress the jar package of Pinyougou-DAO in the generated WAR package and find the value of the generated environment.

A test run

[1] Connect to the production database

(1) In pinyougou-dao project execute the command: install -p pro

(2) Run tomcat7:run in pinyougou-sellergoods-service

(3) In pinyougou-shop-web: execute command: tomcat7:run

[2] Connect to the development database

Install -p dev (or install)

(2) Run tomcat7:run in pinyougou-sellergoods-service

(3) In pinyougou-shop-web: execute command: tomcat7:run

Toggle registry connection configuration

Centrally configure the registry address

Create dubbox.properties under Properties in pinyougou-Common project

Address = 192.168.0.135:2181

Create the Spring configuration file applicationContext-dubbox. XML in the Spring directory as follows:

<dubbo:registry protocol= *“zookeeper” * address= *“${address}”* / >

All service projects and Web projects rely on Pinyougou-common. Delete the registry address configuration from each project

Install Pinyougou-common to the local repository and test run.

MavenProfile configuration

Create the filters directory in the Pinyougou-common project and create dubbox_dev.properties under the directory

Env. Address = 192.168.25.135:2181

Establish dubbox_pro. The properties

Env. Address = 192.168.25.136:2181

Modify dubbox. The properties

address=${env.address}

Modify pom.xml for Pinyougou-common

<properties> <env>dev</env> </properties> <profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>pro</id> <properties> <env>pro</env> </properties> </profile> </profiles> . <build> <filters> <filter>src/main/resources/filters/dubbox_${env}.properties</filter> </filters> <resources> <resource>  <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>Copy the code

Reference data: www.cnblogs.com/0201zcr/p/6…