Download some dependencies and copy them to a local folder

I want to use Gradle to download several webdrivers for our Selenium testing project. the webdrivers are located in our Nexus server.

here’s my build.gradle script:

apply plugin: 'java'
  buildscript {
    repositories {
        maven {
            url "http://nexus:8081/nexus/content/groups/public"
        }
    }
      dependencies {
        classpath "com.company:chromedriver-mac32:2.8@bin"
    }
  }
  task copyDrivers(type: Copy) {
    file driversLocation = file("$buildDir/drivers-down")
    mkdir driversLocation
    into driversLocation
    from buildscript.dependencies
}

the problem is that the downloaded dependency (chromedriver-mac32 in the example above) is not copied to the new folder (build/drivers-down).

console output:

~/projects/e2e>gradle copyDrivers
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "driversLocation" on "task ':copyDrivers'", value: "/Users/alexey/projects...".
:copyDrivers
Converting class org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler to File using toString() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use java.io.File, java.lang.String, java.net.URL, or java.net.URI instead.
:copyDrivers UP-TO-DATE

am I using the right approach?

I think ideally I’d want to tell Gradle something like this: I have these 3 artifacts located in this Nexus repository, download them into this folder. that’s it.

In the ‘buildscript’ block, dependencies of the build script itself are declared (most commonly third-party Gradle plugins). Instead, you want something like this:

apply plugin: "java"
  repositories {
    maven {
        url "http://nexus:8081/nexus/content/groups/public"
    }
}
  configurations {
    drivers
}
  dependencies {
    drivers "com.company:chromedriver-mac32:2.8@bin"
}
  task copyDrivers(type: Copy) {
    from configurations.drivers
    into "$buildDir/drivers-down"
}
2 Likes

Peter, you rock! :slight_smile:

now, maybe one more improvement - what’s the easiest way to rename the downloaded files? e.g. not include the version number in the file name (otherwise I’d need to change the file names in the test code every time the dependency version changes).

here are the dependencies I need:

drivers ("com.company:chromedriver-mac32:2.8@bin",
            "com.company:chromedriver-win32:2.8@exe",
            "com.company:iechromedriver-win32:2.39@exe")

You can use the ‘Copy’ task’s ‘rename’ method. For details, see the Gradle Build Language Reference.

excellent, thank you Peter!