|
| 1 | +import groovy.json.JsonOutput |
| 2 | + |
| 3 | +gradle.projectsLoaded { |
| 4 | + def indentationProp = gradle.startParameter.projectProperties['indentation'] |
| 5 | + def indentation = (indentationProp != null) ? indentationProp.toInteger() : 2 |
| 6 | + def outputDirPath = gradle.startParameter.projectProperties['outputDir'] |
| 7 | + def outputDir = outputDirPath ? new File(outputDirPath) : new File(gradle.rootProject.buildDir, "coordinates") |
| 8 | + |
| 9 | + gradle.rootProject.tasks.register("printCoordinates") { |
| 10 | + doLast { |
| 11 | + if (!outputDir.exists()) { |
| 12 | + outputDir.mkdirs() |
| 13 | + } |
| 14 | + |
| 15 | + def textFile = new File(outputDir, "output.txt") |
| 16 | + def jsonFile = new File(outputDir, "output.json") |
| 17 | + textFile.text = "" |
| 18 | + jsonFile.text = "" |
| 19 | + |
| 20 | + def mavenStyleOutput = new StringBuilder() |
| 21 | + |
| 22 | + def printMavenStyle |
| 23 | + printMavenStyle = { Project project, int depth = 0 -> |
| 24 | + def indent = " " * (depth * indentation) |
| 25 | + def line = "${indent}${project.group}:${project.name}:${project.version}" |
| 26 | + mavenStyleOutput.append(line).append("\n") |
| 27 | + project.childProjects.values().each { child -> |
| 28 | + printMavenStyle(child, depth + 1) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + printMavenStyle(gradle.rootProject) |
| 33 | + textFile.text = mavenStyleOutput.toString() |
| 34 | + |
| 35 | + def toJsonMap |
| 36 | + toJsonMap = { Project project, int depth = 0 -> |
| 37 | + def map = [ |
| 38 | + depth : depth, |
| 39 | + groupId : project.group?.toString(), |
| 40 | + artifactId: project.name, |
| 41 | + version : project.version?.toString() |
| 42 | + ] |
| 43 | + if (!project.childProjects.isEmpty()) { |
| 44 | + map.submodules = project.childProjects.values().collect { |
| 45 | + toJsonMap(it, depth + 1) |
| 46 | + } |
| 47 | + } |
| 48 | + return map |
| 49 | + } |
| 50 | + |
| 51 | + def jsonModel = toJsonMap(gradle.rootProject) |
| 52 | + def jsonString = JsonOutput.prettyPrint(JsonOutput.toJson(jsonModel)) |
| 53 | + jsonFile.text = jsonString |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments