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