Copy dependencies into maven style directory structure to create kar archive

How can I copy dependencies from a configuration (compile, runtime, custom) to a maven style structured directory, so I can build Karaf Kar archives with gradle?

You can use a ‘Copy’ task. Try something along the lines of:

task copy(type: Copy) {
    into "targetDir"
    into("subdir") {
        from configurations.compile
    }
    into("other/subdir") {
        from "some/path"
    }

For more information on the ‘Copy’ task, check the Gradle Build Language Reference.

Hello Peter,

Thank you, but I’m looking for something else: I wish to transform this:

‘compile org.slf4j:slf4j-api:1.7.5’

to

‘repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar’

More details about the layout in the Kar Layout section [1].

What I have now is:

into(‘repository’) {

from project.configurations.kar

}

into(‘resources’) {

from project.files(“src/main/resources/resources”)

}

and does just the first part. I’ve looked over option on how to achieve the directory layout but there are too many abstractions for me to figure out the proper way to do this.

[1] http://karaf.apache.org/manual/latest/developers-guide/karaf-maven-plugin-features-create-kar.html

In a nutshell, you’ll need to use an API such as ‘configurations.compile.incoming’ or ‘configurations.compile.resolvedConfiguration’ to iterate over the dependency information, and use that to configure a ‘Copy’ task accordingly.

I’ve solved it. Thank you for making my options clear. I just need to clean the code a bit and make the path portable on Windows.

task copyDepsInMavenStructure(type: Copy) {

destinationDir = project.buildDir

into(‘repository’) {

def fileRenameMap = [:];

from configurations.bundles.resolvedConfiguration.resolvedArtifacts.collect { dependency ->

def groupAsPath = dependency.moduleVersion.id.group.replace(’.’ as char, File.separatorChar)

def version = dependency.moduleVersion.id.version

def newFileName = “${groupAsPath}/${dependency.name}/${version}/${dependency.name}-${version}.${dependency.type}”

fileRenameMap[dependency.file.name] = newFileName

dependency.file

}

rename { oldName -> fileRenameMap[oldName] }

}

}