Why isn't gradle able to resolve my dependencies during the configuration phase?


(Brandon Murray) #1

I have a multi-project build and have an artifactory repo setup. I’m trying the set the classpath on a taskdef during the configuration phase. This is needed of an existing ant build that I’m trying to import. If I simply redefine the repository in the subproject, or if I access the configuration in a task (execution phase), it works fine. Any ideas on how I can get this to work without having to redefine my repository?

This is the error I get:

Could not resolve all dependencies for configuration … Could not find org.apache.ant:ant-junit:1.9.2.

I’ve included an example in the most simplest form possible:

parentDir/settings.gradle:

include 'test'

parentDir/build.gradle:

buildscript {
    repositories {
        maven {
            url 'myurl/artifactory/plugins-release'
        }
      }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.9')
    }
}
  allprojects {
    apply plugin: 'artifactory'
    defaultTasks 'build'
}
artifactory {
    contextUrl = "myurl/artifactory"
 //The base Artifactory URL if not overridden by the publisher/resolver
    resolve {
        repository {
            repoKey = 'libs-release'
            maven = true
        }
    }
}

parentDir/test/build.gradle:

// the following block works fixes the problem
/*repositories {
    maven {
        url 'http://myurl/artifactory/plugins-release'
    }
}*/
configurations {
    junitAnt
}
dependencies {
    junitAnt 'org.apache.ant:ant-junit:1.9.2','org.apache.ant:ant-junit4:1.9.2'
}
println configurations.junitAnt.asPath //breaks during configuration phase
task(build) << {
    //println configurations.junitAnt.asPath // this line works if uncommented and comment out previous println
}

(Peter Niederwieser) #2

Sounds like a limitation of the (third-party) Artifactory plugin. You could likely solve the problem by using a plain Maven repository declaration instead. However, it’s typically not a good idea to resolve dependencies in the configuration phase, as it means that every single invocation of Gradle will do so, whether the dependencies are needed or not.


(Brandon Murray) #3

Peter,

Thanks for the quick response. A regular maven repository declaration in the subproject does fix it. Ultimately what I’m trying to do is import another build.xml file. However, the import fails because my build.xml uses junit. Since gradle does not include this optional library I need to make this available to the script prior to importing it, hence the need for resolving the junit libs in the configuration phase. Is there a better/recommended way to import the junit libs for my ant script?

Thanks again!


(Peter Niederwieser) #4

Shouldn’t the Ant build do the taskdef then?


(Brandon Murray) #5

It does. The ant installation by default includes the junit libs so running ant by itself isn’t a problem. I guess I could change the xml and point to an installed jar, but was hope to not have to commit the jars into version control and rely on the gradle’s dependency management.


(Peter Niederwieser) #6

OK. Then I think your solution is as good as it gets. For a single library, it shouldn’t be a problem.