this is not that straight forward as expected. The following snippet copies the source jars for all dependencies (runtime + compile) of a java project into a specific folder:
task copySourceJars(type:Copy){
def deps = configurations.runtime.incoming.dependencies.collect{ dependency ->
dependency.artifact { artifact ->
artifact.name = dependency.name
artifact.type = 'source'
artifact.extension = 'jar'
artifact.classifier = 'sources'
}
dependency
}
from(configurations.detachedConfiguration(deps as Dependency[]).resolvedConfiguration.lenientConfiguration.getFiles(Specs.SATISFIES_ALL))
into('sourceLibs')
}
The reason we use a lenientConfiguration here is, that we don't want to fail if a source artifact cannot be resolved. There might be a more elegant way, but I havn't looked deeper into that.
hope it helps,
René
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…