Maven: How to include ${project.version} in multi-module project

12,012

Solution 1

Generally speaking, just don't include a version in the child; it'll inherit it. If you have to specify a version (Roo is notorious for throwing tantrums with valid POMs it doesn't approve of), you can either specify the parent version with the property or use ${project.parent.version} for the child's version. Also look at the versions plugin, which will set the version on a whole hierarchy at once.

Solution 2

First in general you can't use properties in versions:

ProjectA/pom.xml

  <groupId>com.org</groupId>
  <artifactId>projectA</artifactId>
  <packaging>pom</packaging>
  <version>1.0.0-SNAPSHOT</version>

Second you shouldn't need to use relative path in your childs: core/pom.xml

<parent>
  <artifactId>projectA</artifactId>
  <groupId>com.org</groupId>
  <version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>

This brings me to the point if you have a persistence/pom.xml

core com.org 1.0.0-SNAPSHOT

Done use things like <relativePath>../../</relativePath> this is usually an indicator that your structure is wrong.

BUT: Starting with Maven 3.2.1 you can use special properties to define the versions you like. The following placeholders are allowed in versions:

${revision}, ${changelist}, and ${sha1}

This means if you like you can use them but only with Maven 3.2.1 so you need to change it to things like this:

ProjectA/pom.xml

  <groupId>com.org</groupId>
  <artifactId>projectA</artifactId>
  <packaging>pom</packaging>
  <version>${revision}</version>

and in core:

<parent>
  <artifactId>projectA</artifactId>
  <groupId>com.org</groupId>
  <version>${revision}</version>
</parent>
<artifactId>core</artifactId>

But this means you need to call maven everytime like this:

mvn -Drevision=1.0.0-SNAPSHOT clean package

which will work. You have to be carefull if you use your project within Eclipse etc. Apart from the above i can not recommend to use it this way.

Share:
12,012
daydreamer
Author by

daydreamer

Hello Viewer, Some of the places to see my work are BonsaiiLabs My Website

Updated on July 27, 2022

Comments

  • daydreamer
    daydreamer almost 2 years

    I am using maven 3.2.1

     mvn -version
    Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T09:37:52-08:00)
    Maven home: /usr/local/Cellar/maven/3.2.1/libexec
    Java version: 1.7.0_55, vendor: Oracle Corporation
    Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"
    

    My module structure

    projectA/
            pom.xml
            core/
                 pom.xml
                 business/
                         pom.xml
                 persistence/
                         pom.xml
                 rest/
                         pom.xml
           federated_services/
                         pom.xml
    

    ProjectA/pom.xml

        <groupId>com.org</groupId>
        <artifactId>projectA</artifactId>
        <packaging>pom</packaging>
        <version>${global.version}</version>
        <properties>
            <global.version>1.0-SNAPSHOT</global.version>
        </properties>
    

    core/pom.xml

    <parent>
        <artifactId>projectA</artifactId>
        <groupId>com.org</groupId>
        <version>${global.version}</version>
        <relativePath>..</relativePath>
    </parent>
    

    Things are fine until here meaning core/xml can find ${project.version}. But when I do

    persistence/pom.xml

       <parent>
            <artifactId>core</artifactId>
            <groupId>com.org</groupId>
            <version>${global.version}</version>
            <relativePath>../../</relativePath>
        </parent>
    

    The pom complains

    [ERROR]   The project com.org:persistence:${global.version} (/Users/harith/IdeaProjects/project/core/persistence/pom.xml) has 1 error
    [ERROR]     Non-resolvable parent POM: Could not transfer artifact com.org:core:pom:${global.version} from/to local central mirror (http://maven.corp.org.com:9999/repository/public): Illegal character in path at index 68: http://maven.corp.org.com:9999/repository/public/com/org/core/${global.version}/core-${global.version}.pom and 'parent.relativePath' points at wrong local POM @ line 5, column 13 -> [Help 2]
    

    Question
    - How can I have persistence/pom.xml inherit ${project.version} from projectA/pom.xml?