Skip to content

Instantly share code, notes, and snippets.

@welsh
Created October 26, 2014 16:38
Show Gist options
  • Save welsh/7c167fcbeb1dda8426c1 to your computer and use it in GitHub Desktop.
Save welsh/7c167fcbeb1dda8426c1 to your computer and use it in GitHub Desktop.
SplitCopyDependenciesPlugin v2
import java.util.regex.Pattern
apply plugin : SplitCopyDependenciesPlugin
class SplitCopyDependenciesPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("splitCopyDependencies", SplitCopyDependenciesPluginExtension)
project.task('splitCopyDependencies') << {
def config = project.splitCopyDependencies
def ignoreNonJars = config.ignoreNonJars
def artifactGroups = [:]
def defaultLib = []
println ""
println "Build Config:"
config.groupConfigs.each {
println "\t ${it.artifactGroup} into build/${it.outputDir}"
artifactGroups.put(it.artifactGroup, [])
}
println "\t default into build/${config.defaultDir}"
println ""
println "Building..."
def pattern = Pattern.quote(System.getProperty("file.separator"))
project.configurations.compile.each { File file ->
if(!ignoreNonJars || (ignoreNonJars && file.absolutePath.endsWith(".jar"))) {
project.copy {
from file.absolutePath
def groupIdSplit = file.absolutePath.split(pattern)
def groupId = groupIdSplit[groupIdSplit.length - 5]
boolean found = false;
config.groupConfigs.each {
if(!found) {
if((it.exactMatch && groupId.equals(it.artifactGroup)) || (!it.exactMatch && groupId.contains(it.artifactGroup))) {
artifactGroups.get(it.artifactGroup).add(file)
into "build/${it.outputDir}"
found = true;
}
}
}
if(!found) {
defaultLib.add(file)
into "build/${config.defaultDir}"
}
}
} else {
println "\t Ignoring non-xml file. (${file.name})"
}
}
println "Built."
config.groupConfigs.each {
println ""
println "build/${it.outputDir} Contents:"
artifactGroups.get(it.artifactGroup).each { File file -> println "\t" + file.name }
}
println ""
println "build/${config.defaultDir} Contents:"
defaultLib.each { File file -> println "\t" + file.name }
}
}
}
class SplitCopyDependenciesPluginExtension {
List<Map<String, Object>> groupConfigs
String defaultDir
boolean ignoreNonJars
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment