一個大型的軟件應(yīng)用通常包含多個模塊,并且通常的場景是多個團(tuán)隊開發(fā)同一應(yīng)用的不同模塊。舉個例子,設(shè)想一個團(tuán)隊開發(fā)應(yīng)用的前端,項(xiàng)目為 app-ui(app-ui.jar:1.0),而另一個團(tuán)隊開發(fā)應(yīng)用的后臺,使用的項(xiàng)目是 data-service(data-service.jar:1.0)。
現(xiàn)在可能出現(xiàn)的情況是開發(fā) data-service 的團(tuán)隊正在進(jìn)行快節(jié)奏的 bug 修復(fù)或者項(xiàng)目改進(jìn),并且他們幾乎每隔一天就要發(fā)布庫到遠(yuǎn)程倉庫。 現(xiàn)在如果 data-service 團(tuán)隊每隔一天上傳一個新版本,那么將會出現(xiàn)下面的問題:
? data-service 團(tuán)隊每次發(fā)布更新的代碼時都要告知 app-ui 團(tuán)隊。
? app-ui 團(tuán)隊需要經(jīng)常地更新他們 pom.xml 文件到最新版本。
為了解決這種情況,快照的概念派上了用場。
快照是一種特殊的版本,指定了某個當(dāng)前的開發(fā)進(jìn)度的副本。不同于常規(guī)的版本,Maven 每次構(gòu)建都會在遠(yuǎn)程倉庫中檢查新的快照。 現(xiàn)在 data-service 團(tuán)隊會每次發(fā)布更新代碼的快照到倉庫中,比如說 data-service:1.0-SNAPSHOT 來替代舊的快照 jar 包。
對于版本,如果 Maven 以前下載過指定的版本文件,比如說 data-service:1.0,Maven 將不會再從倉庫下載新的可用的 1.0 文件。若要下載更新的代碼,data-service 的版本需要升到1.1。
快照的情況下,每次 app-ui 團(tuán)隊構(gòu)建他們的項(xiàng)目時,Maven 將自動獲取最新的快照(data-service:1.0-SNAPSHOT)。
app-ui 項(xiàng)目使用的是 data-service 項(xiàng)目的 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 項(xiàng)目的 pom.xml 文件
data-service 項(xiàng)目為每次小的改動發(fā)布 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 參數(shù)強(qiáng)制 maven 現(xiàn)在最新的快照構(gòu)建。
mvn clean package -U
讓我們打開命令控制臺,去到 C:\ > MVN > app-ui 目錄,然后執(zhí)行下面的 mvn 命令。
C:\MVN\app-ui>mvn clean package -U
Maven 將在下載 data-service 最新的快照之后,開始構(gòu)建項(xiàng)目。
[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] ------------------------------------------------------------------------