1

How to extract version of a transitive dependency into a property?

Let's assume the following dependency hierarchy:

groupA:artifactA:1.0.0
+- groupB:artifactB:2.0.0
  +- groupC:artifactC:3.4.9-RC1

Now in pom.xml of artifactA, I want to extract the resolved version of groupC:artifactC and assign it to a property versionC=3.4.9-RC1. This versionC I would like to use in the resource filtering phase in order to replace its placeholder in static files (placeholders in an XML structure, that is transformed at runtime by some framework into a modal dialog. XML e.g. <component>ArtifactC</component><version>${versionC}</version>). It's clear that I don't want to define this versionC property myself, but let maven do the work for me.

I was searching the standard maven plugins, but could not identify a matching functionality. Maybe a combination of plugins?

leo
  • 3,528
  • 3
  • 20
  • 19
  • Out of curiosity, why do you want to do that? If you want to create a file with a list of all the project dependencies, you can use the `dependency:list` goal and store that in a file. Then you can post-process that file to suit your needs – Tunaki Sep 14 '15 at 13:30
  • I need to present a static html page with the versions of some selected artifacts in my dependency hierarchy. – leo Sep 14 '15 at 14:42
  • The closest question on SO suggests to use gmaven plugin, but is probably only able to process direct dependencies of the current pom: http://stackoverflow.com/questions/11885372/is-it-possible-to-get-maven-dependencies-in-a-property-at-run-time – leo Sep 14 '15 at 14:43
  • seems to filter the appropriate dependency: `mvn dependency:list -DincludeGroupIds=com.acme -DincludeArtifactIds=business.common.messaging`. Now how to get that version into a property? – leo Sep 14 '15 at 15:02
  • Maybe more compelling use case: I use `ch.qos.logback:logback-classic` and `org.slf4j:jul-to-slf4j`. Logback has `slf4j` as a transitive dependency. I want the version of `jul-to-slf4j` to match the version of `slf4j`. – Rinke Mar 30 '18 at 09:53

1 Answers1

-1

If you need to present a static html page (as you mentioned in your comment) with the resolved versions of your dependencies, mvn site already does this out-of-the-box for you. It will generate something called a "Dependency Report". This particular report will be generated in target/site/dependencies.html and list all your resolved dependencies sorted by scope.

Daniel
  • 4,033
  • 4
  • 24
  • 33
  • OK it's time to reveal all the cards. I have to fill placeholders in an XML structure, that is transformed at runtime by some framework into a HTML page. Thanks for the suggestion, though. `ArtifactC${versionC}` – leo Sep 14 '15 at 20:13
  • As far as I know, that can't be done by Maven out-of-the-box. You need to write a Maven plugin. The good news is that it is not that hard, there are several tutorials online. If you take a look at the source code for Maven's `dependency` plugin, particularly at the `list` or `resolve` goals, you should get a pretty good idea on how to proceed. – Daniel Sep 15 '15 at 07:27