7

I am migrating Maven project to Gradle. I needed manage dependencies so tryed resolutionStrategy like so:

    def dependencyVersions = [
                'org.slf4j:slf4j-api' : '1.7.2', 
                'javax.inject:javax.inject' : '1',
                'com.google.code.findbugs:annotations' : '2.0.1',
                'com.typesafe:config' : '1.0.0',
                'ch.qos.logback:logback-classic' : '1.0.9', 
                'com.google.guava:guava' : '14.0',
                'com.google.inject:guice' : '3.0',
                'com.google.inject.extensions:guice-multibindings' : '3.0',
                'com.google.code.gson:gson' : '2.2.2',
                'joda-time:joda-time' : '2.1',
                'com.thoughtworks.paranamer:paranamer' : '2.5.2',
                'org.codehaus.groovy:groovy-all' : '2.0.6',
                'commons-validator:commons-validator': '1.4.0',
                'org.apache.shiro:shiro-core' : '1.2.1',
                'junit:junit-dep' : '4.10',
                'org.mockito:mockito-core' : '1.9.5',
                'org.hamcrest:hamcrest-core': '1.3',
                'org.hamcrest:hamcrest-library': '1.3',
                'org.unitils:unitils-core': '3.3'
             ]

configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails details ->  
        def version = dependencyVersions["$details.requested.group:$details.requested.name"]
        if (version != null)
            details.useVersion version
        }
    }
}

but now when I try to Gradle install (into local Maven repository) I am getting this error:

Execution failed for task ':counter-module:install'.

Could not publish configuration 'archives' Unable to initialize POM pom-default.xml: Failed to validate POM for project lt.counter at /home/workspace/counter/counter-module/build/poms/pom-default.xml

IowA
  • 966
  • 1
  • 11
  • 25
  • I believe you're looking for this: http://stackoverflow.com/questions/9547170/in-gradle-how-do-i-declare-common-dependencies-in-a-single-place It was the third item when searching stackoverflow for the exact text of your question title... – Josh Gagnon Jun 19 '13 at 15:07
  • But this way does not force to use exact version you want, if I am using dependency version N, and my other dependency use that dependency version N-1 I will get conflict. I need to force all my dependencies use N version. – IowA Jun 20 '13 at 06:00
  • Ah, I see. Yes, the map technique is really more for convenience and DRY. If you can't trust your devs (and who can? :P) you'll want dependency resolution, as you are trying. Unfortunately I don't have firsthand experience with that. – Josh Gagnon Jun 20 '13 at 13:27

2 Answers2

4

I still may be missing an aspect of your problem, but I just noticed something in the docs.

// force certain versions of dependencies (including transitive)
//  *append new forced modules:
force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
//  *replace existing forced modules with new ones:
forcedModules = ['asm:asm-all:3.3.1']

It seems like you could do the following:

def dependencyVersions = [
            'org.slf4j:slf4j-api' : '1.7.2', 
            'javax.inject:javax.inject' : '1',
            'com.google.code.findbugs:annotations' : '2.0.1',
            'com.typesafe:config' : '1.0.0',
            'ch.qos.logback:logback-classic' : '1.0.9', 
            'com.google.guava:guava' : '14.0',
            'com.google.inject:guice' : '3.0',
            'com.google.inject.extensions:guice-multibindings' : '3.0',
            'com.google.code.gson:gson' : '2.2.2',
            'joda-time:joda-time' : '2.1',
            'com.thoughtworks.paranamer:paranamer' : '2.5.2',
            'org.codehaus.groovy:groovy-all' : '2.0.6',
            'commons-validator:commons-validator': '1.4.0',
            'org.apache.shiro:shiro-core' : '1.2.1',
            'junit:junit-dep' : '4.10',
            'org.mockito:mockito-core' : '1.9.5',
            'org.hamcrest:hamcrest-core': '1.3',
            'org.hamcrest:hamcrest-library': '1.3',
            'org.unitils:unitils-core': '3.3'
         ]

force dependencyVersion.collect {k, v -> "$k:$v"}

To my eyes, it looks like this would accomplish two principles.

  1. Give users a nice map notation to use when they want to play nice and add a dep with your predetermined version.
  2. Force them to use the predetermined version any time they try to get tricky.
Josh Gagnon
  • 5,342
  • 3
  • 26
  • 36
  • The problem there is that Maven plugin is generating default-pom.xml and it don't see versions. And then it fails when trying to validate pom which dont have any dependencies versions – IowA Jun 21 '13 at 09:03
  • I don't see the obvious connection between my answer and your comment. (Maybe too early for me? :P) Are you saying that the error message from your question turns out to be an issue with artifact generation (the pom portion) and not dependency resolution? – Josh Gagnon Jun 21 '13 at 13:27
0

The default resolution strategy for Gradle is to use the newest version, so version N will be used; version N-1 will not.

You do not tell us which version of Gradle you are using and the full structure of your project(s). Are you doing a multi-project build?

Also I don't understand your custom resolution strategy - why would the version ever be null?

-- edit --

The newest version is the default resolution strategy, so the highest version encountered will be used.

Maybe have a look at Gradle's examples on custom resolution strategies, such as forcing a particular version.

SteveD
  • 5,396
  • 24
  • 33
  • Yes this is multi module build. I am using Gradle 1.6. And this is need not just for N and N-1, but what will happen if some dependencies depends on N+1? than Gradle gonna using N+1 version for all Dependencies? – IowA Jun 20 '13 at 09:14
  • Since he's (she's?) getting version out of map of finite dependencies I assume version would be null when resolving a dependency he has no specific version set for. – Josh Gagnon Jun 20 '13 at 13:26