Skip to content

Instantly share code, notes, and snippets.

@SNWCreations
Created October 6, 2024 07:21
Show Gist options
  • Select an option

  • Save SNWCreations/1cf3ace9bc15ce2b2d4e1170dd3c5095 to your computer and use it in GitHub Desktop.

Select an option

Save SNWCreations/1cf3ace9bc15ce2b2d4e1170dd3c5095 to your computer and use it in GitHub Desktop.
Get files of all dependencies in your Java project which is using Gradle
// The code below is tested with Gradle 8.5, it works well in my project.
// This Gist is licensed under MIT license. Feel free to copy it and use them.
static Collection<File> getAllDependencyFiles(Project project) {
var container = project.configurations
List<Configuration> toScan = [
container.compileClasspath,
container.runtimeClasspath
]
Set<File> result = new HashSet<>()
for (final def conf in toScan) {
var collected = getAllDependencyFilesFromConf(conf)
result.addAll(collected)
}
return result
}
static Collection<File> getAllDependencyFilesFromConf(Configuration conf) {
Set<File> result = new HashSet<>()
for (final def dep in conf.allDependencies) {
Collection<File> collected
if (dep instanceof ProjectDependency) {
var depProj = dep.dependencyProject
collected = getAllDependencyFiles(depProj)
} else {
collected = conf.files(dep)
}
result.addAll(collected)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment