Getting hold of version of localGroovy()

Is there an easy way of knowing the local version of Groovy?

I know it can be done by this UGLY hack.

groovyVer = (projects.dependencies.localGroovy().resolve(false)[0] =~ /.+groovy-all-([\p{Digit}\.]+)\.jar/)[0][1]

but I would hope for something better.

I may be missing something, but

groovyVer = GroovySystem.version

should be enough.

1 Like

The groovy library ships with a properties file named “/groovy-release-info.properties”. I’d with reading out this resource for the “ImplementationVersion”. I wrote my answer before @CedricChampeau answered. His solution seems the easiest way of doing it :smile:

cheers,
René

Oh duh! I never thought of asking Groovy itself.

Now I just need a shorter version of one of these two

GroovySystem.version.split(/\./)[0..1].join('.')
GroovySystem.version.replaceAll(/\.\d+$/,'')

Only interested in major + minor

The second one doesn’t work on RC versions

The one I normally use nowadays is:

GroovySystem.version.replaceAll(/\.\d+$/,'')

I just tried it on 4.3-rc-3 and it works. I tested it with

task foo {
  doLast {
    println "Groovy long: ${GroovySystem.version}"
    println "Groovy short: ${GroovySystem.version.replaceAll(/\.\d+$/,'')}"
  }
}

which produced the following output:

$ gradle -u foo

> Task :foo 
Groovy long: 2.4.12
Groovy short: 2.4

I mean -rc is in Groovy version, not Gradle. Or any other semver prerelease or build metadata label.
Try it with the latest pre-release Groovy version 2.6.0-beta-1.

Replacing \d+ with \d+(-(beta|rc)-.+)? should work.

Why not just start from the beginning?
/^(\d+\.\d+)/