From 4a605f6270852dbcdd0ad43394e53f63665eb996 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 31 May 2017 14:14:55 +0100 Subject: [PATCH] Drop custom software component in favor of just publishing an artifact Previously, Spring Boot's Gradle plugin provided custom software components (bootJava and bootWeb) for publishing the artifacts created by the standard bootJar and bootWar tasks respectively. Providing a custom software component requires the usage of some of Gradle's internal APIs and this has now proven to be more trouble than it is worth. This commit removes the custom software component and documents how to publish the artifact created by a BootJar or BootWar task directly instead. Closes gh-9153 --- .../src/main/asciidoc/index.adoc | 1 + .../src/main/asciidoc/publishing.adoc | 10 +- .../src/main/asciidoc/reacting.adoc | 12 +- .../gradle/publishing/maven-publish.gradle | 2 +- .../boot/gradle/plugin/JavaPluginAction.java | 2 - .../boot/gradle/plugin/SpringBootPlugin.java | 15 --- .../plugin/SpringBootSoftwareComponent.java | 108 ------------------ .../boot/gradle/plugin/WarPluginAction.java | 2 - .../JavaPluginActionIntegrationTests.java | 13 --- .../WarPluginActionIntegrationTests.java | 13 --- .../JavaPluginActionIntegrationTests.gradle | 6 - .../WarPluginActionIntegrationTests.gradle | 6 - ...egrationTests-bootJarCanBePublished.gradle | 4 +- ...egrationTests-bootWarCanBePublished.gradle | 4 +- 14 files changed, 14 insertions(+), 184 deletions(-) delete mode 100644 spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/index.adoc b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/index.adoc index f1a4dccf32..d2d12ac49c 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/index.adoc +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/index.adoc @@ -43,6 +43,7 @@ In addition to this user guide, {api-documentation}[API documentation] is also a include::getting-started.adoc[] include::managing-dependencies.adoc[] include::packaging.adoc[] +include::publishing.adoc[] include::running.adoc[] include::integrating-with-actuator.adoc[] include::reacting.adoc[] \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/publishing.adoc b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/publishing.adoc index e62857e743..6ba51474bd 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/publishing.adoc +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/publishing.adoc @@ -20,12 +20,10 @@ include::../gradle/publishing/maven.gradle[tags=upload] [[publishing-your-application-maven-publish]] === Publishing with the `maven-publish` plugin -When the {java-plugin}[`java` plugin] is applied Spring Boot automatically creates a -{software-component}[software component] named `bootJava`. Similarly, when the `war` -plugin is applied, a software component named `bootWeb` is created. `bootJava` contains -the archive produced by the `bootJar` task and `bootWeb` contains the archive provided by -the `bootWar` task. The components can be used with the -{maven-publish-plugin}[`maven-publish` plugin] to publish the archive, for example: +To publish your Spring Boot jar or war, add it to the publication using the `artifact` +method on `MavenPublication`. Pass the task that produces that artifact that you wish +to publish to the `artifact` method. For example, to publish the artifact produced by the +default `bootJar` task: [source,groovy,indent=0,subs="verbatim"] ---- diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc index 34fcbe19dd..1de2476b76 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc @@ -16,13 +16,11 @@ plugin: are packaged in `BOOT-INF/lib` 2. Configures the `assemble` task to depend on the `bootJar` task. 3. Disables the `jar` task. -4. Creates a {software-component}[software component] named `bootJava` that contains the - archive produced by the `bootJar` task. -5. Creates a {boot-run-javadoc}[`BootRun`] task named `bootRun` that can be used to run +4. Creates a {boot-run-javadoc}[`BootRun`] task named `bootRun` that can be used to run your application. -6. Creates a configuration named `bootArchives` that contains the artifact produced by +5. Creates a configuration named `bootArchives` that contains the artifact produced by the `bootJar` task. -7. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`. +6. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`. @@ -36,9 +34,7 @@ When Gradle's {war-plugin}[`war` plugin] is applied to a project, the Spring Boo in the `providedRuntime` configuration will be packaged in `WEB-INF/lib-provided`. 2. Configures the `assemble` task to depend on the `bootWar` task. 3. Disables the `war` task. -4. Creates a {software-component}[software component] named `bootWeb` that contains the - archive produced by the `bootWar` task. -5. Configures the `bootArchives` configuration to contain the artifact produced by the +4. Configures the `bootArchives` configuration to contain the artifact produced by the `bootWar` task. diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/publishing/maven-publish.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/publishing/maven-publish.gradle index 61ded0c357..6a24def5df 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/publishing/maven-publish.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/publishing/maven-publish.gradle @@ -12,7 +12,7 @@ apply plugin: 'maven-publish' publishing { publications { bootJava(MavenPublication) { - from components.bootJava + artifact bootJar } } repositories { 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 7403709b05..7f08789c47 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,8 +92,6 @@ 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(project, artifact, - SpringBootPlugin.BOOT_JAVA_SOFTWARE_COMPONENT_NAME)); } private void configureBootRunTask(Project project) { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java index 532154a2e5..f96e09edc2 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java @@ -23,7 +23,6 @@ import org.gradle.api.GradleException; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; -import org.gradle.api.component.SoftwareComponent; import org.gradle.util.GradleVersion; import org.springframework.boot.gradle.dsl.SpringBootExtension; @@ -46,20 +45,6 @@ public class SpringBootPlugin implements Plugin { */ public static final String BOOT_ARCHIVES_CONFIGURATION_NAME = "bootArchives"; - /** - * The name of the {@link SoftwareComponent} for a Spring Boot Java application. - * - * @since 2.0.0 - */ - public static final String BOOT_JAVA_SOFTWARE_COMPONENT_NAME = "bootJava"; - - /** - * The name of the {@link SoftwareComponent} for a Spring Boot Web application. - * - * @since 2.0.0 - */ - public static final String BOOT_WEB_SOFTWARE_COMPONENT_NAME = "bootWeb"; - /** * The name of the default {@link BootJar} task. * 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 deleted file mode 100644 index 863eb806e1..0000000000 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootSoftwareComponent.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2012-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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; -import org.gradle.api.internal.attributes.Usages; -import org.gradle.api.internal.component.SoftwareComponentInternal; -import org.gradle.api.internal.component.UsageContext; - -/** - * {@link org.gradle.api.component.SoftwareComponent} for a Spring Boot fat jar or war. - * - * @author Andy Wilkinson - */ -final class SpringBootSoftwareComponent implements SoftwareComponentInternal { - - private final PublishArtifact artifact; - - private final 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 - public String getName() { - return this.name; - } - - @Override - public Set getUsages() { - return Collections.singleton(new BootUsageContext(this.usage, this.artifact)); - } - - private static final class BootUsageContext implements UsageContext { - - private final Usage usage; - - private final PublishArtifact artifact; - - private BootUsageContext(Usage usage, PublishArtifact artifact) { - this.usage = usage; - this.artifact = artifact; - } - - @Override - public Usage getUsage() { - return this.usage; - } - - @Override - public Set getArtifacts() { - return Collections.singleton(this.artifact); - } - - @Override - public Set getDependencies() { - return Collections.emptySet(); - } - - } - -} 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 4873f9b8da..8619bd90b1 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,8 +55,6 @@ class WarPluginAction implements PluginApplicationAction { bootWar.providedClasspath(providedRuntimeConfiguration(project)); ArchivePublishArtifact artifact = new ArchivePublishArtifact(bootWar); this.singlePublishedArtifact.addCandidate(artifact); - project.getComponents().add(new SpringBootSoftwareComponent(project, artifact, - SpringBootPlugin.BOOT_WEB_SOFTWARE_COMPONENT_NAME)); bootWar.conventionMapping("mainClass", new MainClassConvention(project, bootWar::getClasspath)); } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java index 7591d7f8e4..068a3b838d 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java @@ -66,19 +66,6 @@ public class JavaPluginActionIntegrationTests { .getOutput()).contains("bootRun exists = true"); } - @Test - public void noBootJavaSoftwareComponentWithoutJavaPluginApplied() { - assertThat(this.gradleBuild.build("componentExists", "-PcomponentName=bootJava") - .getOutput()).contains("bootJava exists = false"); - } - - @Test - public void applyingJavaPluginCreatesBootJavaSoftwareComponent() { - assertThat(this.gradleBuild - .build("componentExists", "-PcomponentName=bootJava", "-PapplyJavaPlugin") - .getOutput()).contains("bootJava exists = true"); - } - @Test public void javaCompileTasksUseUtf8Encoding() { assertThat(this.gradleBuild.build("javaCompileEncoding", "-PapplyJavaPlugin") diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java index 84aac096a2..0f3f4c6434 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java @@ -53,19 +53,6 @@ public class WarPluginActionIntegrationTests { .getOutput()).contains("bootWar exists = true"); } - @Test - public void noBootWebSoftwareComponentWithoutJavaPluginApplied() { - assertThat(this.gradleBuild.build("componentExists", "-PcomponentName=bootWeb") - .getOutput()).contains("bootWeb exists = false"); - } - - @Test - public void applyingJavaPluginCreatesBootWebSoftwareComponent() { - assertThat(this.gradleBuild - .build("componentExists", "-PcomponentName=bootWeb", "-PapplyWarPlugin") - .getOutput()).contains("bootWeb exists = true"); - } - @Test public void assembleRunsBootWarAndWarIsSkipped() { BuildResult result = this.gradleBuild.build("assemble"); diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle index 20fb8832f2..70df201a02 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle @@ -16,12 +16,6 @@ task('taskExists') { } } -task('componentExists') { - doFirst { - println "$componentName exists = ${components.findByName(componentName) != null}" - } -} - task('javaCompileEncoding') { doFirst { tasks.withType(JavaCompile) { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.gradle index 9bec2b8c13..15ca34eec4 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.gradle @@ -15,9 +15,3 @@ task('taskExists') { println "$taskName exists = ${tasks.findByName(taskName) != null}" } } - -task('componentExists') { - doFirst { - println "$componentName exists = ${components.findByName(componentName) != null}" - } -} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootJarCanBePublished.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootJarCanBePublished.gradle index fbc1cb0dba..6146899a6d 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootJarCanBePublished.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootJarCanBePublished.gradle @@ -22,8 +22,8 @@ publishing { } } publications { - mavenBootJava(MavenPublication) { - from components.bootJava + bootJava(MavenPublication) { + artifact bootJar } } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootWarCanBePublished.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootWarCanBePublished.gradle index 4c71589e0c..df3f763321 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootWarCanBePublished.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests-bootWarCanBePublished.gradle @@ -22,8 +22,8 @@ publishing { } } publications { - mavenBootWeb(MavenPublication) { - from components.bootWeb + bootWeb(MavenPublication) { + artifact bootWar } } }