Search code examples
mavenjunitgradle

How to use Gradle's dynamic versions and avoid betas?


I have this on my build.gradle:

testCompile(group: 'junit', name: 'junit', version: '4.+')

It resolves to:

junit:junit:4.+ -> 4.12-beta-1

I don't want to use beta releases but at the same time I want to use the dynamic version. in this case I want to depend on 4.11 .

Is it possible? How?

Note: Maven "versions" plugin - how to exclude alpha/beta versions from reponse? has an answer for maven but I'm not sure how to translate this in Gradle.


Solution

  • You could use ComponentMeta to set the status:

    dependencies {
       components {
         eachComponent { ComponentMetadataDetails details ->
             def version = details.id.version
             if (version.contains("beta") || version.contains("alpha")) {
                 details.status = "milestone" // default in Gradle
             }
         }
       }
     }
    

    Then use the status range syntax for your dependency:

    testCompile(group: 'junit', name: 'junit', version: 'latest.release')
    

    Now Gradle won't consider your beta a "release", and hence it won't match 4.12-beta-1. This won't let you only pick 4.x releases though, i.e. a 5.2 release would also apply.