Maven内置了三大特性:属性、Profile和资源过滤来支持构建的灵活性。css
事实上有六种类型的Maven属性:mysql
<project> <properties> <my.prop>hello</my.prop> </properties> </project>
maven的properties filter功能能够帮你自动替换配置文件中以${}包裹的变量。web
为了方便构建不一样的环境,咱们一般将不一样的配置以properties形式配置在pom 中。sql
默认状况下,Maven属性只有在POM中才会被解析。资源过滤就是指让Maven属性在资源文件(src/main/resources、src/test/resources)中也能被解析。数据库
在POM中添加下面的配置即可以开启资源过滤apache
<build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> </build>
Maven除了能够对主资源目录、测试资源目录过滤外,还能对Web项目的资源目录(如css、js目录)进行过滤。这时须要对maven-war-plugin插件进行配置app
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</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能够看做是POM的一部分配置,咱们能够根据不一样的环境应用不一样的Profile,从而达到不一样环境使用不一样的POM配置的目的。webapp
profile能够声明在如下这三个文件中:maven
很是值得注意的一点是,profile在pom.xml中可声明的元素在settings.xml中可声明的元素是不同的:测试
<project> <repositories></repositories> <pluginRepositories></pluginRepositories> <distributionManagement></distributionManagement> <dependencies></dependencies> <dependencyManagement></dependencyManagement> <modules></modules> <properties></properties> <reporting></reporting> <build> <plugins></plugins> <defaultGoal></defaultGoal> <resources></resources> <testResources></testResources> <finalName></finalName> </build> </project>
profile在settings.xml中可声明的元素:
<project> <repositories></repositories> <pluginRepositories></pluginRepositories> <properties></properties> </project>
profile 配置实例:
<project> <modelVersion>4.0.0</modelVersion> <groupId>cc.mzone</groupId> <artifactId>myjar</artifactId> <version>0.1</version> <packaging>jar</packaging> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>*.*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> <properties> <jdbc.url>jdbc:mysql://localhost:3306/abc</jdbc.url> <jdbc.username>root</jdbc.username> <jdbc.password>root</jdbc.password> </properties> <profiles> <profile> <id>product</id> <properties> <jdbc.url>jdbc:mysql://localhost:3306/abc123</jdbc.url> <jdbc.username>rootuser</jdbc.username> <jdbc.password>rootpwd</jdbc.password> </properties> </profile> </profiles> </project>
这里咱们在pom文件中定义了数据库的相关配置,同时定义了一个profile,其id为product,同时在这个profile中也定义了数据库的相关配置。这样咱们使用mvn package命令时就可使用默认的jdbc设置,当咱们使用mvn package -P product时maven就会自动使用id为product的profile中的数据库配置,这个是maven读取属性配置文件的覆盖。
有多种激活Profile的方式:
mvn clean install -Pdevx,devy
<settings> ... <activeProfiles> <activeProfile>devx</activeProfile> <activeProfile>devy</activeProfile> </activeProfiles> ... </settings>
<profiles> <profile> <activation> <property> <name>actProp</name> <value>x</value> </property> </activation> </profile> </profiles>不要忘了,能够在命令行声明系统属性。如:
<profiles> <profile> <activation> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> </profile> </profiles>
这里的family值包括Window、UNIX和Mac等,而其余几项对应系统属性的os.name、os.arch、os.version
5 . 文件存在与否激活
Maven能根据项目中某个文件存在与否来决定是否激活profile
<profiles> <profile> <activation> <file> <missing>x.properties</missing> <exists>y.properties</exists> </file> </activation> </profile> </profiles>
Notice:插件maven-help-plugin提供了一个目标帮助用户了解当前激活的profile:
mvn help:active-profiles
另外还有一个目标来列出当前全部的profile:
mvn help:all-profiles