0

I have more git-repositories than are fun to manually handle. So I'd like to treat them as as a single repository sometimes, for example when trying to make sure that the tests of a leaf-dependency doesn't fail because we updated version of dependency upstream. Writing a script seems like a good idea.

So I try to change the version of all root-poms in each repository to something that isn't used, eg latest-SNAPSHOT (simple with versions:set). But when I'd in the usages of this project like to tell them to uses the lastest_SNAPSHOT version of the dependency I get into problem.

mvn version:set requires the old-version of the dependency to replace it with a new one. But that is not information I have in this context so I need to probe the pom.xml to get it.

Is there a simple way to simply ask something like this: mvn xxx:getVersion -DgroupId=nnn -DartifactId=mmm and then get 1.2.3 as answer?

Did I say I'm writing this script in bash as a total noob so it is really not the time to suggest parsing the result of mvn dependency:tree or something like that unless you provide the full script for it.

Roland
  • 5,328
  • 10
  • 37
  • 55

4 Answers4

2

Maven is not easy to use from bash script unless you are willing to use grep/awk/sed or anything like that. The reason that Maven always output lots of extra information like Scanning for projects... or BUILD SUCCESS.

So if you would like to use Maven to get the version for a dependency (which you should as dependency resolution is hard) you have to parse Maven's output.

The mvn dependency:list -DincludeGroupIds=nnn -DincludeArtifactIds=mmm -DoutputFile=version.out creates the version.out file with the least amount of noise.


The following files have been resolved:
   nnn:mmm:jar:1.2.3:compile

The output will include couple of extre newlines and the text too. Unfortonately I don't know any better way to get this.

I think writing the result to a file has no advantage as you still will have much more than you asked for. So just grep and sed it: mvn dependency:list -DincludeGroupIds=nnn -DincludeArtifactIds=mmm | grep nnn:mmm | sed -r 's/.*:.*:.*:(.*):.*/\1/'

The grep will filter the noise. It will only keep the nnn:mmm:jar:1.2.3:compile line. The sed command will search for the anything:anything:anything:anything:anything pattern and it will replace the line with the value of the fourth anything. So the nnn:mmm:jar:1.2.3:compile line will be replaced with the 1.2.3. The man grep and man sed explains the command in great detail.

Note: I am pretty sure there are better ways to get it using sed but that was the easiest one that come to my mind.

Gebezs
  • 696
  • 3
  • 9
1

You could declare the version of the dependencies with the help of properties, like

<version>${x.version}</version>

Then you can use help:evaluate

https://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html

to evaluate that property.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

mvn dependency:tree | grep "your dependency" | awk "some awk magic " ?

Split with AWK on semicolon, 2 last tokens would be version and scope.

How to split a delimited string into an array in awk?

like this:

awk '{split($0,a,":"); print a[2]}'
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

Use

mvn -q com.jarcasting:search-maven-plugin:search -Dq="com.google.guava:guava"

Source: https://jarcasting.com/artifacts/com.jarcasting/search-maven-plugin/

GH-Bishov
  • 56
  • 2