|
1 | 1 | /** |
2 | | - * Sources download task for Gradle, tested in versions 4+ |
| 2 | + * Conservative sources download task - targets main classpaths only |
| 3 | + * Compatible with Gradle 4-8 |
3 | 4 | */ |
4 | 5 | task konveyorDownloadSources { |
5 | | - allprojects { project -> |
6 | | - project.getConfigurations().each { config -> |
7 | | - config.setCanBeResolved(true) |
8 | | - |
9 | | - inputs.files config |
10 | | - outputs.dir "${buildDir}/download" |
11 | | - doLast { |
12 | | - def componentIds = config.incoming.resolutionResult.allDependencies.collect { it.selected.id } |
13 | | - ArtifactResolutionResult result = dependencies.createArtifactResolutionQuery() |
14 | | - .forComponents(componentIds) |
15 | | - .withArtifacts(JvmLibrary, SourcesArtifact) |
16 | | - .execute() |
17 | | - def sourceArtifacts = [] |
18 | | - result.resolvedComponents.each { ComponentArtifactsResult component -> |
19 | | - Set<ArtifactResult> sources = component.getArtifacts(SourcesArtifact) |
20 | | - println "Found ${sources.size()} sources for ${component.id}" |
21 | | - sources.each { ArtifactResult ar -> |
22 | | - if (ar instanceof ResolvedArtifactResult) { |
23 | | - sourceArtifacts << ar.file |
| 6 | + doLast { |
| 7 | + def allSourceFiles = [] |
| 8 | + |
| 9 | + allprojects { project -> |
| 10 | + println "Processing project: ${project.name}" |
| 11 | + |
| 12 | + // Focus on main classpaths that are typically resolvable |
| 13 | + def targetConfigs = [ |
| 14 | + 'compileClasspath', |
| 15 | + 'runtimeClasspath', |
| 16 | + 'implementation', |
| 17 | + 'api' |
| 18 | + ].findAll { configName -> |
| 19 | + project.configurations.findByName(configName)?.canBeResolved ?: false |
| 20 | + } |
| 21 | + |
| 22 | + targetConfigs.each { configName -> |
| 23 | + try { |
| 24 | + def config = project.configurations.getByName(configName) |
| 25 | + println " Processing configuration: ${configName}" |
| 26 | + |
| 27 | + // Get resolved dependencies |
| 28 | + def resolvedConfig = config.resolvedConfiguration |
| 29 | + def dependencies = resolvedConfig.resolvedArtifacts |
| 30 | + |
| 31 | + // Extract module identifiers for source resolution |
| 32 | + def moduleIds = dependencies.collect { artifact -> |
| 33 | + artifact.moduleVersion.id |
| 34 | + }.unique() |
| 35 | + |
| 36 | + if (!moduleIds.isEmpty()) { |
| 37 | + println " Found ${moduleIds.size()} unique modules" |
| 38 | + |
| 39 | + // Query for sources using the dependency notation |
| 40 | + moduleIds.each { moduleId -> |
| 41 | + try { |
| 42 | + def sourceDep = project.dependencies.create( |
| 43 | + group: moduleId.group, |
| 44 | + name: moduleId.name, |
| 45 | + version: moduleId.version, |
| 46 | + classifier: 'sources' |
| 47 | + ) |
| 48 | + |
| 49 | + def sourceConfig = project.configurations.detachedConfiguration(sourceDep) |
| 50 | + sourceConfig.transitive = false |
| 51 | + |
| 52 | + def sourceFiles = sourceConfig.resolve() |
| 53 | + if (!sourceFiles.isEmpty()) { |
| 54 | + allSourceFiles.addAll(sourceFiles) |
| 55 | + println " Found sources for ${moduleId}" |
| 56 | + } |
| 57 | + } catch (Exception e) { |
| 58 | + // Sources not available for this dependency, continue |
| 59 | + println " No sources available for ${moduleId}" |
| 60 | + } |
24 | 61 | } |
25 | 62 | } |
| 63 | + } catch (Exception e) { |
| 64 | + println " Error processing ${configName}: ${e.message}" |
26 | 65 | } |
| 66 | + } |
| 67 | + } |
27 | 68 |
|
28 | | - copy { |
29 | | - from sourceArtifacts |
30 | | - into "${buildDir}/download" |
31 | | - } |
| 69 | + // Copy all found source files |
| 70 | + if (!allSourceFiles.isEmpty()) { |
| 71 | + def downloadDir = new File(buildDir, "download") |
| 72 | + downloadDir.mkdirs() |
| 73 | + |
| 74 | + copy { |
| 75 | + from allSourceFiles |
| 76 | + into downloadDir |
| 77 | + duplicatesStrategy = "exclude" |
32 | 78 | } |
| 79 | + |
| 80 | + println "Downloaded ${allSourceFiles.size()} source files to ${downloadDir}" |
| 81 | + } else { |
| 82 | + println "No source files found to download" |
33 | 83 | } |
34 | 84 | } |
35 | 85 | } |
| 86 | + |
0 commit comments