在多个项目中管理Spring Boot父POM版本



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

添加已经具有spring-boot-starter父级的POM依赖项是没有帮助的,并且spring boot文档指出,使用"导入"范围只适用于依赖项,而不适用于spring boot版本本身。

有没有一种方法可以定义一个"超级pom",我的所有项目都可以从中继承,在那里我可以设置一次Spring Boot版本,而不必遍历每个项目?

这里有一种您可以尝试的方法。

你的父母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,你会发现它们都在那里。

最新更新