Skip to content

Commit 3b1a207

Browse files
committed
add unpackAllDependencySources gradle task
1 parent 305e7a7 commit 3b1a207

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

buildSrc/src/main/kotlin/framefork.java.gradle.kts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,55 @@ configurations.all {
230230
)
231231
}
232232
}
233+
234+
// This is the custom task to unpack the source JARs
235+
tasks.register("unpackAllDependencySources") {
236+
group = "Build Setup"
237+
description = "Unpacks the source JAR for each dependency into build/dependencies/"
238+
239+
doLast {
240+
// Define the target directory for all unpacked sources and ensure it's clean.
241+
val outputDir = project.layout.buildDirectory.dir("dependencies")
242+
delete(outputDir)
243+
mkdir(outputDir)
244+
245+
// 1. Get the component identifiers for all dependencies in the 'runtimeClasspath' configuration.
246+
// This gives us a list of all libraries we need to find sources for.
247+
val componentIds: List<ComponentIdentifier> = configurations.runtimeClasspath.get()
248+
.incoming
249+
.resolutionResult
250+
.allComponents
251+
.map { it.id }
252+
253+
// 2. Create and execute an artifact query.
254+
// We ask Gradle to find all artifacts of type 'SourcesArtifact' for Java libraries.
255+
val result = dependencies.createArtifactResolutionQuery()
256+
.forComponents(componentIds)
257+
.withArtifacts(JvmLibrary::class.java, SourcesArtifact::class.java)
258+
.execute()
259+
260+
// 3. Iterate over the results and unpack each source JAR found.
261+
println("Unpacking dependency sources...")
262+
result.resolvedComponents.forEach { component ->
263+
component.getArtifacts(SourcesArtifact::class.java).forEach { artifact ->
264+
// Ensure the artifact was successfully resolved and is the type we expect.
265+
if (artifact is ResolvedArtifactResult) {
266+
val sourceJarFile = artifact.file
267+
268+
// Create a unique directory name from the source JAR's filename.
269+
// e.g., 'guava-31.1-jre-sources.jar' -> 'guava-31.1-jre'
270+
val artifactDirName = sourceJarFile.name.removeSuffix("-sources.jar")
271+
val targetDir = outputDir.get().dir(artifactDirName)
272+
273+
// Perform the unzip operation.
274+
copy {
275+
from(zipTree(sourceJarFile))
276+
into(targetDir)
277+
}
278+
println(" -> Unpacked ${sourceJarFile.name} to ${targetDir}")
279+
}
280+
}
281+
}
282+
println("Finished unpacking all dependency sources.")
283+
}
284+
}

0 commit comments

Comments
 (0)