|
| 1 | +/* |
| 2 | + * Copyright the GradleX team. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.gradlex.jvm.dependency.conflict.resolution; |
| 18 | + |
| 19 | +import org.gradle.api.artifacts.Configuration; |
| 20 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 21 | +import org.gradle.api.artifacts.Dependency; |
| 22 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 23 | +import org.gradle.api.attributes.Bundling; |
| 24 | +import org.gradle.api.attributes.Category; |
| 25 | +import org.gradle.api.attributes.LibraryElements; |
| 26 | +import org.gradle.api.attributes.Usage; |
| 27 | +import org.gradle.api.attributes.java.TargetJvmEnvironment; |
| 28 | +import org.gradle.api.model.ObjectFactory; |
| 29 | +import org.gradle.api.tasks.SourceSetContainer; |
| 30 | +import org.gradle.util.GradleVersion; |
| 31 | + |
| 32 | +import javax.inject.Inject; |
| 33 | +import java.util.Collections; |
| 34 | + |
| 35 | +import static org.gradlex.jvm.dependency.conflict.resolution.JvmDependencyConflictResolutionPlugin.INTERNAL_CONFIGURATION_NAME; |
| 36 | +import static org.gradlex.jvm.dependency.conflict.resolution.JvmDependencyConflictResolutionPlugin.MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME; |
| 37 | + |
| 38 | +public abstract class ConsistentResolution { |
| 39 | + |
| 40 | + @Inject |
| 41 | + protected abstract ObjectFactory getObjects(); |
| 42 | + |
| 43 | + @Inject |
| 44 | + protected abstract ConfigurationContainer getConfigurations(); |
| 45 | + |
| 46 | + @Inject |
| 47 | + protected abstract DependencyHandler getDependencies(); |
| 48 | + |
| 49 | + @Inject |
| 50 | + protected abstract SourceSetContainer getSourceSets(); |
| 51 | + |
| 52 | + /** |
| 53 | + * The runtime classpath of the given project always respected in version conflict detection and resolution. |
| 54 | + */ |
| 55 | + public Configuration providesVersions(String versionProvidingProject) { |
| 56 | + Configuration mainRuntimeClasspath = maybeCreateMainRuntimeClasspathConfiguration(); |
| 57 | + getDependencies().add(mainRuntimeClasspath.getName(), createDependency(versionProvidingProject)); |
| 58 | + return mainRuntimeClasspath; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A platform/BOM (<a href="https://docs.gradle.org/current/userguide/java_platform_plugin.html">Java Platform Plugin</a>) |
| 63 | + * used to provide versions not available through consistent resolution alone. |
| 64 | + * Useful if additional dependencies are needed only for tests. |
| 65 | + */ |
| 66 | + public void platform(String platform) { |
| 67 | + Configuration internal = maybeCreateInternalConfiguration(); |
| 68 | + internal.withDependencies(d -> { |
| 69 | + Dependency platformDependency = getDependencies().platform(createDependency(platform)); |
| 70 | + d.add(platformDependency); |
| 71 | + }); |
| 72 | + |
| 73 | + getSourceSets().configureEach(sourceSet -> { |
| 74 | + ConfigurationContainer configurations = getConfigurations(); |
| 75 | + configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName()).extendsFrom(internal); |
| 76 | + configurations.getByName(sourceSet.getCompileClasspathConfigurationName()).extendsFrom(internal); |
| 77 | + configurations.getByName(sourceSet.getAnnotationProcessorConfigurationName()).extendsFrom(internal); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + private Configuration maybeCreateMainRuntimeClasspathConfiguration() { |
| 82 | + Configuration existing = getConfigurations().findByName(MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME); |
| 83 | + if (existing != null) { |
| 84 | + return existing; |
| 85 | + } |
| 86 | + |
| 87 | + Configuration mainRuntimeClasspath = getConfigurations().create(MAIN_RUNTIME_CLASSPATH_CONFIGURATION_NAME, c -> { |
| 88 | + ObjectFactory objects = getObjects(); |
| 89 | + c.setCanBeResolved(true); |
| 90 | + c.setCanBeConsumed(false); |
| 91 | + c.extendsFrom(maybeCreateInternalConfiguration()); |
| 92 | + c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_RUNTIME)); |
| 93 | + c.getAttributes().attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.LIBRARY)); |
| 94 | + c.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.class, LibraryElements.JAR)); |
| 95 | + c.getAttributes().attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.class, Bundling.EXTERNAL)); |
| 96 | + if (GradleVersion.current().compareTo(GradleVersion.version("7.0")) >= 0) { |
| 97 | + c.getAttributes().attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, |
| 98 | + objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); |
| 99 | + } |
| 100 | + }); |
| 101 | + getSourceSets().configureEach(sourceSet -> { |
| 102 | + ConfigurationContainer configurations = getConfigurations(); |
| 103 | + Configuration runtime = configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName()); |
| 104 | + Configuration compile = configurations.getByName(sourceSet.getCompileClasspathConfigurationName()); |
| 105 | + Configuration processor = configurations.getByName(sourceSet.getAnnotationProcessorConfigurationName()); |
| 106 | + runtime.shouldResolveConsistentlyWith(mainRuntimeClasspath); |
| 107 | + compile.shouldResolveConsistentlyWith(runtime); |
| 108 | + processor.shouldResolveConsistentlyWith(runtime); |
| 109 | + }); |
| 110 | + return mainRuntimeClasspath; |
| 111 | + } |
| 112 | + |
| 113 | + private Configuration maybeCreateInternalConfiguration() { |
| 114 | + Configuration internal = getConfigurations().findByName(INTERNAL_CONFIGURATION_NAME); |
| 115 | + if (internal != null) { |
| 116 | + return internal; |
| 117 | + } |
| 118 | + return getConfigurations().create(INTERNAL_CONFIGURATION_NAME, c -> { |
| 119 | + c.setCanBeResolved(false); |
| 120 | + c.setCanBeConsumed(false); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private Dependency createDependency(String project) { |
| 125 | + boolean isProjectInBuild = project.startsWith(":"); |
| 126 | + return getDependencies().create(isProjectInBuild |
| 127 | + ? getDependencies().project(Collections.singletonMap("path", project)) |
| 128 | + : project); |
| 129 | + } |
| 130 | +} |
0 commit comments