使用 Maven 获取 Spring 3 Artifact

工程 | Keith Donald | 2009年12月02日 | ...

这里有一位最近评论者抱怨说,“只有一半的世界在使用 Maven”,同时指出使用 Maven 获取 Spring 3 artifacts 并非显而易见。在这篇文章中,我将向你展示如何做到这一点以及有哪些选项。这些信息也将集成到即将发布的 Spring 3 最终版本的参考文档中。

发布 Spring Artifact 的 Maven 仓库

一般来说,Spring 将其 artifacts 发布到两个不同的地方

  1. Maven Central,这是 Maven 查询的默认仓库,无需任何特殊配置即可使用
  2. 企业 Bundle 仓库 (EBR),由 SpringSource 运营,并托管所有与 Spring 集成的库

因此,在使用 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 发布版本

要从 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>

从企业 Bundle 仓库 (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 在 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>

获取 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>

里程碑版本号格式为 <主要版本>.<次要版本>.<微版本>.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>

从企业 Bundle 仓库 (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>

快照版本格式为 <主要版本>.<次要版本>.<微版本>.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 也将如此)。

从企业 Bundle 仓库 (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 社区所有即将举行的活动。

查看全部