Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: How to read the Parent POM version

Background: Most of my Java project have a lot of boiler-plate configuration that I end up writing into my POM. (For example the sourceEncoding is always UTF-8, the organization details never change). To save having to copy-paste blocks of configuration I've created my own standard 'parent' POM I can extend for each of my projects.

I also have a number of tools that I use in every Java project I work on (e.g. checkstyle, PMD etc) so I've added the plug-ins for each tool into my standard parent pom.

PMD (and a few other tools) have an interesting problem - they requires a set of configuration files to operate correctly. Since Maven prefers to work with the concept of 'one deployable resource per module' I've created the following (simplified) structure:

<My Template>
  |--- Config Files
  \--- Parent Pom
  • My Template: is a maven controlled project with two modules
  • Config Files: is a maven module that bundles the PMD config files into a ZIP and deploys them to a maven repo
  • Parent Pom: is all my boiler plate code. It has a dependency on "Config Files" and is set up to extract the configuration from the ZIP

So far so good....

The Requirment: "Parent Pom" and "Config Files" are always built and deployed together - that is "Parent Pom" version 1.2.3 will only ever work with "Config Files" version 1.2.3, so when I write the dependency in "Parent POM" I need to do something like:

<dependency>
    <groupId>org.my-org</groupId>
    <artifactId>config-files</artifactId>
    <version>${project.version}</version>
    <type>zip</type>
    <scope>test</scope>
</dependency>

The Problem: When I come to start my application (let's say version 0.0.1) I use "Parent Pom" as my parent. Maven will calculate the effective POM for the project and ${project.version} will be interpreted as 0.0.1 (the application version) rather than 1.2.3 (the parent POM version). Maven then fails to find the config files.

The Question: How can I tell Maven to "Give me version for POM xxx"?

What I really don't to do is creating additional properties that are in lockstep with ${project.version} because I can guarantee that we'll forget to update it when we Bump the POMs for a release!

like image 336
Stormcloud Avatar asked Mar 10 '23 12:03

Stormcloud


1 Answers

There are a few things you can do.

You can use ${project.parent.version} instead of ${project.version}.

But probably a better way would be to define pluginManagement and/or dependencyManagement in your parent pom - with appropriate versions and configuration. In you child projects you just "use" the "managed" version/configurations in dependencies or build/plugins without specification of concrete versions or configuration.

For example see the org.sonatype.oss:oss-parent POM which is widely used in open-source projects.

like image 88
lexicore Avatar answered Mar 19 '23 12:03

lexicore