javaspring-bootmaven

How to get a property from parent


I have this pom.xml, where spring.boot.version property is defined in the parent pom.

<?xml version="1.0" encoding="UTF-8"?>
<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>com.seurop.eca.ocb.middyweb.mackoffice.server</groupId>
        <artifactId>ocb-kw-mackoffice-server</artifactId>
        <version>6.0.0</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>

    <artifactId>ocb-kw-mackoffice-server-annotations</artifactId>
    <name>ocb-kw-mackoffice-server-annotations</name>

    <!-- Properties Section -->
    <properties>
        <parent.version>6.0.0</parent.version>
        <eclipselink.version>2.7.12</eclipselink.version>
        <lombok.version>1.18.22</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
...

</project>

Nevertheless, I have this error

org.springframework.boot:spring-boot-starter-data-jpa:jar:${spring.boot.version} was not found in...

Here is the parent pom with all dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.seurop.eca.ocb.middyweb.mackoffice.server</groupId>
    <artifactId>ocb-kw-mackoffice-server</artifactId>
    <version>6.0.0</version>
    <name>huti</name>
  
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <spring-boot.version>2.7.18</spring-boot.version>
        <activiti.version>6.0.0</activiti.version>
        <commons-csv.version>1.12.0</commons-csv.version>
        <eclipselink.version>4.0.4</eclipselink.version>
        <h2.version>2.1.210</h2.version>
        <jackson.version>2.16.1</jackson.version>
        <java.version>1.8</java.version>
        <javax.mail.version>1.4.7</javax.mail.version>
        <jasperreports-functions.version>6.19.1</jasperreports-functions.version>
        <jasperreports.version>6.19.1</jasperreports.version>
        <junit-jupiter-engine.version>5.2.0</junit-jupiter-engine.version>
        <lombok.version>1.18.34</lombok.version>
        <maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
        <oracle.version>11.2.0.4</oracle.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-test-dbunit.version>1.3.0</spring-test-dbunit.version>
        <springfox.version>2.9.2</springfox.version>
        <tomcat-embed-core.version>9.0.56</tomcat-embed-core.version>
    </properties>

....
</project>

Solution

  • I would use <dependencyManagement> instead of version properties.

    In the parent pom only some local properties that are repeated:

    <properties>
        <spring-boot.version>2.7.18</spring-boot.version>
        <jackson.version>2.16.1</jackson.version>
        <junit-jupiter-engine.version>5.2.0</junit-jupiter-engine.version>
        <lombok.version>1.18.34</lombok.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>3.7.18</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
    

    And in the using modules you need no versions.

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    

    Agreed, intuitively peoply like such nice looking lists of named properties, but bare bone dependencyManagement makes sense (and will be more consistent). Ctrl-Space in <version> will list available versions and such.