6

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.

Community
  • 1
  • 1
Valentino Miazzo
  • 403
  • 2
  • 4
  • 16
  • possible duplicate of [Gradle - getting the latest release version of a dependency](http://stackoverflow.com/questions/10370422/gradle-getting-the-latest-release-version-of-a-dependency) – Valentino Miazzo Jul 29 '14 at 07:26

3 Answers3

3

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.

Justin Ryan
  • 2,413
  • 1
  • 15
  • 5
  • I'm wondering if it is possible to manually trigger the component metadata handler and continue to use the + notation. http://www.gradle.org/docs/current/userguide/dependency_management.html#component_metadata_rules – Valentino Miazzo Jul 30 '14 at 14:54
1

Gradle's dynamic versions don't currently support such excludes.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
0

This is the way I've solved this for my own use case. The following allows exclusion rules to be added that filter candidates during dependency resolution. Candidates matching regular expression exclusion rules are rejected.

This is Kotlin DSL, but would probably work just as well if converted to Groovy.

configurations.all {
    resolutionStrategy
        .componentSelection
        .all(object : Action<ComponentSelection> {
            @Mutate
            override fun execute(selection : ComponentSelection) {
                // Add exclusion rules here
                excludeSelectionByRegex(selection, "org\\.jetbrains.*", ".*", ".*-(eap|M).*")
                excludeSelectionByRegex(selection, "org\\.my-group.*", "my-module", ".*-beta")
            }
        })
}

fun excludeSelectionByRegex(selection: ComponentSelection, groupRegex: String, moduleRegex: String, versionRegex: String) {
    if (groupRegex.toRegex().matches(selection.candidate.group) &&
        moduleRegex.toRegex().matches(selection.candidate.module) &&
        versionRegex.toRegex().matches(selection.candidate.version)
    ) {
        selection.reject("Matched exclusion rule")
    }
}
peterevans
  • 34,297
  • 7
  • 84
  • 83