From 5f3462f3584a4666577fe0a67c86bfc83a335f83 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 31 May 2017 11:58:04 +0100 Subject: [PATCH] Use ObjectFactory rather than Usages to create Usage on Gradle 4.0 Usages has been removed in Gradle 4.0 and replaced with the use of a new ObjectFactory API that can be used to create a Usage. This commit uses reflection to access the ObjectFactory from the Project and use it to create a Usage when an attempt to use Usages fails. Closes gh-9364 --- .../boot/gradle/plugin/JavaPluginAction.java | 2 +- .../plugin/SpringBootSoftwareComponent.java | 36 ++++++++++++++++--- .../boot/gradle/plugin/WarPluginAction.java | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java index dc8ec423cf..7403709b05 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java @@ -92,7 +92,7 @@ final class JavaPluginAction implements PluginApplicationAction { private void configureArtifactPublication(Project project, BootJar bootJar) { ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootJar); this.singlePublishedArtifact.addCandidate(artifact); - project.getComponents().add(new SpringBootSoftwareComponent(artifact, + project.getComponents().add(new SpringBootSoftwareComponent(project, artifact, SpringBootPlugin.BOOT_JAVA_SOFTWARE_COMPONENT_NAME)); } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java index 83f6b5dfc1..863eb806e1 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java @@ -19,6 +19,7 @@ package org.springframework.boot.gradle.plugin; import java.util.Collections; import java.util.Set; +import org.gradle.api.Project; import org.gradle.api.artifacts.ModuleDependency; import org.gradle.api.artifacts.PublishArtifact; import org.gradle.api.attributes.Usage; @@ -37,9 +38,33 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal { private final String name; - SpringBootSoftwareComponent(PublishArtifact artifact, String name) { + private final Usage usage; + + SpringBootSoftwareComponent(Project project, PublishArtifact artifact, String name) { this.artifact = artifact; this.name = name; + this.usage = createUsage(project); + } + + private static Usage createUsage(Project project) { + try { + return Usages.usage("master"); + } + catch (Throwable ex) { + return createUsageUsingObjectFactory(project); + } + } + + private static Usage createUsageUsingObjectFactory(Project project) { + try { + Object objects = project.getClass().getMethod("getObjects").invoke(project); + return (Usage) objects.getClass() + .getMethod("named", Class.class, String.class) + .invoke(objects, Usage.class, "master"); + } + catch (Throwable ex) { + throw new RuntimeException(ex); + } } @Override @@ -49,22 +74,23 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal { @Override public Set getUsages() { - return Collections.singleton(new BootUsageContext(this.artifact)); + return Collections.singleton(new BootUsageContext(this.usage, this.artifact)); } private static final class BootUsageContext implements UsageContext { - private static final Usage USAGE = Usages.usage("master"); + private final Usage usage; private final PublishArtifact artifact; - private BootUsageContext(PublishArtifact artifact) { + private BootUsageContext(Usage usage, PublishArtifact artifact) { + this.usage = usage; this.artifact = artifact; } @Override public Usage getUsage() { - return USAGE; + return this.usage; } @Override diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java index 9ac15147bf..4873f9b8da 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java @@ -55,7 +55,7 @@ class WarPluginAction implements PluginApplicationAction { bootWar.providedClasspath(providedRuntimeConfiguration(project)); ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootWar); this.singlePublishedArtifact.addCandidate(artifact); - project.getComponents().add(new SpringBootSoftwareComponent(artifact, + project.getComponents().add(new SpringBootSoftwareComponent(project, artifact, SpringBootPlugin.BOOT_WEB_SOFTWARE_COMPONENT_NAME)); bootWar.conventionMapping("mainClass", new MainClassConvention(project, bootWar::getClasspath));