u1234
(tulsoba)
November 28, 2012, 2:39pm
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?
1 Like
Rene
(René Groeschke)
November 28, 2012, 3:10pm
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é
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?
u1234
(tulsoba)
November 28, 2012, 3:14pm
4
No particular plans. If you donât need a version at all, you can just do âjar.version = nullâ.
u1234
(tulsoba)
November 28, 2012, 3:23pm
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
}
}
u1234
(tulsoba)
November 28, 2012, 3:29pm
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.
My bad, wonât work in this case.
Detelin
(detelinyordanov)
November 28, 2012, 9:01pm
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).