最大限度利用Maven settings.xml文件
John Ferguson在他的最新Blog中,介紹了使用Maven settings.xml文件的一些鮮為人知的技巧,希望對大家也有所助益。(2009.08.14最后更新)如果你在任何程度中使用過Maven,你應該知道settings.xml文件。settings.xml文件包含有與系統環境相關的配置細節,例如代理配置,倉庫,服務器的用戶名和密碼,等等。
下面是一個典型的settings.xml文件的示例:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:/maven/repository</localRepository>
<proxies>
<proxy>
<id>localproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.acme.com</host>
<port>8080</port>
<username>scott</username>
<password>t0ps3cr3t</password>
<nonProxyHosts>*.acme.com</nonProxyHosts>
</proxy>
</proxies>

<servers>
<server>
<id>dbserver</id>
<username>scott</username>
<password>tiger</password>
</server>
</servers>

</settings>
例如,若你使用公司的環境,在該環境中,你的Maven主目錄是每天早晨當你登錄時由網絡傳輸的,localRepository元素就會非常有用。在這種情況 下,將本地倉庫置于你本地磁盤的不同目錄中,將會節約大量的帶寬。xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:/maven/repository</localRepository>
<proxies>
<proxy>
<id>localproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.acme.com</host>
<port>8080</port>
<username>scott</username>
<password>t0ps3cr3t</password>
<nonProxyHosts>*.acme.com</nonProxyHosts>
</proxy>
</proxies>

<servers>
<server>
<id>dbserver</id>
<username>scott</username>
<password>tiger</password>
</server>
</servers>

</settings>
你也可以在settings.xml文件中定義倉庫,鏡像,概述和屬性。在此處,我不想涉及上述內容,因為在其它地方已經有關于它們的很好的文檔了。
很少為人所知,或至少是很少被使用的功能是,在你的pom.xml文件中使用定義在settings.xml文件中的數據。事實上,你能使用settings.xml中的任一元素,然而其中的一些則更為有用。
一個普遍且方便的例子就是使用localRepository變量。你可能需要將該變量置于一個腳本中,或使用它來引用倉庫中的某個特定的JAR文件(盡管通常有更優雅的方案來解決這一問題)。你可以通過簡單地引用${settings.localRepository}來使用localRepository屬性。例如,在下面的代碼中,我們調用一個Ant腳本,并通過名為"localRepository"的屬性傳給它一個本地倉庫路徑:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-stuff</id>
<phase>pre-comile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant target="generate">
<property name="localRepository" value="${settings.localRepository}"/>
</ant>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
做更多有趣的事情,特別是當你也在構建中集成了Groovy時。例如,假設在集成測試階段期間,我們需要確保特定的SQL腳本已在數據庫中執行過了。我們有一個名為update-scripts.groovy的Groovy腳本來做這件事情,但它需要以命令行參數的形式提供用戶名和密碼。在集成測試階段之前,你應如何使用定義在settings.xml中的用戶名和密碼呢?是的,只需一點兒Groovy魔術,沒有比這兒更簡單的了。settings對象可用于任何集成到pom.xml中的Groovy腳本,所以你可像使用一個普通對象那樣方便地使用它,如下示例:<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-stuff</id>
<phase>pre-comile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant target="generate">
<property name="localRepository" value="${settings.localRepository}"/>
</ant>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-5</version>
<executions>
<execution>
<id>process-db-scripts</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def server = settings.servers.find{ it.id.equals('dbserver') }
"""groovy update-scripts.groovy -Ddb.username=${server.username}
-Ddb.password=${server.password}""".execute()
</source>
</configuration>
</execution>
</executions>
</plugin>
簡單!事實上,一旦你既知道如何訪問頂級變量,又知道如何訪問settings.xml中的各個元素,那么其功效將不可限量!只需記住一定要使你的構建保持彈性--例如,不要在settings.xml文件中定義那些在pom.xml文件中無有效默認值的屬性。<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-5</version>
<executions>
<execution>
<id>process-db-scripts</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def server = settings.servers.find{ it.id.equals('dbserver') }
"""groovy update-scripts.groovy -Ddb.username=${server.username}
-Ddb.password=${server.password}""".execute()
</source>
</configuration>
</execution>
</executions>
</plugin>
如果你想學習更多關于應用Maven的酷炫方法,查看最新的來自于Sonatype的在線課程。或者,為了有一個更具全景的視角,可以參加Java Power Tools bootcamp會議--很快在堪培拉,悉尼,布里斯班和惠靈頓就有研討會了。