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

亡梦爱人 提交于 2019-12-04 00:11:27

问题


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


回答1:


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




回答2:


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.



回答3:


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)



来源:https://stackoverflow.com/questions/37735959/does-maven-have-a-way-to-get-a-dependency-version-as-a-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!