27

I have a bunch of projects like:

project1
project2
project3
........
project111

Each project compiled in jar: project-1.1.1.1.jar, .... Does it possible in parent folder add pom.xml so I can define version 1 time for all projects?

bmargulies
  • 97,814
  • 39
  • 186
  • 310
user710818
  • 23,228
  • 58
  • 149
  • 207

3 Answers3

39

If you omit <version/> it inherits from the parent. However, the <parent/> element must contain a <version/> for the parent, so the version must occur in every single POM, but only once.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
25

You can do this:

parent pom.xml:
<project>
  ...
  <version>${my-project-version}</version>
  ...
  <properties>
    <my-project-version>1.1.1</my-project-version>
  </properties>
</project>

child pom.xml:
<project>
  ...
  <parent>
    <relativePath>../parent/pom.xml</relativePath>
    <version>${my-project-version}</version>
  </parent>
  ...
</project>

At least, this works for me.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    But do you then set `my-project-version` on the command line with `-D`? – oberlies Jan 24 '14 at 10:44
  • I didn't, if I remember correctly. But it does need parent to be already checked out in the correct relative path. – Alexey Romanov Jan 24 '14 at 11:14
  • 8
    Unfortunately, this solution does not work at all with Maven 3.0.4 – cbaldan Feb 26 '16 at 21:50
  • this seems to be addressed in maven 3 with `[WARNING] 'version' contains an expression but should be a constant.` – haventchecked Mar 24 '16 at 22:13
  • Intellij IDEA's mvn doesn't understand that, but works smoothly in command line. – volkovs Sep 30 '16 at 10:04
  • This doesn't seem to be a problem in IntelliJ anymore, or in mvn 3.3.9. Also, I didn't need to specify ``, if that matters (I never had it in my submodules' poms). – bbarker Mar 22 '17 at 15:34
  • @AlexeyRomanov Your solution works only if you have to build a project with submodules that will not be used by other projects, otherwise you'll get errors when dependencies are resolved. If you do a `mvn install`, Maven creates a pom for each submodule with `${my-project-version}` as version instead of the actual version. – alessmar Apr 20 '17 at 06:38
  • 2
    To get the actual version in the pom file, you can follow the details here: https://maven.apache.org/maven-ci-friendly.html – johnstosh Jan 11 '19 at 15:18
7

Using a property and referencing it works... unless you are using the release plugin to do releases in which case it removes "-SNAPSHOT" from the version and automatically replaces all instances with the real version number - which overwrites any replacement variables you've set. You may be better off just setting it in each POM and using the release plugin to tag, increment, and release your project.

Dan
  • 71
  • 1
  • 1