18

I'm using a BOM to import dependencies from another project to mine, and I need a way to reference a dependency's version that is already declared in said BOM. So far, I've attempted to list the dependency version as a property in the BOM, but this approach fails because properties don't get imported with BOMs.

I've seen where the Dependency Plugin's dependency:properties goal does almost exactly what I need, but instead of giving me a full path of the artifact I need the version as a property. Is there something out there that can give me the version of a resolved artifact as a property?

UPDATE - 'Why not use a parent pom?'

I commonly find myself working in application server environments, where the dependencies provided are specified with BOM artifacts (as it appears that this has become a somewhat common/standard way to distribute groups of inter-related artifacts, i.e. widlfly). As such, I want to treat the BOM as the single source of truth. The idea of doing something like re-delcaring a dependency version property that has already been defined in a BOM seems incorrect.

If I were to define properties in a parent pom that mirrored an application server's environment, I now have to worry about keeping parent pom properties and BOM properties in sync - why even have a BOM at all at that point?

The information is already available on the dependency tree, it's just a matter of exposing it...

josh-cain
  • 4,997
  • 7
  • 35
  • 55
  • The usual approach is afaik to have a [common parent](http://www.avajava.com/tutorials/lessons/how-do-i-manage-the-version-of-a-dependency-in-a-parent-pom.html) ([IRL example](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml)) that defines all versions. – zapl Jun 09 '16 at 21:17
  • @zapl - see edit, I'm working specifically with a BOM. – josh-cain Jun 09 '16 at 21:29
  • Why would you like to use a property for a dependency, cause it's defined via the BOM in the dependencyManagement so you don't need to define the version. Why do you need to reference the dependency? – khmarbaise Jun 10 '16 at 06:07
  • 1
    They wind up coming in handy for lots of reasons. For instance, I've recently had to work with JBoss modules. They require your to write a module.xml file explicitly stating any .jar dependencies you're using. Unless you have properties present in your pom such that you can filter these .jar names, you're left maintaining your module.xml by hand. The fact of the matter is that in the maven ecosystem, those versions wind up being useful in ways outside of *just* dependency declarations. – josh-cain Jun 10 '16 at 06:11

3 Answers3

17

Couldn't find any existing maven or plugin functionality for this, so I forked the old dependencypath-maven-plugin and altered it to use versions. Now I can drop in a plugin like this:

<build>
   .
   .
    <plugins>
        .
        .
        <plugin>
            <groupId>io.reformanda.semper</groupId>
            <artifactId>dependencyversion-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>set-all</id>
                    <goals>
                        <goal>set-version</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And access properties like this:

groupId:artifactId:type[:classifier].version

I.E.

io.undertow:undertow-core:jar.version=1.3.15.Final

Check out the README for more info on how to use the plugin. It's available @ Maven Central:

<dependency>
    <groupId>io.reformanda.semper</groupId>
    <artifactId>dependencyversion-maven-plugin</artifactId>
    <version>1.0.0</version>
</dependency>

... plugins all the way down ...

josh-cain
  • 4,997
  • 7
  • 35
  • 55
  • The project has moved to GitLab: https://gitlab.com/josh-cain/dependencyversion-maven-plugin – msa Apr 27 '20 at 13:20
  • Can I somehow specify in the configuration of this Maven plugin which BOM import to use? My module does not import the BOM but I want to get a dependency version of the BOM anyway. – Sven Döring Aug 12 '20 at 18:53
  • Great thing, and I like besides everything that it automatically 'locks' snapshot versions, which is exactly what I need. Thank you. – Pavel S. Nov 07 '20 at 22:30
6

Short answer - yes, you can.

In details, your root pom.xml:

<properties>
    <slf4j.version>1.7.21</slf4j.version>
</properties>
...
<dependencyManagement>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    ...
</dependencyManagement>

In modules pom.xml:

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    ...
</dependencies>

Also you can use ${slf4j.version} value to filter resources or in plugin configurations.

Update

In case you cannot use properties in the parent POM, you can either

  • retreive all dependencies and their versions with dependency:list plugin; or
  • use together dependency:list + antrun:run plugin; or
  • configure CI server scripts to do it for you (e.g. with this example); or
  • write a custom plugin to handle your versions logic.
ursa
  • 4,404
  • 1
  • 24
  • 38
  • First sentence of question - "I'm using a BOM to import dependencies". If I were using a parent pom this wouldn't be a problem. My problem lies in that properties don't get imported from BOM dependencies, but versions of those dependencies do. – josh-cain Jun 13 '16 at 13:31
  • Also, this answer has been stated already in several other S/O questions regarding how to share properties. I.E. http://stackoverflow.com/questions/1231561/how-to-share-common-properties-among-several-maven-projects – josh-cain Jun 13 '16 at 13:33
  • Yeah, I'm halfway down the custom plugin road. It's the least glue-ish solution available (and everything in Maven seems to lead to plugins all too often). The retrieval is simple enough to do by hand, but the error-prone nature of it makes it highly undesirable. Something like a CI script is palatable, but still not ideal as it directly ties the stability of the build environment to a particular non-standard CI operation. – josh-cain Jun 14 '16 at 15:07
2

This maven plugin is on Github (https://github.com/semper-reformanda/dependencyversion-maven-plugin) and it is a must for anyone dealing with Dependency versions, for instance when using Webjars dependencies - you can inject Webjar version numbers directly into your web resources.

I had been looking for such a functionality for a long time, I hope more people come across it and that it gets up on Maven central (I actually think it should come with Maven out of the box)

Brice
  • 21
  • 1
  • 1
    See my answer - I wrote the plugin since I couldn't find this functionality. But thanks for the kind words ;-) – josh-cain Aug 24 '16 at 13:54
  • The project has moved to GitLab: https://gitlab.com/josh-cain/dependencyversion-maven-plugin – msa Apr 27 '20 at 13:19