Commit 5f3462f3 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 80ee2a8c
...@@ -92,7 +92,7 @@ final class JavaPluginAction implements PluginApplicationAction { ...@@ -92,7 +92,7 @@ final class JavaPluginAction implements PluginApplicationAction {
private void configureArtifactPublication(Project project, BootJar bootJar) { private void configureArtifactPublication(Project project, BootJar bootJar) {
ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootJar); ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootJar);
this.singlePublishedArtifact.addCandidate(artifact); this.singlePublishedArtifact.addCandidate(artifact);
project.getComponents().add(new SpringBootSoftwareComponent(artifact, project.getComponents().add(new SpringBootSoftwareComponent(project, artifact,
SpringBootPlugin.BOOT_JAVA_SOFTWARE_COMPONENT_NAME)); SpringBootPlugin.BOOT_JAVA_SOFTWARE_COMPONENT_NAME));
} }
......
...@@ -19,6 +19,7 @@ package org.springframework.boot.gradle.plugin; ...@@ -19,6 +19,7 @@ package org.springframework.boot.gradle.plugin;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ModuleDependency; import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.PublishArtifact; import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.attributes.Usage; import org.gradle.api.attributes.Usage;
...@@ -37,9 +38,33 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal { ...@@ -37,9 +38,33 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal {
private final String name; private final String name;
SpringBootSoftwareComponent(PublishArtifact artifact, String name) { private final Usage usage;
SpringBootSoftwareComponent(Project project, PublishArtifact artifact, String name) {
this.artifact = artifact; this.artifact = artifact;
this.name = name; 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 @Override
...@@ -49,22 +74,23 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal { ...@@ -49,22 +74,23 @@ final class SpringBootSoftwareComponent implements SoftwareComponentInternal {
@Override @Override
public Set<UsageContext> getUsages() { public Set<UsageContext> 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 class BootUsageContext implements UsageContext {
private static final Usage USAGE = Usages.usage("master"); private final Usage usage;
private final PublishArtifact artifact; private final PublishArtifact artifact;
private BootUsageContext(PublishArtifact artifact) { private BootUsageContext(Usage usage, PublishArtifact artifact) {
this.usage = usage;
this.artifact = artifact; this.artifact = artifact;
} }
@Override @Override
public Usage getUsage() { public Usage getUsage() {
return USAGE; return this.usage;
} }
@Override @Override
......
...@@ -55,7 +55,7 @@ class WarPluginAction implements PluginApplicationAction { ...@@ -55,7 +55,7 @@ class WarPluginAction implements PluginApplicationAction {
bootWar.providedClasspath(providedRuntimeConfiguration(project)); bootWar.providedClasspath(providedRuntimeConfiguration(project));
ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootWar); ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootWar);
this.singlePublishedArtifact.addCandidate(artifact); this.singlePublishedArtifact.addCandidate(artifact);
project.getComponents().add(new SpringBootSoftwareComponent(artifact, project.getComponents().add(new SpringBootSoftwareComponent(project, artifact,
SpringBootPlugin.BOOT_WEB_SOFTWARE_COMPONENT_NAME)); SpringBootPlugin.BOOT_WEB_SOFTWARE_COMPONENT_NAME));
bootWar.conventionMapping("mainClass", bootWar.conventionMapping("mainClass",
new MainClassConvention(project, bootWar::getClasspath)); new MainClassConvention(project, bootWar::getClasspath));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment