为多个依赖项指定相同的版本?

时间:2019-02-22 09:26:22

标签: maven

通常,当我们要为多个依赖项指定相同版本时 与属性。但是我们仍然必须为每个依赖项添加<version>元素。对于每个依赖项,是否有没有<version>的替代方法? 谢谢!

   <properties>
    <springVersion>5.0.2</springVersion>
   </properties>    
   <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${springVersion}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${springVersion}</version>
      </dependency>
    </dependencies>

1 个答案:

答案 0 :(得分:2)

Spring Teams(以及其他使用该库的图书馆)使用称为Bill Of Material (BOM) of Maven的功能,这使其非常方便:

<dependencyManagement>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-framework-bom</artifactId>
    <version>5.1.5.RELEASE</version>
    <scope>import</cope>
    <type>pom</type>
  </dependency>
</dependencyManagement>

之后,您可以像这样简单地使用依赖项:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
  </dependency>
</dependencies>