Skip to content

Commit 69eff90

Browse files
authored
Increase Gradle compatibility (#142)
Signed-off-by: Juan Manuel Leflet Estrada <jleflete@redhat.com>
1 parent fa57c2b commit 69eff90

3 files changed

Lines changed: 176 additions & 24 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ENV M2_HOME /usr/local/apache-maven-3.9.11
3939
# Copy "download sources" gradle task. This is needed to download project sources.
4040
RUN mkdir /root/.gradle
4141
COPY ./gradle/build.gradle /usr/local/etc/task.gradle
42+
COPY ./gradle/build-v9.gradle /usr/local/etc/task-v9.gradle
4243

4344
COPY --from=jdtls-download /jdtls /jdtls/
4445
COPY --from=addon-build /root/.m2/repository/io/konveyor/tackle/java-analyzer-bundle.core/1.0.0-SNAPSHOT/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar /jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/

gradle/build-v9.gradle

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Configuration cache compatible sources download task - compatible with Gradle 8.14+
3+
* All project iteration happens at configuration time
4+
*/
5+
6+
// Collect all source files at configuration time
7+
def allProjectSourceFiles = []
8+
9+
allprojects { proj ->
10+
// Process each project during configuration phase
11+
def targetConfigs = [
12+
'compileClasspath',
13+
'runtimeClasspath',
14+
'implementation',
15+
'api'
16+
].findAll { configName ->
17+
def config = proj.configurations.findByName(configName)
18+
return config != null && config.canBeResolved
19+
}
20+
21+
targetConfigs.each { configName ->
22+
try {
23+
def config = proj.configurations.getByName(configName)
24+
25+
// Use modern incoming artifacts API
26+
def artifacts = config.incoming.artifacts
27+
def artifactResults = artifacts.artifacts
28+
29+
// Extract module identifiers for source resolution
30+
def moduleIds = artifactResults.collect { artifactResult ->
31+
def componentId = artifactResult.id.componentIdentifier
32+
if (componentId instanceof ModuleComponentIdentifier) {
33+
return [
34+
group: componentId.group,
35+
name: componentId.module,
36+
version: componentId.version
37+
]
38+
}
39+
return null
40+
}.findAll { it != null }.unique()
41+
42+
if (!moduleIds.isEmpty()) {
43+
// Create source dependencies
44+
def sourceDependencies = moduleIds.collect { moduleId ->
45+
proj.dependencies.create(
46+
"${moduleId.group}:${moduleId.name}:${moduleId.version}:sources"
47+
)
48+
}
49+
50+
if (!sourceDependencies.isEmpty()) {
51+
def sourcesConfig = proj.configurations.detachedConfiguration(
52+
sourceDependencies as Dependency[]
53+
)
54+
sourcesConfig.transitive = false
55+
56+
try {
57+
// Resolve and collect source files
58+
def sourceFiles = sourcesConfig.incoming.artifactView { view ->
59+
view.lenient(true)
60+
}.artifacts.artifacts.collect { it.file }.findAll {
61+
it != null && it.exists()
62+
}
63+
64+
if (!sourceFiles.isEmpty()) {
65+
allProjectSourceFiles.addAll(sourceFiles)
66+
println "Found ${sourceFiles.size()} source files for ${proj.name}:${configName}"
67+
}
68+
} catch (Exception e) {
69+
println "Error resolving sources for ${proj.name}:${configName}: ${e.message}"
70+
}
71+
}
72+
}
73+
} catch (Exception e) {
74+
println "Error processing ${proj.name}:${configName}: ${e.message}"
75+
}
76+
}
77+
}
78+
79+
task konveyorDownloadSources {
80+
// Store the collected files as task input
81+
def sourceFiles = allProjectSourceFiles
82+
83+
doLast {
84+
// Copy all found source files
85+
if (!sourceFiles.isEmpty()) {
86+
def downloadDir = new File(project.layout.buildDirectory.get().asFile, "downloaded-sources")
87+
downloadDir.mkdirs()
88+
89+
copy {
90+
from sourceFiles
91+
into downloadDir
92+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
93+
}
94+
95+
println "Downloaded ${sourceFiles.size()} source files to ${downloadDir}"
96+
} else {
97+
println "No source files found to download"
98+
}
99+
}
100+
}

gradle/build.gradle

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,86 @@
11
/**
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
34
*/
45
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+
}
2461
}
2562
}
63+
} catch (Exception e) {
64+
println " Error processing ${configName}: ${e.message}"
2665
}
66+
}
67+
}
2768

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"
3278
}
79+
80+
println "Downloaded ${allSourceFiles.size()} source files to ${downloadDir}"
81+
} else {
82+
println "No source files found to download"
3383
}
3484
}
3585
}
86+

0 commit comments

Comments
 (0)