|
1 | 1 | package io.spring.gradle.convention; |
2 | 2 |
|
3 | | -import org.gradle.api.Action; |
| 3 | + |
| 4 | +import io.spring.gradle.propdeps.PropDepsPlugin; |
4 | 5 | import org.gradle.api.Plugin; |
5 | 6 | import org.gradle.api.Project; |
6 | | -import org.gradle.api.artifacts.Configuration; |
| 7 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 8 | +import org.gradle.api.plugins.JavaPlugin; |
| 9 | +import org.gradle.api.plugins.JavaTestFixturesPlugin; |
| 10 | +import org.gradle.api.plugins.PluginContainer; |
| 11 | +import org.gradle.api.publish.PublishingExtension; |
| 12 | +import org.gradle.api.publish.maven.MavenPublication; |
| 13 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; |
7 | 14 |
|
8 | 15 | /** |
9 | | - * https://github.com/gradle/gradle/issues/7576#issuecomment-434637595 |
| 16 | + * Creates a Management configuration that is appropriate for adding a platform to that is not exposed externally. If |
| 17 | + * the JavaPlugin is applied, the compileClasspath, runtimeClasspath, testCompileClasspath, and testRuntimeClasspath |
| 18 | + * will extend from it. |
10 | 19 | * @author Rob Winch |
11 | 20 | */ |
12 | 21 | public class ManagementConfigurationPlugin implements Plugin<Project> { |
| 22 | + |
| 23 | + public static final String MANAGEMENT_CONFIGURATION_NAME = "management"; |
| 24 | + |
13 | 25 | @Override |
14 | 26 | public void apply(Project project) { |
15 | | - Configuration management = project.getConfigurations() |
16 | | - .create("management", new Action<Configuration>() { |
17 | | - @Override |
18 | | - public void execute(Configuration configuration) { |
19 | | - configuration.setCanBeResolved(false); |
20 | | - configuration.setCanBeConsumed(false); |
21 | | - configuration.setDescription("Used for setting Gradle constraints that impact all configurations that can be resolved"); |
22 | | - } |
| 27 | + ConfigurationContainer configurations = project.getConfigurations(); |
| 28 | + configurations.create(MANAGEMENT_CONFIGURATION_NAME, (management) -> { |
| 29 | + management.setVisible(false); |
| 30 | + management.setCanBeConsumed(false); |
| 31 | + management.setCanBeResolved(false); |
| 32 | + |
| 33 | + PluginContainer plugins = project.getPlugins(); |
| 34 | + plugins.withType(JavaPlugin.class, (javaPlugin) -> { |
| 35 | + configurations.getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME).extendsFrom(management); |
| 36 | + configurations.getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME).extendsFrom(management); |
| 37 | + configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(management); |
| 38 | + configurations.getByName(JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME).extendsFrom(management); |
| 39 | + configurations.getByName(JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME).extendsFrom(management); |
| 40 | + configurations.getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(management); |
23 | 41 | }); |
24 | | - project.getConfigurations().all(new Action<Configuration>() { |
25 | | - @Override |
26 | | - public void execute(Configuration configuration) { |
27 | | - if (configuration.isCanBeResolved()) { |
28 | | - configuration.extendsFrom(management); |
29 | | - } |
30 | | - } |
| 42 | + plugins.withType(JavaTestFixturesPlugin.class, (javaTestFixturesPlugin) -> { |
| 43 | + configurations.getByName("testFixturesCompileClasspath").extendsFrom(management); |
| 44 | + configurations.getByName("testFixturesRuntimeClasspath").extendsFrom(management); |
| 45 | + }); |
| 46 | + plugins.withType(MavenPublishPlugin.class, (mavenPublish) -> { |
| 47 | + PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); |
| 48 | + publishing.getPublications().withType(MavenPublication.class, (mavenPublication -> { |
| 49 | + mavenPublication.versionMapping((versions) -> |
| 50 | + versions.allVariants(versionMapping -> versionMapping.fromResolutionResult()) |
| 51 | + ); |
| 52 | + })); |
| 53 | + }); |
| 54 | + plugins.withType(PropDepsPlugin.class, (propDepsPlugin -> { |
| 55 | + configurations.getByName("optional").extendsFrom(management); |
| 56 | + configurations.getByName("provided").extendsFrom(management); |
| 57 | + })); |
31 | 58 | }); |
32 | 59 | } |
33 | 60 | } |
| 61 | + |
0 commit comments