|
12 | 12 | import org.gradle.api.artifacts.ComponentVariantIdentifier;
|
13 | 13 | import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
|
14 | 14 | import org.gradle.api.artifacts.dsl.ComponentMetadataHandler;
|
| 15 | +import org.gradle.api.initialization.Settings; |
| 16 | +import org.gradle.api.plugins.ExtensionAware; |
15 | 17 | import org.gradle.util.GradleVersion;
|
16 | 18 |
|
17 | 19 | import javax.annotation.Nullable;
|
|
24 | 26 |
|
25 | 27 | @SuppressWarnings("unused")
|
26 | 28 | @NonNullApi
|
27 |
| -public abstract class JavaEcosystemCapabilitiesPlugin implements Plugin<Project> { |
| 29 | +public abstract class JavaEcosystemCapabilitiesPlugin implements Plugin<ExtensionAware> { |
28 | 30 |
|
29 | 31 | private static final GradleVersion MINIMUM_SUPPORTED_VERSION = GradleVersion.version("6.0");
|
| 32 | + private static final GradleVersion MINIMUM_SUPPORTED_VERSION_SETTINGS = GradleVersion.version("6.8"); |
30 | 33 |
|
31 | 34 | /**
|
32 | 35 | * Map: Capability -> Default Module to resolve to (or 'null' if resolve to the highest version)
|
33 | 36 | */
|
34 | 37 | private final Map<String, String> standardResolutionStrategy = new HashMap<>();
|
35 | 38 |
|
36 | 39 | @Override
|
37 |
| - public void apply(Project project) { |
| 40 | + public void apply(ExtensionAware projectOrSettings) { |
38 | 41 | if (GradleVersion.current().compareTo(MINIMUM_SUPPORTED_VERSION) < 0) {
|
39 |
| - throw new IllegalStateException("Plugin requires at least Gradle 6.0"); |
| 42 | + throw new IllegalStateException("Plugin requires at least Gradle " + MINIMUM_SUPPORTED_VERSION.getVersion()); |
40 | 43 | }
|
41 | 44 |
|
42 | 45 | Set<String> allCapabilities = new TreeSet<>();
|
43 |
| - JavaEcosystemCapabilitiesExtension javaEcosystemCapabilities = |
44 |
| - project.getExtensions().create("javaEcosystemCapabilities", JavaEcosystemCapabilitiesExtension.class, allCapabilities); |
45 |
| - |
46 |
| - registerCapabilityRules(project.getDependencies().getComponents(), allCapabilities); |
47 |
| - registerComponentRules(project.getDependencies().getComponents()); |
48 |
| - |
49 |
| - project.getConfigurations().all(configuration -> |
50 |
| - defineStrategies(javaEcosystemCapabilities, configuration.getResolutionStrategy().getCapabilitiesResolution())); |
| 46 | + |
| 47 | + ComponentMetadataHandler components; |
| 48 | + if (projectOrSettings instanceof Project) { |
| 49 | + components = ((Project) projectOrSettings).getDependencies().getComponents(); |
| 50 | + } else if (projectOrSettings instanceof Settings) { |
| 51 | + if (GradleVersion.current().compareTo(MINIMUM_SUPPORTED_VERSION_SETTINGS) < 0) { |
| 52 | + throw new IllegalStateException("Using this plugin in settings.gradle requires at least Gradle " + MINIMUM_SUPPORTED_VERSION_SETTINGS.getVersion()); |
| 53 | + } |
| 54 | + //noinspection UnstableApiUsage |
| 55 | + components = ((Settings) projectOrSettings).getDependencyResolutionManagement().getComponents(); |
| 56 | + } else { |
| 57 | + throw new IllegalStateException("Cannot apply plugin to: " + projectOrSettings.getClass().getName()); |
| 58 | + } |
| 59 | + |
| 60 | + registerCapabilityRules(components, allCapabilities); |
| 61 | + registerComponentRules(components); |
| 62 | + |
| 63 | + if (projectOrSettings instanceof Project) { |
| 64 | + JavaEcosystemCapabilitiesExtension javaEcosystemCapabilities = |
| 65 | + projectOrSettings.getExtensions().create("javaEcosystemCapabilities", JavaEcosystemCapabilitiesExtension.class, allCapabilities); |
| 66 | + ((Project) projectOrSettings).getConfigurations().all(configuration -> |
| 67 | + defineStrategies(javaEcosystemCapabilities, configuration.getResolutionStrategy().getCapabilitiesResolution())); |
| 68 | + } |
51 | 69 | }
|
52 | 70 |
|
53 | 71 | private void registerCapabilityRules(ComponentMetadataHandler components, Set<String> allCapabilities) {
|
|
0 commit comments