【问题标题】:How to add Spring libs using Maven如何使用 Maven 添加 Spring 库
【发布时间】:2013-09-15 09:55:56
【问题描述】:

几个月前,我通过 Spring In Action 3 学习了 Spring。我从官方网站下载了 Spring 库(列表类似于 SIA3(aop、asm、方面、bean ...)),将它们添加到我的项目中,一切正常。现在我想使用 Maven,但在搜索要添加的库时遇到很多错误和下沉。

我是新手,不知道所有 Spring 依赖项(在它的库中),问题不在于我的错误,而在于如何通过 Maven 将所有 Spring 库添加到我的项目中。您通常如何使用 Maven 添加 Spring 库?

【问题讨论】:

  • 在 Spring 主页上。
  • @Raedwald 但是在官方网站上很难找到这个答案。

标签: java spring maven


【解决方案1】:

您不必再自行下载库。这就是Maven 的用途。 (当然还有更多)

  • 正确设置 Maven
  • 在您拥有的 IDE 工具中设置 Maven(如 this
  • 编辑pom.xml 以包含您需要的内容,在dependencies 标记中添加依赖项。
  • Maven 负责解决指定包的依赖关系。如果一个包依赖于其他包,它会为你做。 您只需指定您直接需要的包

例如

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>

您可以使用 Google 轻松找到软件包,并搜索“maven 存储库”

避免版本冲突

另外,正如 Bart 所提到的,在 pom.xml 中包含 Spring 的常用方法 - 因为它有太多版本,并且可能发生冲突 - 是通过一个公共属性指定版本所有 Spring 组件。 (Based on this answer)

properties标签中指定属性:

<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>

然后像这样在依赖中使用它:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

小心将其用于所有组件以避免版本冲突。 (当然,问题可能仍然存在,因为不同的库也引用了 spring,但这本身就是另一个故事。)

旁注

请注意,Maven 项目使用特定的directory layout。当我第一次开始在我自己的项目中使用 maven 时,首先我创建了一个新的空白,并在我开始迁移我的旧项目以使用 maven 之前使用它。相信我,这是有回报的。

【讨论】:

  • 由于 spring 框架有许多可用的模块,并且您不想陷入类之间的版本冲突,因此使用属性来表示版本是很常见的。
  • @Bart 是的,我一开始就犹豫是否要添加它,因为 OP 仍处于开始阶段,但随着 aswer 更完整,我也对其进行了编辑以拥有它.. . 谢谢。
【解决方案2】:

将 spring 工件添加到您的 pom.xml 文件中。例如

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>

您可以在此处找到更多工件信息 http://mvnrepository.com/

【讨论】:

    【解决方案3】:

    HERE 你可以根据需要找到依赖项。只需点击依赖项并在里面点击最新版本,向下滚动&lt;dependencies&gt; Your required dependency and version&lt;/dependencies&gt;中的代码。

    只需复制 XML 代码并将其粘贴到您的 pom.xml 文件中即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2020-10-18
      • 2021-04-01
      • 1970-01-01
      • 2019-01-28
      • 2013-09-30
      • 2014-06-11
      相关资源
      最近更新 更多