maven構建靈活性

Maven內置了三大特性:屬性、Profile和資源過濾來支持構建的靈活性。css

maven屬性

事實上有六種類型的Maven屬性:mysql

  • 內置屬性:主要有兩個經常使用內置屬性——${basedir}表示項目根目錄,即包含pom.xml文件的目錄;${version}表示項目版本。
  • POM屬性:pom中對應元素的值。例如${project.artifactId}對應了<project><artifactId>元素的值。具體有哪些POM屬性能夠用,能夠查看本頁末的附件——超級POM
  • 自定義屬性:在pom中<properties>元素下自定義的Maven屬性。例如
<project>  
        <properties>  
            <my.prop>hello</my.prop>  
        </properties>  
    </project>
  • Settings屬性:與POM屬性同理。如${settings.localRepository}指向用戶本地倉庫的地址。
  • Java系統屬性:全部Java系統屬性均可以使用Maven屬性引用,例如${user.home}指向了用戶目錄。能夠經過命令行mvn help:system查看全部的Java系統屬性
  • 環境變量屬性:全部環境變量均可以使用以env.開頭的Maven屬性引用。例如${env.JAVA_HOME}指代了JAVA_HOME環境變量的值。也能夠經過命令行mvn help:system查看全部環境變量。

資源過濾

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>

maven profile

每一個Profile能夠看做是POM的一部分配置,咱們能夠根據不一樣的環境應用不一樣的Profile,從而達到不一樣環境使用不一樣的POM配置的目的。webapp

profile能夠聲明在如下這三個文件中:maven

  • pom.xml:很顯然,這裏聲明的profile只對當前項目有效
  • 用戶settings.xml:.m2/settings.xml中的profile對該用戶的Maven項目有效
  • 全局settings.xml:conf/settings.xml,對本機上全部Maven項目有效

很是值得注意的一點是,profile在pom.xml中可聲明的元素在settings.xml中可聲明的元素是不同的:測試

  • profile在pom.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

有多種激活Profile的方式:

  1. 命令行方式激活,若有兩個profile id爲devx和devy的profile:

    mvn clean install  -Pdevx,devy  

  2. settings文件顯式激活
    <settings>  
            ...  
            <activeProfiles>  
                <activeProfile>devx</activeProfile>  
                <activeProfile>devy</activeProfile>  
            </activeProfiles>  
            ...  
        </settings>
  3. 系統屬性激活,用戶能夠配置當某系統屬性存在或其值等於指望值時激活profile,如:
    <profiles>  
            <profile>  
                <activation>  
                    <property>  
                        <name>actProp</name>  
                        <value>x</value>  
                    </property>  
                </activation>  
            </profile>  
        </profiles>
    不要忘了,能夠在命令行聲明系統屬性。如:
    1. mvn clean install -DactProp=x  
    這其實也是一種從命令行激活profile的方法,並且多個profile徹底可使用同一個系統屬性來激活。別忘了,系統屬性能夠經過mvn help:system來查看操做系統環境激活,
<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  

相關文章
相關標籤/搜索