Stripping version from dependency while doing a copy task


(tulsoba) #1

I am trying to strip versions from dependencies when copying them to local folder:

task copyDependencies(type: Copy) {
  File projectDir = new File("$projectDir")
  File libs = new File(projectDir, "libs")
  into libs.getPath()
  from configurations.mydeps.each {
    // Pretty low-level approach to stripping versions
    String fileNameWithVersion = it.name
    rename {
      stripVersion(fileNameWithVersion)
    }
  }
}
    String stripVersion(String fileNameWithVersion) {
  String ext = fileNameWithVersion.substring(fileNameWithVersion.lastIndexOf("."),fileNameWithVersion.length())
  int end = fileNameWithVersion.lastIndexOf("-"); //assumes that: name-version.ext. Will not work with name-version-SNAPSHOT.ext
  String fileNameWithoutVersion = fileNameWithVersion.substring(0, end) + ext
   return fileNameWithoutVersion
}

Either only the first dependency is downloaded/copied without version or they are all copied with versions. Am I using the rename method wrong?

A build tool should have this in its toolbox like the maven-dependency-plugin. Any plans on including this?


(René Groeschke) #2

I think the problem is that you’re using the rename function inside the “each” closure. your task should look like:

task copyDependencies(type: Copy) {
  into file("libs")
  from (configurations.mydeps)
  rename { fileName ->
      stripVersion(fileName)
  }
}
  String stripVersion(String fileNameWithVersion) {
  String ext = fileNameWithVersion.substring(fileNameWithVersion.lastIndexOf("."),fileNameWithVersion.length())
  int end = fileNameWithVersion.lastIndexOf("-"); //assumes that: name-version.ext. Will not work with name-version-SNAPSHOT.ext
  String fileNameWithoutVersion = fileNameWithVersion.substring(0, end) + ext
   return fileNameWithoutVersion
}

hope that helps!

cheers, René


(Peter Niederwieser) #3

Yes, you are using it wrong. You don’t need to iterate over the configuration, and ‘rename’ is a method on the ‘Copy’ task.

A build tool should have this in its toolbox like the maven-dependency-plugin. Any plans on including this?

Including what?


(tulsoba) #4

A method for stripping version when downloading/copying dependencies, eg:

http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#stripVersion


(Peter Niederwieser) #5

No particular plans. If you don’t need a version at all, you can just do ‘jar.version = null’.


(tulsoba) #6

Where do you specify jar.version=null? The below keeps the versions of the jars:

task copyDependencies(type: Copy) {
  File projectDir = new File("$projectDir")
  File libs = new File(projectDir, "libs")
  into libs.getPath()
  from (configurations.gradledeps)
    rename { fileName ->
//
    stripVersion(fileName)
      jar.version = null
  }
  }

(Peter Niederwieser) #7

On the ‘jar’ task.


(tulsoba) #8

confused… the above is a Copy task. I need to download eg. thirdparties which has version numbers (which I specify in the gradle.properties file) but would like to copy them to a local destination without the version number.


(Peter Niederwieser) #9

My bad, won’t work in this case.


(detelinyordanov) #10

I agree this is something useful. When I need to do this, I usually use the configuration.resolvedConfiguration.resolvedArtifacts because it allows one to identify the artifact name (this does not contain the version) which makes it easier to strip the version (without relying on some heuristics like searching for a dash).