diff --git a/buildSrc/src/main/java/org/springframework/boot/build/EclipseConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/EclipseConventions.java index f692b3e70f0..576e20e23d1 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/EclipseConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/EclipseConventions.java @@ -18,6 +18,7 @@ package org.springframework.boot.build; import org.gradle.api.Project; import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.tasks.TaskProvider; import org.gradle.plugins.ide.api.XmlFileContentMerger; import org.gradle.plugins.ide.eclipse.EclipsePlugin; import org.gradle.plugins.ide.eclipse.model.Classpath; @@ -35,12 +36,27 @@ import org.gradle.plugins.ide.eclipse.model.Library; class EclipseConventions { void apply(Project project) { - project.getPlugins() - .withType(EclipsePlugin.class, - (eclipse) -> project.getPlugins().withType(JavaBasePlugin.class, (javaBase) -> { - EclipseModel eclipseModel = project.getExtensions().getByType(EclipseModel.class); - eclipseModel.classpath(this::configureClasspath); - })); + project.getPlugins().withType(EclipsePlugin.class, (eclipse) -> configure(project, eclipse)); + } + + private void configure(Project project, EclipsePlugin eclipse) { + TaskProvider synchronizeResourceSettings = registerEclipseSynchronizeResourceSettings(project); + project.getPlugins().withType(JavaBasePlugin.class, (javaBase) -> { + EclipseModel model = project.getExtensions().getByType(EclipseModel.class); + model.synchronizationTasks(synchronizeResourceSettings); + model.classpath(this::configureClasspath); + }); + } + + private TaskProvider registerEclipseSynchronizeResourceSettings(Project project) { + TaskProvider eclipseSynchronizateResource = project.getTasks() + .register("eclipseSynchronizateResourceSettings", EclipseSynchronizeResourceSettings.class); + eclipseSynchronizateResource.configure((task) -> { + task.setDescription("Synchronizate the Eclipse resource settings file from Buildship."); + task.setOutputFile(project.file(".settings/org.eclipse.core.resources.prefs")); + task.setInputFile(project.file(".settings/org.eclipse.core.resources.prefs")); + }); + return eclipseSynchronizateResource; } private void configureClasspath(EclipseClasspath classpath) { diff --git a/buildSrc/src/main/java/org/springframework/boot/build/EclipseSynchronizeResourceSettings.java b/buildSrc/src/main/java/org/springframework/boot/build/EclipseSynchronizeResourceSettings.java new file mode 100644 index 00000000000..5db9a73b07f --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/EclipseSynchronizeResourceSettings.java @@ -0,0 +1,56 @@ +/* + * Copyright 2025 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.Properties; + +import org.gradle.api.Task; +import org.gradle.api.internal.PropertiesTransformer; +import org.gradle.plugins.ide.api.PropertiesGeneratorTask; + +import org.springframework.boot.build.EclipseSynchronizeResourceSettings.Configuration; + +/** + * {@link Task} to synchronize Eclipse resource settings. + * + * @author Phillip Webb + */ +public abstract class EclipseSynchronizeResourceSettings extends PropertiesGeneratorTask { + + @Override + protected Configuration create() { + return new Configuration(getTransformer()); + } + + @Override + protected void configure(Configuration configuration) { + } + + public static class Configuration extends EmptyPropertiesPersistableConfigurationObject { + + Configuration(PropertiesTransformer transformer) { + super(transformer); + } + + @Override + protected void store(Properties properties) { + properties.put("encoding/", "UTF-8"); + } + + } + +} diff --git a/buildSrc/src/main/java/org/springframework/boot/build/EmptyPropertiesPersistableConfigurationObject.java b/buildSrc/src/main/java/org/springframework/boot/build/EmptyPropertiesPersistableConfigurationObject.java new file mode 100644 index 00000000000..54f18611296 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/EmptyPropertiesPersistableConfigurationObject.java @@ -0,0 +1,57 @@ +/* + * Copyright 2025 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.io.ByteArrayInputStream; +import java.util.Properties; + +import org.gradle.api.internal.PropertiesTransformer; +import org.gradle.internal.UncheckedException; +import org.gradle.plugins.ide.internal.generator.PropertiesPersistableConfigurationObject; + +/** + * Base class for {@link PropertiesPersistableConfigurationObject} instances start empty + * and have no default resource. + * + * @author Phillip Webb + */ +abstract class EmptyPropertiesPersistableConfigurationObject extends PropertiesPersistableConfigurationObject { + + EmptyPropertiesPersistableConfigurationObject(PropertiesTransformer transformer) { + super(transformer); + } + + @Override + protected String getDefaultResourceName() { + return null; + } + + @Override + public void loadDefaults() { + try { + load(new ByteArrayInputStream(new byte[0])); + } + catch (Exception ex) { + throw UncheckedException.throwAsUncheckedException(ex); + } + } + + @Override + protected void load(Properties properties) { + } + +}