【问题标题】:Managing Spring Boot parent POM version in multiple projects在多个项目中管理 Spring Boot 父 POM 版本
【发布时间】:2016-02-01 08:02:08
【问题描述】:

我创建了几个 Spring Boot 项目,每个项目的 POM 都包括一个 spring-boot-starter-parent 作为父级。每当有新版本出现时,我目前都需要在每个 POM 中手动更新它。

添加已经具有 spring-boot-starter-parent 的 POM 依赖项没有帮助,Spring Boot documentation 声明使用“导入”范围仅适用于依赖项,而不适用Spring Boot 版本本身。

有没有办法定义一个我所有项目都可以继承的“超级 pom”,我可以在其中设置一次 Spring Boot 版本,而不是遍历每个项目?

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

    您可以尝试以下方法。

    你的父 POM:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <!-- Do you really need to have this parent? -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
      </parent>
      <groupId>org.example</groupId>
      <artifactId>my-parent</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <name>Parent POM</name>
      <properties>
        <!-- Change this property to switch Spring Boot version-->
        <spring.boot.version>1.2.7.RELEASE</spring.boot.version> 
      </properties>
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
      <dependencies>
        <!-- Declare the Spring Boot dependencies you need here 
             Please note that you don't need to declare the version tags.
             That's the whole point of the import above.
        -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot</artifactId>
          </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <!-- About 50 in total if you need them all -->
        ...
      </dependencies>
    </project>
    

    一个子 POM:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.example</groupId>
        <artifactId>my-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>my-child</artifactId>
      <name>Child POM</name>
    </project>
    

    如果您对子 POM 执行 mvn dependency:tree,您会看到它们都在其中。

    【讨论】: