先行一步
VMware 提供培訓和認證,助你加速進步。
瞭解更多這裡有一位最近評論者抱怨說,“只有一半的世界在使用 Maven”,同時指出使用 Maven 獲取 Spring 3 artifacts 並非顯而易見。在這篇文章中,我將向你展示如何做到這一點以及有哪些選項。這些資訊也將整合到即將釋出的 Spring 3 最終版本的參考文件中。
一般來說,Spring 將其 artifacts 釋出到兩個不同的地方
因此,在使用 Maven 獲取 Spring 時,你需要決定的第一件事是選擇從哪裡獲取。一般來說,如果你關心 OSGi,請使用 EBR,因為它包含 Spring 所有依賴項(如 Hibernate 和 Freemarker)的 OSGi 相容 artifacts。如果你不關心 OSGi,兩個地方都可以,儘管它們之間有一些優缺點。一般來說,為你的專案選擇其中一個地方;不要混合使用。這尤其重要,因為 EBR artifacts 使用的命名約定與 Maven Central artifacts 不同。
下面是一個表格,比較了 Maven Central 和 EBR 在幾個方面的區別
特性 | Maven Central | 企業 Bundle 倉庫 (EBR) |
---|---|---|
OSGi 相容 | 否 | 是 |
Artifact 數量 | 數萬個;各種型別 | 數百個;Spring 整合/支援的那些 |
Artifact 命名約定一致性? | 否 | 是 |
Artifact 命名約定 | Group id 可變;較新的 artifacts 使用域名,例如 "org.sl4j";較舊的 artifacts 使用 artifact id,例如 "log4j" Artifact id 可變;通常是 JAR 檔名去掉副檔名,例如 "log4j" Version 可變;大多數使用數字和點,例如 "3.0.0" | Group id <域名>,例如 "org.springframework" Artifact id <Bundle-SymbolicName>,派生自主包,例如 "org.springframework.beans"。如果 JAR 檔案需要打補丁以確保 OSGi 相容性,會預置 "com.springsource.",例如 "com.springsource.org.apache.log4j" Version OSGi 版本號格式為 <主要版本>.<次要版本>.<微版本>[.限定符],例如 "3.0.0.RC3" |
釋出 | 自動(透過遠端倉庫進行 rSync) | 手動(由 SpringSource 處理 JIRA) |
質量保證 | 據我所知沒有;準確性是釋出組織的責任 | 廣泛(針對 MANIFEST.mf 和 .pom);QA 由 Spring 團隊執行 |
託管 | 由 Sonatype 資助、Contegix 託管,有多個映象 | 由 SpringSource 資助、託管在 S3 |
搜尋工具 | 多種 | www.springsource.com/repository |
與 SpringSource 工具整合(STS, Roo 等) | 是,與 STS 和 Roo | 是,與 STS |
現在你瞭解了這些選項,我將討論如何從這兩個地方獲取 Spring artifacts。
要從 Maven Central 獲取 Spring 專案的最終釋出版本,你無需在 .pom 中新增倉庫。只需新增你的專案所需的依賴項即可。
Spring Framework 3 中每個 artifact 在 Maven Central 中索引時的 .pom <dependency> 片段如下所示。
<!-- Shared version number properties -->
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<!--
Core utilities used by other modules.
Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Expression Language (depends on spring-core)
Define this if you use Spring Expression APIs (org.springframework.expression.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Bean Factory and JavaBeans utilities (depends on spring-core)
Define this if you use Spring Bean APIs (org.springframework.beans.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans)
Define this if you use Spring AOP APIs (org.springframework.aop.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans)
This is the central artifact for Spring's Dependency Injection Container and is generally always defined
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration
Define this if you need any of these integrations
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context)
Define this if you use Spring Transactions or DAO Exception Hierarchy
(org.springframework.transaction.*/org.springframework.dao.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
(depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you need ORM (org.springframework.orm.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans.
(depends on spring-core, spring-beans, spring-context)
Define this if you need OXM (org.springframework.oxm.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Web application development utilities applicable to both Servlet and Portlet Environments
(depends on spring-core, spring-beans, spring-context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Support for testing Spring applications with tools such as JUnit and TestNG
This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
要從 EBR 獲取 Spring 專案的最終釋出版本,請將以下倉庫新增到你的 .pom 中
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>EBR Spring Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/release</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>EBR External Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/external</url>
</repository>
然後只需新增你的專案所需的依賴項,同時記住 EBR artifact 的命名約定。
Spring Framework 3 中每個 artifact 在 EBR 中索引時的 .pom <dependency> 片段如下所示
<!-- Shared version number properties -->
<properties>
<org.springframework.version>3.0.0.RELEASE</org.springframework.version>
</properties>
<!--
Core utilities used by other modules.
Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Expression Language (depends on core)
Define this if you use Spring Expression APIs (org.springframework.expression.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Bean Factory and JavaBeans utilities (depends on core)
Define this if you use Spring Bean APIs (org.springframework.beans.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Aspect Oriented Programming (AOP) Framework (depends on core, beans)
Define this if you use Spring AOP APIs (org.springframework.aop.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Application Context (depends on core, expression, aop, beans)
This is the central artifact for Spring's Dependency Injection Container and is generally always defined
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration
Define this if you need any of these integrations
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context.support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Transaction Management Abstraction (depends on core, beans, aop, context)
Define this if you use Spring Transactions or DAO Exception Hierarchy
(org.springframework.transaction.*/org.springframework.dao.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
JDBC Data Access Library (depends on core, beans, context, transaction)
Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
(depends on core, beans, context, transaction)
Define this if you need ORM (org.springframework.orm.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans.
(depends on core, beans, context)
Define this if you need OXM (org.springframework.oxm.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Web app development utilities common across Servlet/Portlet environments (depends on core, beans, context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Spring MVC for Servlet Environments (depends on core, beans, context, web)
Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Spring MVC for Portlet Environments (depends on core, beans, context, web)
Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.portlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--
Support for testing Spring applications with tools such as JUnit and TestNG
This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
里程碑版本和釋出候選版本可能不會直接釋出到 Maven Central,通常與最終釋出版本分開發布。SpringSource 託管著兩個用於獲取 Spring 里程碑版本的倉庫。第一個應與 Maven Central 結合使用,第二個應與 EBR 結合使用。
要從相容 Maven Central 的倉庫獲取 Spring 里程碑版本,請將以下倉庫新增到你的 .pom 中
<repository>
<id>org.springframework.maven.milestone</id>
<name>Maven Central Compatible Spring Milestone Repository</name>
<url>http:// maven.springframework.org/milestone</url>
</repository>
里程碑版本號格式為 <主要版本>.<次要版本>.<微版本>.M#;例如 3.0.0.M4。釋出候選版本號格式為 <主要版本>.<次要版本>.<微版本>.RC#;例如 3.0.0.RC3。
例如,新增以下依賴項將獲取 spring-context artifact 的 3.0.0.RC3 版本
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RC3</version>
</dependency>
要從 EBR 獲取 Spring 里程碑版本,請將以下倉庫新增到你的 .pom 中
<repository>
<id>com.springsource.repository.bundles.milestone</id>
<name>EBR Spring Milestone Repository</name>
<url>http:// repository.springsource.com/maven/bundles/milestone</url>
</repository>
務必記住獨特的 EBR artifact 命名約定。例如,新增以下依賴項將獲取 org.springframework.context artifact 的 3.0.0.RC3 版本
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.0.0.RC3</version>
</dependency>
Spring 專案的快照版本每晚釋出,使用者可以在下一次釋出之前驗證報告的問題是否已解決。與里程碑版本類似,有一個單獨的相容 Maven Central 的快照倉庫和一個 EBR 快照倉庫。
要從相容 Maven Central 的倉庫獲取 Spring 每夜快照版本,請將以下倉庫新增到你的 .pom 中
<repository>
<id>org.springframework.maven.snapshot</id>
<name>Maven Central Compatible Spring Snapshot Repository</name>
<url>http:// maven.springframework.org/snapshot</url>
</repository>
快照版本格式為 <主要版本>.<次要版本>.<微版本>.BUILD-SNAPSHOT;例如 3.0.1.BUILD-SNAPSHOT。
例如,新增以下依賴項將獲取 spring-context artifact 的最新快照版本
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.1.BUILD-SNAPSHOT</version>
</dependency>
請注意,<主要版本>.<次要版本>.<微版本>.BUILD-SNAPSHOT 格式與傳統的 Maven 2 快照格式 <主要版本>.<次要版本>.<微版本>-SNAPSHOT 略有不同。這是因為 x.y.z-SNAPSHOT 不是有效的 OSGi 版本號。所有 Spring 專案現在都遵循OSGi 版本號方案(Maven 3 也將如此)。
要從 EBR 獲取 Spring 每夜快照版本,請將以下倉庫新增到你的 .pom 中
<repository>
<id>com.springsource.repository.bundles.snapshot</id>
<name>EBR Spring Snapshot Repository</name>
<url>http:// repository.springsource.com/maven/bundles/snapshot</url>
</repository>
作為最後一個例子,新增以下依賴項將獲取 org.springframework.context artifact 的最新快照版本
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.0.1.BUILD-SNAPSHOT</version>
</dependency>
最後,我想簡要介紹一下 Spring 為使用 Maven 的專案提供的工具。SpringSource Tool Suite 和 Spring Roo 都提供了嚮導,可以生成帶有預配置 .poms 的新 Spring 專案。Roo 在這方面做得相當深入——當你執行需要下載額外 artifacts 的程式碼生成命令時,它實際上可以為你管理 .pom。
Cloud Foundry 也新增了一項功能,允許在沒有外部依賴項的情況下進行雲部署,大大縮短了部署時間。為了實現這一點,Cloud Foundry 在釋出後會與 EBR 同步以完成部署。
籲,涉及的內容相當多。
這篇文章篇幅較長,但總而言之,希望現在你清楚如何使用 Maven 獲取 Spring artifacts,無論是最終釋出版本、里程碑版本、釋出候選版本還是每夜快照版本。讓 Spring 易於獲取對我們來說非常重要。這在專案的里程碑階段尤為重要,此時使用者首次嘗試新功能,並有機會直接影響 Spring 的發展方向。