09 属性、资源过滤和profile

为了构建的灵活性, Maven支持属性、资源过滤和Profile。

一、属性
1.属性的引用
属性的引用很简单,如:${属性名}


2.属性的分类
(1)内置属性:
常用的内置属性有:
${basedir} 表示项目根目录
${version} 表示项目版本


(2)POM属性
用户可使用该类属性引用POM文件中的对应元素的值。如:${project.artifacctId}就对应了<project><artifactId>元素的值。

(3)自定义属性
用户可在<properties>元素下自定义Maven属性,如:

<project>
	...
	<properties>
		<my.prop>hello</my.prop>
	</properties>
	...
</project>

 

(4)Settings属性:
与POM属性类似,用户可使用settings开头的属性引用settings.xml中XML元素的值。如${settings.localRepository}


(5)Java系统属性:
所有Java系统属性都可使用Maven属性来引用,例如${user.home}指向用户目录。可使用mvn help:system 查看所有的Java系统属性。


(6)环境变量属性:
所有环境变量都可使用env开头的Maven属性引用。如:${env.JAVA_HOME}表示JAVA_HOME环境变量的值。用户可使用mvn help:system查看所有的环境变量 。

二、资源过滤
1.资源过滤
  不同的环境中需要的构件可能会有区别,如开发环境、测试环境以及正式环境的数据库配置不同。Maven推荐针对不同的环境生成不同的构件。
为了对应环境的变化,首先要使用Maven属性将这些将会发生变化的部分提取出来。如数据库配置可写成:

database.jdbc.driver = ${db.driver}
database.jdbc.url=${db.url}
database.jdbc.username = ${db.username}
database.jdbc.passwrd = ${db.password}


  这里用到了四个Maven自定义属性。我们可在profile元素中定义这些属性。详见下节。

  默认情况下,Maven只会解析POM.XML中的属性引用。也就是说如果上面的数据库配置放在resources目录下,Maven默认不会解析。
  资源文件的处理实际是maven-resources-plugin负责,它的默认行为只是将项目主资源文件复制到主代码编译输出目录中,将测试资源文件复制到测试代码编译输出目录。可通过配置,使该插件解析资源文件中的Maven属性,即开启资源过滤,如:

<project>
	...
	<build>
		<resources>
			<resource>
				<directory>src/main</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/sql</directory>
				<filtering>false</filtering>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<directory>...</directory>
				<filtering>...</filtering>
			</testResource>
		</testResources>
	</build>
	...
</project>

 

  在运行时指定激活的Profile,即可使用指定的属性引用。如:mvn clean install -Pdev

2.web资源过滤
  在web项目中有两类资源,一类是构建后放置到WEB-INF/classes目录下,另一类则放到war包的根目录,如css文件。第二类资源由maven-war-plugin插件控制。可以配置方式不同,如:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugins</artifactId>
	<version>2.1-beta-1</version>
	<configuration>
		<webResources>
			<resource>
				<filtering>true</filtering>
				<directory>src/main/webapp</directory>
				<includes>
					<include>* */*.css</include>
					<include>* */*.js</include>
				</includes>
			</resource>
		</webResources>
	</configuration>
</plugin>

 

三、Profile
  不同的环境需要的配置可能不相同,为了让构建适合各种不同的环境,Maven引入了Profile概念。


1.定义profile
  可在pom.xml、用户settings.xml、全局settings.xml,profiles.xml(Maven3不支持)中定义profile。如:

<project>
	...
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<db.driver>...</db.driver>
				<db.url>...</db.url>
				<db.username>...</db.username>
				<db.password>...</db.password>
			</properties>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<db.driver>...</db.driver>
				<db.url>...</db.url>
				<db.username>...</db.username>
				<db.password>...</db.password>
			</properties>
		</profile>
	</profiles>
	...
</project>

 

2.激活profile
(1)命令行激活
  使用-P参数,可同时激活多个profile。如:mvn clean install -Pdev-x,dev-y

(2)settings文件显示激活
  如果用户希望某个profile一直处于激活状态,可在settings.xml中使用activeProfiles元素,表示其配置的profile对于所有项目都处于激活状态,如:

<settings>
	...
		<activeProfiles>
			<activeProfile>
				dev-x
			</activeProfile>
		</activeProfiles>
	...
</settings>

 

(3)系统属性激活
  用户可以配置当某个系统属性存在时,自动激活profile,如:

	<activation>
		<property>
			<name>test</name>
		</property>
	</activation>
	<id>dev</id>
	<properties>
		<db.driver>...</db.driver>
		<db.url>...</db.url>
		<db.username>...</db.username>
		<db.password>...</db.password>
	</properties>
</profile>

 

  也可以配置成当某个系统属性存在,并且为指定值时,自动激活profile。如:

<profile>
	<activation>
		<property>
			<name>test</name>
			<value>x</value>
		</property>
	</activation>
	<id>dev</id>
	<properties>
		<db.driver>...</db.driver>
		<db.url>...</db.url>
		<db.username>...</db.username>
		<db.password>...</db.password>
	</properties>
</profile>

 

  系统属性可在命令行中指定,如:mvn clean install -Dtest=x

(4)操作系统环境激活
  profile可根据操作系统环境自动激活。如:

<profile>
	<activation>
		<os>
			<name>Windows XP</name>
			<family>Windows</family>
			<arch>x86</arch>
			<version>5.1.2600</version>
		</os>
	</activation>
	<id>dev</id>
	<properties>
		<db.driver>...</db.driver>
		<db.url>...</db.url>
		<db.username>...</db.username>
		<db.password>...</db.password>
	</properties>
</profile>

 

(5)根据文件存在与否激活
  可根据项目中是否存在某个文件来激活profile,如:

<activation>
	<file>
		<missing>x.properties</missing>
		<exists>y.properties</exists>
	</file>
</activation>

 

(6)默认激活
  可在定义profile时指定为默认激活,如:

<activation>
	<activeByDefault>true</activeByDefault>
</activation>

 

  注意:但有profile通过其他方式激活时,默认激活失效。

猜你喜欢

转载自lxmmt.iteye.com/blog/852040
09