From e0b9ec2bc24c8de19c3b782a21755f6998c1776e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 9 Apr 2023 15:42:44 -0700 Subject: [PATCH] Fix Eclipse WTP facet version Add `WarConventions` to fix the facet version used by Eclipse WTP --- .../boot/build/ConventionsPlugin.java | 1 + .../boot/build/WarConventions.java | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 buildSrc/src/main/java/org/springframework/boot/build/WarConventions.java diff --git a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java index e6c02690e7..d98deb7b69 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java @@ -47,6 +47,7 @@ public class ConventionsPlugin implements Plugin { new MavenPublishingConventions().apply(project); new AsciidoctorConventions().apply(project); new KotlinConventions().apply(project); + new WarConventions().apply(project); } } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/WarConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/WarConventions.java new file mode 100644 index 0000000000..a58448e806 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/WarConventions.java @@ -0,0 +1,60 @@ +/* + * Copyright 2023 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 + * + * https://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.build; + +import java.util.ArrayList; +import java.util.List; + +import org.gradle.api.JavaVersion; +import org.gradle.api.Project; +import org.gradle.api.internal.IConventionAware; +import org.gradle.api.plugins.JavaPluginExtension; +import org.gradle.plugins.ide.eclipse.EclipseWtpPlugin; +import org.gradle.plugins.ide.eclipse.model.EclipseModel; +import org.gradle.plugins.ide.eclipse.model.Facet; + +/** + * Conventions that are applied in the presence of the {WarPlugin}. When the plugin is + * applied: + * + * + * @author Phillip Webb + */ +public class WarConventions { + + void apply(Project project) { + project.getPlugins().withType(EclipseWtpPlugin.class, (wtp) -> { + project.getTasks().getByName(EclipseWtpPlugin.ECLIPSE_WTP_FACET_TASK_NAME).doFirst((task) -> { + EclipseModel eclipseModel = project.getExtensions().getByType(EclipseModel.class); + ((IConventionAware) eclipseModel.getWtp().getFacet()).getConventionMapping() + .map("facets", () -> getFacets(project)); + }); + }); + } + + private List getFacets(Project project) { + JavaVersion javaVersion = project.getExtensions().getByType(JavaPluginExtension.class).getSourceCompatibility(); + List facets = new ArrayList<>(); + facets.add(new Facet(Facet.FacetType.fixed, "jst.web", null)); + facets.add(new Facet(Facet.FacetType.installed, "jst.web", "5.0")); + facets.add(new Facet(Facet.FacetType.installed, "jst.java", javaVersion.toString())); + return facets; + } + +}