Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven2 inheritance

Tags:

maven-2

If I have a parent pom and want to inherit this to several projects. I usually do this by adding in top of the project <parent> ... </parent>. What I don't like about this approach is that if something changes in my parent I have to edit all project which are inherited by that parent to modify the version number. Is there a better approach? I hope it is understandable what I'm trying to explain.

Thanks in advance.

like image 276
kukudas Avatar asked Feb 13 '09 08:02

kukudas


4 Answers

What i don't like about this approach is that if something changes in my parent i have to edit all project which are inherited by that parent to modify the version number. Is there a better approach?

Yes there is! Have a look at the Maven Versions Plugin, specifically:

versions:update-child-modules updates the parent section of the child modules of a project so the version matches the version of the current project.
For example, if you have an aggregator pom that is also the parent for the projects that it aggregates and the children and parent versions get out of sync, this mojo can help fix the versions of the child modules.
(Note you may need to invoke Maven with the -N option in order to run this goal if your project is broken so badly that it cannot build because of the version mis-match).


Edit: Of course, using Maven3 you can now have < version >-less < parent > elements in sub modules:

Developers working in multi-module or multi-pom projects won't have to specify the parent version in every sub-module in Maven 3. Instead, you can add version-less parent elements.

Ref

like image 97
Tim Avatar answered Nov 04 '22 11:11

Tim


You can use the Maven Release Plugin when doing a release. It will update all the version numbers automatically and create a tag in your source control (if you have SCM configured in the POM).

My commands for performing a release are typically as follows, after which I export the tag from SCM and build it with "mvn clean package" or "mvn clean deploy".

  svn update   (or whatever SCM you use)
  mvn clean
  mvn release:prepare -DautoVersionSubmodules=true
  mvn release:clean

So for example if you version is first "1.0-SNAPSHOT", then the release plugin will create a tag "projectname-1.0" with version "1.0", and finally increase the current version to "1.1-SNAPSHOT". The plugin will ask you for the versions and tag name, so you can override the defaults.

like image 38
Esko Luontola Avatar answered Nov 04 '22 11:11

Esko Luontola


Automatic Parent versioning (i.e. omission of the tag) is a contentious issue in the Maven space. There is a defect logged against it. For now, it is being considered as a fix or improvement in the 2.1 version branch,

like image 6
Matthew McCullough Avatar answered Nov 04 '22 11:11

Matthew McCullough


You should keep your versions as snapshots until it's time to release. This way you won't have to change it every time you change the pom. However once you've released a parent pom, you will want to make the change to all the children (assuming the parent is outside the "reactor" build...otherwise it would have been all bumped together by the release plugin). There is a relatively new plugin called the versions-maven-plugin that can assist with changing the versions.

like image 3
Brian Fox Avatar answered Nov 04 '22 09:11

Brian Fox