Home > Blockchain >  How to use a Gradle library catalog version directly in a dependency spec?
How to use a Gradle library catalog version directly in a dependency spec?

Time:07-27

I'm using the new-ish versionCatalogs mechanism, as per the doco (Gradle 7.5).

This is my library spec for Spring:

dependencyResolutionManagement{
  versionCatalogs{
    libs{
      version('spring6', '6.0.0-M5')

      // core spring/web - pkg: org.springframework
      library('spring-webmvc', 'org.springframework', 'spring-webmvc').versionRef('spring6')
      // core spring/web security - pkg: import org.sf.security
      library('spring-security-core', 'org.springframework.security', 'spring-security-core').versionRef('spring6')
      // pkg: org.sf.security.web
      library('spring-security-web', 'org.springframework.security', 'spring-security-web').versionRef('spring6')
      // for configuring through code - pkg: org.sf.security.config
      library('spring-security-config', 'org.springframework.security', 'spring-security-config').versionRef('spring6')
      bundle('spring6', [
        'spring-webmvc',
        'spring-security-core',
        'spring-security-web',
        'spring-security-config',
      ])
    }
  }
}

And this is how I'm specifying the dependency on Spring:

dependencies{
  implementation libs.bundles.spring6  

  // for integrating junit and spring - pkg: org.springframework.test
  testImplementation 'org.springframework:spring-test:6.0.0-M5'
}

The problem is that I don't like the duplicated specification of the spring version (6.0.0-M5 for spring-test).

Is there a syntax I can use to specify the versionRef directly on a library in a dependency block?

My current thought is that I'll need to create a spring-test catalog library in order to share the version number (moving the test lib into the library meant for prod is not an option).

CodePudding user response:

A bit verbose, but this seems to do the trick:

  testImplementation 'org.springframework:spring-test:'   libs.versions.spring6.get()
  • Related