|
21 | 21 | import org.gradle.api.artifacts.Configuration; |
22 | 22 | import org.gradle.api.artifacts.ConfigurationContainer; |
23 | 23 | import org.gradle.api.artifacts.Dependency; |
| 24 | +import org.gradle.api.artifacts.ModuleDependency; |
24 | 25 | import org.gradle.api.artifacts.ProjectDependency; |
25 | 26 | import org.gradle.api.artifacts.VersionCatalog; |
26 | 27 | import org.gradle.api.artifacts.VersionCatalogsExtension; |
|
37 | 38 | import org.gradle.api.provider.Provider; |
38 | 39 | import org.gradle.api.provider.ProviderFactory; |
39 | 40 | import org.gradle.api.tasks.SourceSet; |
| 41 | +import org.gradle.api.tasks.SourceSetContainer; |
40 | 42 | import org.gradle.api.tasks.compile.JavaCompile; |
41 | 43 | import org.gradle.api.tasks.javadoc.Javadoc; |
42 | 44 | import org.gradlex.javamodule.dependencies.internal.compile.AddSyntheticModulesToCompileClasspathAction; |
|
45 | 47 |
|
46 | 48 | import javax.inject.Inject; |
47 | 49 | import java.io.File; |
| 50 | +import java.util.Arrays; |
48 | 51 | import java.util.Collections; |
49 | 52 | import java.util.Comparator; |
50 | 53 | import java.util.HashMap; |
@@ -299,16 +302,42 @@ public void versionsFromConsistentResolution(String... versionsProvidingProjects |
299 | 302 | c.setCanBeConsumed(false); |
300 | 303 | c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, getObjects().named(Usage.class, Usage.JAVA_RUNTIME)); |
301 | 304 | }); |
302 | | - getConfigurations().all(c -> { |
303 | | - if (c != mainRuntimeClasspath) { |
| 305 | + getConfigurations().configureEach(c -> { |
| 306 | + if (c.isCanBeResolved() && c != mainRuntimeClasspath) { |
304 | 307 | c.shouldResolveConsistentlyWith(mainRuntimeClasspath); |
305 | 308 | } |
306 | 309 | }); |
307 | 310 | for (String versionsProvidingProject : versionsProvidingProjects) { |
308 | | - getDependencies().add(mainRuntimeClasspath.getName(), getDependencies().project(Collections.singletonMap("path", versionsProvidingProject))); |
| 311 | + getDependencies().add(mainRuntimeClasspath.getName(), createDependency(versionsProvidingProject)); |
309 | 312 | } |
310 | 313 | } |
311 | 314 |
|
| 315 | + public void versionsFromPlatformAndConsistentResolution(String platformProject, String... versionsProvidingProjects) { |
| 316 | + boolean platformInJavaProject = Arrays.asList(versionsProvidingProjects).contains(platformProject); |
| 317 | + getSourceSets().configureEach(sourceSet -> getConfigurations().getByName(sourceSet.getImplementationConfigurationName()).withDependencies(d -> { |
| 318 | + Dependency platformDependency = getDependencies().platform(createDependency(platformProject)); |
| 319 | + if (platformInJavaProject) { |
| 320 | + if (platformProject.startsWith(":")) { |
| 321 | + String capability = ((ProjectDependency) platformDependency).getDependencyProject().getGroup() + platformProject + "-platform"; |
| 322 | + ((ProjectDependency) platformDependency).capabilities(c -> c.requireCapability(capability)); |
| 323 | + } else if (platformDependency instanceof ModuleDependency) { |
| 324 | + String capability = platformProject + "-platform"; |
| 325 | + ((ModuleDependency) platformDependency).capabilities(c -> c.requireCapability(capability)); |
| 326 | + } |
| 327 | + } |
| 328 | + d.add(platformDependency); |
| 329 | + })); |
| 330 | + |
| 331 | + versionsFromConsistentResolution(versionsProvidingProjects); |
| 332 | + } |
| 333 | + |
| 334 | + private Dependency createDependency(String project) { |
| 335 | + boolean isProjectInBuild = project.startsWith(":"); |
| 336 | + return getDependencies().create(isProjectInBuild |
| 337 | + ? getDependencies().project(Collections.singletonMap("path", project)) |
| 338 | + : project); |
| 339 | + } |
| 340 | + |
312 | 341 | /** |
313 | 342 | * Adds support for compiling module-info.java in the given source set with the given task, |
314 | 343 | * if 'requires runtime' dependencies are used. |
@@ -389,6 +418,9 @@ private String moduleDebugInfo(String moduleName, File moduleInfoFile, File root |
389 | 418 | @Inject |
390 | 419 | protected abstract ConfigurationContainer getConfigurations(); |
391 | 420 |
|
| 421 | + @Inject |
| 422 | + protected abstract SourceSetContainer getSourceSets(); |
| 423 | + |
392 | 424 | ModuleInfoCache getModuleInfoCache() { |
393 | 425 | return moduleInfoCache; |
394 | 426 | } |
|
0 commit comments