一個大型的軟件應用通常包含多個模塊,并且通常的場景是多個團隊開發同一應用的不同模塊。舉個例子,設想一個團隊開發應用的前端,項目為 app-ui(app-ui.jar:1.0),而另一個團隊開發應用的后臺,使用的項目是 data-service(data-service.jar:1.0)。
現在可能出現的情況是開發 data-service 的團隊正在進行快節奏的 bug 修復或者項目改進,并且他們幾乎每隔一天就要發布庫到遠程倉庫。 現在如果 data-service 團隊每隔一天上傳一個新版本,那么將會出現下面的問題:
? data-service 團隊每次發布更新的代碼時都要告知 app-ui 團隊。
? app-ui 團隊需要經常地更新他們 pom.xml 文件到最新版本。
為了解決這種情況,快照的概念派上了用場。
快照是一種特殊的版本,指定了某個當前的開發進度的副本。不同于常規的版本,Maven 每次構建都會在遠程倉庫中檢查新的快照。 現在 data-service 團隊會每次發布更新代碼的快照到倉庫中,比如說 data-service:1.0-SNAPSHOT 來替代舊的快照 jar 包。
對于版本,如果 Maven 以前下載過指定的版本文件,比如說 data-service:1.0,Maven 將不會再從倉庫下載新的可用的 1.0 文件。若要下載更新的代碼,data-service 的版本需要升到1.1。
快照的情況下,每次 app-ui 團隊構建他們的項目時,Maven 將自動獲取最新的快照(data-service:1.0-SNAPSHOT)。
app-ui 項目使用的是 data-service 項目的 1.0 快照。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>app-ui</groupId>
<artifactId>app-ui</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>health</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>data-service</groupId>
<artifactId>data-service</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
⒉ data-service 項目的 pom.xml 文件
data-service 項目為每次小的改動發布 1.0 快照。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>data-service</groupId>
<artifactId>data-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>health</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
雖然,快照的情況下,Maven 在日常工作中會自動獲取最新的快照, 你也可以在任何 maven 命令中使用 -U 參數強制 maven 現在最新的快照構建。
mvn clean package -U
讓我們打開命令控制臺,去到 C:\ > MVN > app-ui 目錄,然后執行下面的 mvn 命令。
C:\MVN\app-ui>mvn clean package -U
Maven 將在下載 data-service 最新的快照之后,開始構建項目。
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------
[INFO] Building consumerBanking
[INFO] task-segment: [clean, package]
[INFO] -------------------------------------------------------------------
[INFO] Downloading data-service:1.0-SNAPSHOT
[INFO] 290K downloaded.
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory C:\MVN\app-ui\target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\app-ui\src\main\
resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to C:\MVN\app-ui\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\app-ui\src\test\
resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to C:\MVN\app-ui\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\app-ui\target\
surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\app-ui\target\
app-ui-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Jul 10 16:52:18 IST 2012
[INFO] Final Memory: 16M/89M
[INFO] ------------------------------------------------------------------------