Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.gradle.api.Project
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ComponentSelector
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentSelector
import org.gradle.api.artifacts.result.DependencyResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.artifacts.result.UnresolvedDependencyResult
import com.google.common.io.Files
import groovy.json.JsonOutput
task dependencies2Json {
group = "reporting"
doLast {
def root = new HashMap<String, Object>()
for (def configuration : project.getConfigurations()) {
if (configuration.isCanBeResolved()) {
root[configuration.getName()] = toStrMap(
configuration.getIncoming()
.getResolutionResult()
.getRoot()
.getDependencies()
)
}
}
def outFile = new File(project.getRootDir(), "dependencies.json")
Files.createParentDirs(outFile)
outFile.write(JsonOutput.toJson(root))
}
}
private static HashMap toStrMap(Set<? extends DependencyResult> dependencies) {
HashMap map = new HashMap()
for (DependencyResult d : dependencies) {
if (d instanceof UnresolvedDependencyResult) {
map[getName(d)] = new HashMap()
} else if (d instanceof ResolvedDependencyResult) {
map[getName(d)] = toStrMap(d.getSelected().getDependencies())
}
}
return map
}
private static String getName(DependencyResult dependency) {
ComponentSelector requested = dependency.getRequested()
if (dependency instanceof ResolvedDependencyResult) {
ComponentIdentifier selected = dependency.getSelected().getId()
if (requested.matchesStrictly(selected)) {
return requested.getDisplayName()
}
if (requested instanceof ModuleComponentSelector && selected instanceof ModuleComponentIdentifier) {
if (requested.getGroup() == selected.getGroup() &&
requested.getModule() == selected.getModule() &&
requested.getVersionConstraint().getPreferredVersion() != selected.getVersion()) {
return requested.getDisplayName() + " -> " + selected.getVersion()
}
}
return requested.getDisplayName() + " -> " + selected.getDisplayName()
} else if (dependency instanceof UnresolvedDependencyResult) {
ComponentSelector attempted = dependency.getAttempted()
if (requested == attempted) {
return requested.getDisplayName()
}
if (requested instanceof ModuleComponentSelector && attempted instanceof ModuleComponentSelector) {
if (requested.getGroup() == attempted.getGroup() &&
requested.getModule() == attempted.getModule() &&
requested.getVersionConstraint() != attempted.getVersionConstraint()) {
return requested.getDisplayName() + " -> " + attempted.getVersionConstraint().getPreferredVersion()
}
}
return requested.getDisplayName() + " -> " + attempted.getDisplayName()
}
return ""
}
import com.google.common.io.Files
import groovy.json.JsonOutput
def TARGET_CONFIGURATIONS = ['releaseCompileClasspath']
def OUTPUT_FILENAME = 'dependencies.json'
task dependencies2Json {
group = "reporting"
doLast {
def rootNode = new HashMap<String, Object>()
for (def configuration : project.getConfigurations()) {
if (configuration.isCanBeResolved() && configuration.name in TARGET_CONFIGURATIONS) {
def configurationNode = new HashMap<String, String>()
configuration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.each {
configurationNode["${it.moduleGroup}:${it.moduleName}"] = it.moduleVersion
}
rootNode[configuration.name] = configurationNode
}
}
def outFile = new File(project.getRootDir(), OUTPUT_FILENAME)
Files.createParentDirs(outFile)
outFile.write(JsonOutput.toJson(rootNode))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.