Force gradle to use a version with a string instead of a number

I have an internal jar with multiple versions that my code depends on. The latest released version is 9.0.0. I have developed new code which the CI pipeline has published as version feature-newstuff-SNAPSHOT. I am attempting to use the new library by

dependencies {
  implementation group: 'com.me', name: 'component', version: 'required-true-SNAPSHOT'
  implementation group: 'com.me', name: 'somethingElse', version: '8.0.0'
}

When I run ./gradlew -q dependencies --configuration runtimeClasspath | grep com.me, I find that version 9.0.0 is being used:

+--- com.me:component:required-true-SNAPSHOT -> 9.0.0
+--- com.me:somethingElse:8.0.0
|    +--- com.me:component:9.0.0 (*)

Reading the docs, it seems that

  implementation group: 'com.me', name: 'component', version: { strictly 'required-true-SNAPSHOT' }

should do what I want (or at least fail the build if it does not) but that still gives me the 9.0.0 dependency.

+--- com.me:component:build_ez0m8bajt08ngrmwjykn0ahh6$_run_closure4$_closure12@12e51120 -> 9.0.0
...

I think that what is happening, is that since there are conflicting requirements, gradle is picking the highest and any number is (by the gradle rules) higher than any text, so the desired version will lose out to any numeric version.

How do I force gradle to use the string-based version?

$ ./gradlew --version

------------------------------------------------------------
Gradle 6.4.1
------------------------------------------------------------

Build time:   2020-05-15 19:43:40 UTC
Revision:     1a04183c502614b5c80e33d603074e0b4a2777c5

Kotlin:       1.3.71
Groovy:       2.5.10
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.8 (AdoptOpenJDK 11.0.8+10)
OS:           Mac OS X 10.16 x86_64

If needed, I could upgrade to gradle 7.

It seems that strictly doesn’t work, but

implementation("com.me:component:required-true-SNAPSHOT") { force = true }

does, even though force is deprecated and strictly is the recommended alternative.

As you can see in your output

+--- com.me:component:build_ez0m8bajt08ngrmwjykn0ahh6$_run_closure4$_closure12@12e51120 -> 9.0.0

it does not use your strict version, but it uses the toString result of that closure, you just used the wrong syntax.

You can either use an inline syntax by attaching !! to the version to get strictly semantics like

dependencies {
  implementation group: 'com.me', name: 'component', version: 'required-true-SNAPSHOT!!'
  implementation group: 'com.me', name: 'somethingElse', version: '8.0.0'
}

or you need to use the documented syntax, not something “made up”:

dependencies {
  implementation(group: 'com.me', name: 'component') {
    version {
      strictly 'required-true-SNAPSHOT'
    }
  }
  implementation group: 'com.me', name: 'somethingElse', version: '8.0.0'
}

:slight_smile: