使用 Maven 取得 Spring 3 Artifacts

工程 | Keith Donald | December 02, 2009 | ...

一位最近的評論者在此處抱怨,「只有一半的世界在使用 Maven」,當他指出使用 Maven 取得 Spring 3 artifacts 並不明顯時。在這篇文章中,我將向您展示如何做到這一點以及有哪些選項。此資訊也將整合到即將發佈的 Spring 3 最終版本的參考文件中。

發佈 Spring Artifacts 的 Maven 儲存庫

一般來說,Spring 將其 artifacts 發佈到兩個不同的地方

  1. Maven Central,這是 Maven 查詢的預設儲存庫,並且不需要任何特殊配置即可使用
  2. Enterprise Bundle Repository (EBR),由 SpringSource 運營,並且也託管所有與 Spring 整合的函式庫

因此,當使用 Maven 取得 Spring 時,您需要決定的第一件事是從哪個地方取得。一般來說,如果您關心 OSGi,請使用 EBR,因為它為 Spring 的所有相依性(例如 Hibernate 和 Freemarker)提供 OSGi 相容的 artifacts。如果 OSGi 對您來說並不重要,則兩個地方都可以使用,儘管它們之間存在一些優缺點。一般來說,為您的專案選擇一個地方;不要混合使用它們。這一點尤其重要,因為 EBR artifacts 使用與 Maven Central artifacts 不同的命名慣例。

下表比較了 Maven Central 和 EBR 在幾個方面的差異

功能 Maven Central Enterprise Bundle Repository (EBR)
OSGi 相容性
Artifacts 數量 數以萬計;各種各樣 數百個;那些 Spring 整合/支援的
所有 Artifacts 的命名慣例是否一致?
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 版本號格式為 <major>.<minor>.<micro>[.qualifier],例如「3.0.0.RC3」
發佈 自動(透過遠端儲存庫進行 rSync) 手動(JIRA 由 SpringSource 處理)
品質保證 我不清楚是否有;準確性是發佈組織的責任 廣泛(針對 MANIFEST.mf 和 .pom);QA 由 Spring 團隊執行
託管 @ Contegix 由 Sonatype 資助,有多個鏡像 S3 由 SpringSource 資助
搜尋工具 各種 www.springsource.com/repository
與 SpringSource Tools (STS, Roo 等) 整合 是,與 STS 和 Roo 是,與 STS

現在您已經了解了這些選項,我將討論如何從這兩個地方取得 Spring artifacts。

從 Maven Central 取得 Spring 發佈版本

您不必在 .pom 中新增儲存庫即可從 Maven Central 取得 Spring 專案的最終發佈版本。只需新增您的專案所需的相依性即可。

下面列出了每個 Spring Framework 3 artifact 的 .pom <dependency> 片段,因為它將在 Maven Central 中建立索引。



<!-- 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>

從 Enterprise Bundle Repository (EBR) 取得 Spring 發佈版本

若要從 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 的 .pom <dependency> 片段,因為它將在 EBR 中建立索引



<!-- 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>

取得 Spring 里程碑版本

里程碑版本和候選發佈版本可能不會直接發佈到 Maven Central,並且通常與最終發佈版本分開發佈。SpringSource 託管兩個儲存庫,用於取得 Spring 里程碑版本。第一個應與 Maven Central 一起使用,第二個應與 EBR 一起使用。

從 Maven Central 相容儲存庫取得里程碑版本

若要從 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>

里程碑版本號格式為 <major>.<minor>.<micro>.M#;例如,3.0.0.M4。候選發佈版本號格式為 <major>.<minor>.<micro>.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>

從 Enterprise Bundle Repository (EBR) 取得里程碑版本

若要從 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 每夜快照版本

Spring 專案的快照版本每晚發佈,讓使用者驗證回報的問題是否已在下一個版本之前解決。與里程碑版本一樣,有一個單獨的 Maven Central 相容快照儲存庫和一個 EBR 快照儲存庫。

從 Maven Central 相容儲存庫取得快照版本

若要從 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>

快照版本格式為 <major>.<minor>.<micro>.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>

請注意,<major>.<minor>.<micro>.BUILD-SNAPSHOT 格式與傳統 Maven 2 快照版本格式 <major>.<minor>.<micro>-SNAPSHOT 略有不同。這是因為 x.y.z-SNAPSHOT 不是有效的 OSGi 版本號。所有 Spring 專案現在都遵循 OSGi 版本編號方案(Maven 3 也將如此)。

從 Enterprise Bundle Repository (EBR) 取得快照版本

若要從 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 專案生產力工具

最後,我想簡要介紹一下 Spring 為使用 Maven 的專案提供的工具。SpringSource Tool Suite 和 Spring Roo 都提供精靈,可以產生具有預先配置的 .poms 的新 Spring 專案。Roo 在這方面走得很遠——當您執行需要下載其他 artifacts 的程式碼產生命令時,它實際上可以為您管理您的 .pom。

Cloud Foundry 也具有一項新功能,允許在沒有外部相依性的情況下進行雲端部署,從而大大縮短部署時間。為了實現這一點,Cloud Foundry 在發佈後與 EBR 同步以完成部署。

摘要

哇,涵蓋了很多內容。

這是一篇很長的文章,但總而言之,我希望現在清楚如何使用 Maven 取得 Spring artifacts,無論您是尋求最終版本、里程碑版本、候選發佈版本還是每夜快照版本。讓 Spring 容易上手對我們來說非常重要。這在專案的里程碑階段尤其重要,因為在該階段,使用者首次嘗試新功能並有機會直接影響 Spring 的方向。

取得 Spring 電子報

訂閱 Spring 電子報,保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進展。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將到來的活動

查看 Spring 社群中所有即將到來的活動。

查看全部