标签:maven
maven移植-级别广义可移植
Maven移植-Profiles作用是:Profile允许你为移植或特殊的需要,自定义一个特殊的构建。
1、maven移植-Profiles位于pom.xml
Profile可以覆盖几乎所有的pom元素
<profile> <build> <defaultGoal>...</defaultGoal> <finalName>...</finalName> <resources>...</resources> <testResources>...</testResources> <plugins>...</plugins> </build> <reporting>...</reporting> <modules>...</modules> <dependencies>...</dependencies> <dependencyManagement>...</dependencyManagement> <distributionManagement>...</distributionManagement> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <properties>...<properties> </profile>
2、maven移植-Profiles位于profiles.xml
Profile抽取至profiles.xml文件中,将这种profile称为外部profile。注:maven3以后不再支持
3、maven移植-Profiles位于setting.xml
在setting.xml文件中配置profiles,会在所有的项目上生效。
4、maven移植-Profiles(激活方式)
a、通过调用id激活:mvn install -Pprofile-id
b、通过<activation>条件匹配激活
<activation> <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> <os> <name>Windows XP</name> <family>Windows></family> <arch>x86</arch> <version>5.1.2600</version> </os> <file> <exists>file2.properties</exists> <missing>file1.properties</missing> </file> </activation>
<activation> <activeByDefault>false</activeByDefault> <property> <name>mavenVersion</name> <value>2.0.5</value> </property> <!-- 是不是不存在这个环境变量的类型 --> <property> <name>!environment.type</name> </property> </activation>
<profile> <activation> <!-- 默认激活 --> <activeByDefault>true</activeByDefault> </activation> </profile>
<settings> <activeProfiles> <activeProfile>profile-id</activeProfile> </activeProfiles> </settings>
------------------------------------------------------------------------------------------------------------------
************************************************************************************************************
------------------------------------------------------------------------------------------------------------------
实例:
需求:如何做到对所有的项目,开发环境和生产环境的数据库配置连接不同?
下面是原pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.maven</groupId>
<artifactId>test1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>runTest</id>
<!-- 激活profile -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- 下面的build是覆盖上面的build从而达到控制是否跳过测试 -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
把上面的build标签中加入resources标签,制定路径以及是否过滤。
<build> <!-- src/main/resources路径下的进行属性过滤 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.1</version> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build>
项目结构图:
db.properties内容
driverClassName=${jdbc.driverClassName}
url=${jdbc.url}
username=${jdbc.username}
password=${jdbc.password}
property1=${property1}
<!-- 在setting.xml中配置environment.type,为dev时激活oracle的配置,为prod时激活mysql的配置 --> <profile> <id>development</id> <activation> <property> <name>environment.type</name> <value>dev</value> </property> </activation> <properties> <jdbc.driverClassName>oracle.jdbc.driver.OracleDriver</jdbc.driverClassName> <jdbc.url>jdbc:oracle:thin:@proddb01:1521:DEV</jdbc.url> <jdbc.username>dev_user</jdbc.username> <jdbc.password>devpass</jdbc.password> </properties> </profile> <profile> <id>production</id> <activation> <property> <name>environment.type</name> <value>prod</value> </property> </activation> <properties> <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> <jdbc.url>jdbc:mysql://localhost:3306/production_db</jdbc.url> <jdbc.username>prod_user</jdbc.username> <jdbc.password>prodpass</jdbc.password> </properties> </profile>
在settings.xml中的profiles中加入:
<profile> <id>environment</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment.type>dev</environment.type> <property1>2</property1> </properties> </profile> <profile> <id>product</id> <activation> <property> <name>environment.type</name> <value>prod</value> </property> </activation> <properties> <database.password>sdfsdf</database.password> <property1>3</property1> </properties> </profile>
执行mvn clean install,打开target下的classes中的db.properties
可以看到db.properties被改变了。
如果生产环境中的数据库连接、密码等信息不希望别人看到,我们可以在settings.xml中配置,运行后会覆盖pom.xml中的配置。我们可以在pom.xml中定义一些假的数据。
把settings.xml中生产环境下的properties改变了,如下:
<profile> <id>environment</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment.type>dev</environment.type> <property1>2</property1> </properties> </profile> <profile> <id>product</id> <activation> <property> <name>environment.type</name> <value>prod</value> </property> </activation> <properties> <jdbc.driverClassName>update_className</jdbc.driverClassName> <jdbc.url>update_url</jdbc.url> <jdbc.username>update_username</jdbc.username> <jdbc.password>update_password</jdbc.password> <property1>3</property1> </properties>
标签:maven
原文地址:http://blog.csdn.net/benjamin_whx/article/details/42778695