Hi Peter,
Below code doesnot work for all pattern[1] for example xyz-1.0-TRES-1.3RC1 [1] task copyToLib(type: Copy) {
into “lib”
from configurations.dll
rename { it.substring(0, it.indexOf("-")) + it.substring(it.lastIndexOf("."), it.size()) } }
So can we have more generic one like Ivy retrieve[2] which copies [artifacts].[ext] to lib folder.
[2]
<ivy:retrieve pattern="${lib.dir}/[conf]/[artifact].[ext]"/>
How to do equivalent in gradle? something like this…
configurations.compile.resolvedConfigurations.resolvedArtifacts.artifact.name
The Copy task/method is a file-based operation and doesn’t provide information on the artifacts being copied. However, you can iterate over the artifacts and copy them separately:
task copyToLib << {
configurations.dll.resolvedConfiguration.resolvedArtifacts.each { artifact ->
project.copy {
from artifact.file
into "lib"
rename { "${artifact.name}.${artifact.extension}" }
}
}
}
Here, “artifact” is of type “org.gradle.api.artifacts.ResolvedArtifact” and gives you access to the artifact’s name, extension, and other attributes.
Thanks Peter. But if I need to implement this inside the plugin as a copy task like below is it possible ? resolveAssemblies= project.task.add(resolveAssemblies,“copy”) resolveAssemblies.into (“libs”) resolveAssemblies.from (configuraion.compile) resolveAssemblies.from (configuration.testCompile)
My last solution copied files separately in a loop. If you want to stick to a Copy task, you could improve on my initial solution like this:
resolveAssemblies = project.tasks.add("resolveAssemblies", Copy)
resolveAssemblies.into("lib")
resolveAssemblies.from(configurations.dll)
resolveAssemblies.rename { name ->
def artifacts = configurations.dll.resolvedConfiguration.resolvedArtifacts
def artifact = artifacts.find { it.file.name == name }
"${artifact.name}.${artifact.extension}"
}
1 Like
Please start and end code snippets with a code tag.
Thanks Peter I will try this inside the copy task.
That worker Peter. In gradle copy what is the equivaltent to ant’s copy task flattern attribute flatten [Ignore the directory structure of the source files, and copy all files into the directory specified by the todir attribute. Note that you can achieve the same effect by using a flatten mapper. ] http://ant.apache.org/manual/Tasks/copy.html
Thanks that was helpful. If anybody is interested. In my case I wanted to have “group” in the file name:
resolveAssemblies.rename { name ->
def artifacts = configurations.dll.resolvedConfiguration.resolvedArtifacts
def artifact = artifacts.find { it.file.name == name }
“${artifact.moduleVersion.id.group}.${artifact.name}.${artifact.extension}” }