Maven 简明教程
Maven - Snapshots
大型软件应用程序通常包含多个模块,在多个团队处理同一应用程序的不同模块时,这种情况很常见。例如,假设有一个团队正在处理应用程序的前端,作为 app-ui 项目(app-ui.jar:1.0),并且他们正在使用 data-service 项目(data-service.jar:1.0)。
A large software application generally consists of multiple modules and it is common scenario where multiple teams are working on different modules of same application. For example, consider a team is working on the front end of the application as app-ui project (app-ui.jar:1.0) and they are using data-service project (data-service.jar:1.0).
现在可能发生的情况是,负责 data-service 的团队正在快速修复 bug 或进行增强,他们几乎每隔一天就会向远程存储库发布库。
Now it may happen that team working on data-service is undergoing bug fixing or enhancements at rapid pace and they are releasing the library to remote repository almost every other day.
现在,如果 data-service 团队每隔一天上传一个新版本,那么将会出现以下问题 −
Now if data-service team uploads a new version every other day, then following problems will arise −
-
data-service team should tell app-ui team every time when they have released an updated code.
-
app-ui team required to update their pom.xml regularly to get the updated version.
为了处理此类情况, SNAPSHOT 的概念便应运而生。
To handle such kind of situation, SNAPSHOT concept comes into play.
What is SNAPSHOT?
SNAPSHOT 是一个特殊版本,它表示当前开发副本。与常规版本不同,Maven 会在远程存储库中为每次构建检查新的 SNAPSHOT 版本。
SNAPSHOT is a special version that indicates a current development copy. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository for every build.
现在,data-service 团队将每次向存储库发布更新的代码的 SNAPSHOT,比如 data-service:1.0-SNAPSHOT,替换旧的 SNAPSHOT jar。
Now data-service team will release SNAPSHOT of its updated code every time to repository, say data-service: 1.0-SNAPSHOT, replacing an older SNAPSHOT jar.
Snapshot vs Version
对于版本,如果 Maven 曾经下载过提到的版本,比如 data-service:1.0,它将永远不会尝试下载存储库中可用的较新的 1.0。要下载更新的代码,data-service 版本应升级到 1.1。
In case of Version, if Maven once downloaded the mentioned version, say data-service:1.0, it will never try to download a newer 1.0 available in repository. To download the updated code, data-service version is be upgraded to 1.1.
对于 SNAPSHOT,Maven 将每次 app-ui 团队构建其项目时自动获取最新的 SNAPSHOT(data-service:1.0-SNAPSHOT)。
In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-service:1.0-SNAPSHOT) every time app-ui team build their project.
app-ui pom.xml
app-ui 项目使用 data-service 1.0-SNAPSHOT。
app-ui project is using 1.0-SNAPSHOT of data-service.
<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-SNAPSHOT。
data-service project is releasing 1.0-SNAPSHOT for every minor change.
<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>
尽管对于 SNAPSHOT,Maven 会在每天自动提取最新的 SNAPSHOT,但你可以通过任何 Maven 命令使用 -U 转换强制 Maven 下载最新的快照构建。
Although, in case of SNAPSHOT, Maven automatically fetches the latest SNAPSHOT on daily basis, you can force maven to download latest snapshot build using -U switch to any maven command.
mvn clean package -U
让我们打开命令控制台,进入 C:\ > MVN > app-ui 目录并执行以下 mvn 命令。
Let’s open the command console, go to the C:\ > MVN > app-ui directory and execute the following mvn command.
C:\MVN\app-ui>mvn clean package -U
Maven 会在下载 data-service 的最新 SNAPSHOT 后开始构建项目。
Maven will start building the project after downloading the latest SNAPSHOT of 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: 2015-09-27T12:30:02+05:30
[INFO] Final Memory: 16M/89M
[INFO]------------------------------------------------------------------------