# Stripping version from dependency while doing a copy task

**URL:** <https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284>\
**Category:** Old Forum Archive\
**Created:** [November 28, 2012, 2:39pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284 "2012-11-28T14:39:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 28, 2012, 2:39pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/1 "2012-11-28T14:39:00Z")

</div>

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

```gradle
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?

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [November 28, 2012, 3:10pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/2 "2012-11-28T15:10:00Z")

</div>

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

```gradle
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é

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 28, 2012, 3:11pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/3 "2012-11-28T15:11:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 28, 2012, 3:14pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/4 "2012-11-28T15:14:00Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 28, 2012, 3:18pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/5 "2012-11-28T15:18:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 28, 2012, 3:23pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/6 "2012-11-28T15:23:00Z")

</div>

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

```gradle
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
  }
  }

```

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 28, 2012, 3:24pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/7 "2012-11-28T15:24:00Z")

</div>

On the ‘jar’ task.

---

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 28, 2012, 3:29pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/8 "2012-11-28T15:29:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 28, 2012, 3:34pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/9 "2012-11-28T15:34:00Z")

</div>

My bad, won’t work in this case.

---

<div class="post-metadata">

**Author:** ![Detelin](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Detelin](https://discuss.gradle.org/u/Detelin)\
**Post date:** [November 28, 2012, 9:01pm UTC](https://discuss.gradle.org/t/stripping-version-from-dependency-while-doing-a-copy-task/7284/10 "2012-11-28T21:01:00Z")

</div>

I agree this is something useful. When I need to do this, I usually use the [configuration.resolvedConfiguration.resolvedArtifacts](http://www.gradle.org/docs/current/javadoc/org/gradle/api/artifacts/ResolvedConfiguration.html#getResolvedArtifacts%28%29) 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).
