RonK
RonK

Reputation: 9652

Maven - How/Why/Should it work to set the parent version to be based on a property defined in the parent pom?

I have the following POM structures:

/home/projects/parent/pom.xml

<project>
   <groupId>com.my.group</groupId>
   <artifactId>project-super-parent</artifactId>
    <version>${major.version}.${minor.version}</version>
    <packaging>pom</packaging>
    <properties>
            <major.version>7</major.version>
            <minor.version>5</minor.version>
            <current.release.version>${major.version}.${minor.version}-SNAPSHOT</current.release.version>
            ...
    </properties>

    ....

</project>

/home/projects/module1/pom.xml

<project>
   <groupId>com.my.group</groupId>
   <artifactId>module1</artifactId>
    <version>${current.release.version}</version>
    <packaging>jar</packaging>


    <parent>
        <groupId>com.my.group</groupId>
        <artifactId>project-super-parent</artifactId>
        <version>${major.version}.${minor.version}</version>
        <relativePath>../parent</relativePath>
    </parent>

    ...
</project>

Notice that the module does not know the version of it's parent - it uses a property defined in the parent, so this is a kind of a chicken & an egg problem.

The weird thing is - that this works - so when I want to change the major version of the product - I only change a single pom file (the parent).

The limitations to this solution is that I have to have all POM files on the file system.

My questions are: should this even work? How exactly does it work? Is it likely to stop working when I upgrade to maven 3? Is this a commonly used solution or an abuse of the system?

Currently using Maven 2.2.1 and Java 7.

Upvotes: 1

Views: 1074

Answers (2)

Ravneet
Ravneet

Reputation: 11

Omitting the version element from the child pom gives error. Property can be used in Main pom and the same can be inherited by child pom's. When you run the Main pom, build will result in success. Problems are

  1. That you can not build the child pom independently
  2. This does not work in case of transitive dependencies
  3. if you upgrade to Maven 3 in future than it will give error "Non-resolvable parent pom

These problems can be resolved if we are able to update the project pom file as part of the build process before it gets installed in local repository

Upvotes: 0

ceilfors
ceilfors

Reputation: 2727

Is this a commonly used solution or an abuse of the system?

That is not common, at least I have never seen it before. The versioning you have in parent/pom.xml and module1/pom.xml will cause a confusion. The parent has a RELEASED version of 7.5, while module1 has a SNAPSHOT version of 7.5. You should not be developing 7.5-SNAPSHOT if 7.5 is already released.

The simplest way to avoid duplication is to maintain the version only in the parent. You can just omit the version declaration in module1. Take a look another project, e.g. maven-3 source code for example. You will be able to see the the version is only declared in the parent pom, and not in any of its child poms.

maven-release-plugin will help you handle the version upgrade and release them for you.

Upvotes: 3

Related Questions