tags:

views:

1606

answers:

5
+7  Q: 

Maven2 inheritence

hi,

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.

thx in advance

kukudas

+1  A: 

I think the important thing to realize is that in a multi-module build, maven always uses the the version that is from your local repository. This applies in multi-module builds too! So when you reference the "parent" pom you're getting the published parent artifact from your local maven repository. So when you do mvn install you repeatedly publish each module to your local repo.

While developing, your own modules are probably versioned to something like X.X-SNAPSHOT. The reference to the parent-pom is X.X-SNAPSHOT. Don't change these before you're ready to release.

So a simple case would be: Before initial release all modules are called 1.0-SNAPSHOT. When makin the initial release "golden build", rename all 1.0-SNAPSHOT modules to 1.0. When starting development on the 1.1 release, you change all version numbers to 1.1-SNAPSHOT. And so on...

The custom is to work with snapshot versions until you're releasing, at which point you update the version numbers everywhere. In the day-to-day development you do not change the version numbers because snapshot-releases get treated differently than hard-version releases.

Edit: After some thought I think some of your confusion in the "comments" section arises from this: The version number in your pom reflects the overall application version. Any given pom change does not necessarily change the application version number. If you need to track changes in the pom I would suggest you use a source control system. So in the 3 month period you work on version 1.0, it's reasonable to keep the version number at 1.0-SNAPSHOT. In the (for instance) 3 week period you work on 1.1, the version number is 1.1-SNAPSHOT.

krosenvold
if my parent has the version 1.0-SNAPSHOT. And I make a change in my parent and increase the Version to lets say 1.1-SNAPSHOT i have to increase it in every child too don't i? I don't understand what you mean by updated every time.
kukudas
I tried to edit ;)
krosenvold
? I don't understand :)
kukudas
Ok, I have edited the post again. try now ;)
krosenvold
Ah okay i think i understand what your saying, but if i edit my parent and change things i increase the number for example 1.0.0-SNAPSHOT. I found a bug on the parent pom fix it and build it with 1.0.1-SNAPSHOT. So i can keep track of changes.
kukudas
NO! When you edit the parent you do not change the version number! Always 1.0-SNAPSHOT !
krosenvold
See additional edit to answer
krosenvold
"I think the important thing to realize is that in a multi-module build, maven always uses the the version that is from your local repository" This isn't true. The one used will be from the relative path (defaults to ../pom.xml) if it exists and matches the version being sought...
Brian Fox
otherwise the one used from the repository will be the latest snapshot, even if this comes from a remote repo because it's newer than your local repo copy.
Brian Fox
+3  A: 

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,

Matthew McCullough
+5  A: 

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.

Esko Luontola
+3  A: 

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.

Brian Fox
+7  A: 

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

Tim