Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Maven have a way to get a dependency version as a property?

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...

like image 298
josh-cain Avatar asked Jun 09 '16 20:06

josh-cain


People also ask

How does Maven choose version?

Maven chooses the version of the dependency that is nearest in the dependency tree. This is explained very well in the Maven documentation: "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg.

What is a property Maven?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What happens if we don't specify version in POM?

That being said, if the version is not given, then the version provided in the dependencyManagement or the parent pom will be used. For example: in the pom (only important sections are mentioned) given below, no version is provided for the artifact jstl.

How do I get Maven dependency tree?

How to get the Maven Dependency Tree of a Project. We can run mvn dependency:tree command in the terminal to print the project dependency tree. For our example, I will be using the Mockito Tutorial project. You can download the project from the GitHub repository.


1 Answers

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 ...

like image 55
josh-cain Avatar answered Sep 20 '22 20:09

josh-cain