Use Maven property variables and configuration files

Source: Internet
Author: User

MVN archetype: generate

1 <profiles> 2 <profile> 3 <! -- Local environment --> 4 <ID> Local </ID> 5 <Properties> 6 <DB-URL> JDBC: oracle: thin: @ localhost: 1521: xe </DB-URL> 7 <DB-username> *** </DB-username> 8 <DB-Password> *** </DB-Password> 9 </ properties> 10 </profile> 11 <profile> 12 <! -- Development Environment --> 13 <ID> Dev </ID> 14 <Properties> 15 <DB-URL> JDBC: oracle: thin: @ 172.21.129.51: 1521: orcl </DB-URL> 16 <DB-username> *** </DB-username> 17 <DB-Password> *** </DB-Password> 18 </ properties> 19 <! -- The current environment is activated by default --> 20 <activation> 21 <activebydefault> true </activebydefault> 22 </activation> 23 </profile> 24... 25 </profiles>

In the profiles node, two environments are defined: local and Dev (the dev environment is activated by default). You can add the required property values to the environment, and then modify the build node, see the following example:

 1 <build> 2         <resources> 3             <resource> 4                 <directory>src/main/resources</directory> 5                 <filtering>true</filtering> 6             </resource> 7         </resources> 8         <plugins> 9             <plugin>10                 <groupId>org.apache.maven.plugins</groupId>11                 <artifactId>maven-compiler-plugin</artifactId>12                 <version>2.5.1</version>13                 <configuration>14                     <source>1.6</source>15                     <target>1.6</target>16                     <encoding>utf-8</encoding>17                 </configuration>18             </plugin>19         </plugins>20     </build>

The resource node is the key. It indicates the configuration file (whether the xml configuration file or the properties attribute file) under the directory, and the attribute value needs to be replaced according to the profile environment.

Usually, the configuration file is placed under the resources directory. During build, all files under this directory are automatically copied to the class directory.

Example:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />        <property name="url" value="${db-url}" />        <property name="username" value="${db-username}" />        <property name="password" value="${db-password}" />            </bean></beans>

The value of each attribute node is Placeholder "$ {attribute name}". In Maven package, these Placeholders are automatically replaced with actual attribute values according to the profile environment.

By default:

Maven package

The default activated profile environment is used for packaging. You can also manually specify the environment, for example:

Maven package-P Dev

Automatically package the deployment package into the dev environment (Note: The parameter P is capitalized)

Use Maven property variables and configuration files

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.