Skip to content

Commit b4c9bb0

Browse files
committed
Merge branch '2.4.x'
Closes gh-26122
2 parents bf8051c + fb3796d commit b4c9bb0

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

buildSrc/src/main/java/org/springframework/boot/build/MavenPublishingConventions.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import org.apache.maven.artifact.repository.MavenArtifactRepository;
2020
import org.gradle.api.Project;
2121
import org.gradle.api.attributes.Usage;
22+
import org.gradle.api.component.AdhocComponentWithVariants;
23+
import org.gradle.api.component.ConfigurationVariantDetails;
2224
import org.gradle.api.plugins.JavaPlugin;
25+
import org.gradle.api.plugins.JavaPluginConvention;
2326
import org.gradle.api.plugins.JavaPluginExtension;
2427
import org.gradle.api.publish.PublishingExtension;
2528
import org.gradle.api.publish.VariantVersionMappingStrategy;
@@ -50,8 +53,6 @@
5053
* </ul>
5154
* </ul>
5255
*
53-
* <p/>
54-
*
5556
* @author Andy Wilkinson
5657
* @author Christoph Dreis
5758
* @author Mike Smithson
@@ -79,7 +80,9 @@ void apply(Project project) {
7980

8081
private void customizeMavenPublication(MavenPublication publication, Project project) {
8182
customizePom(publication.getPom(), project);
82-
project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> customizeJavaMavenPublication(publication));
83+
project.getPlugins().withType(JavaPlugin.class)
84+
.all((javaPlugin) -> customizeJavaMavenPublication(publication, project));
85+
suppressMavenOptionalFeatureWarnings(publication);
8386
}
8487

8588
private void customizePom(MavenPom pom, Project project) {
@@ -97,13 +100,36 @@ private void customizePom(MavenPom pom, Project project) {
97100
}
98101
}
99102

100-
private void customizeJavaMavenPublication(MavenPublication publication) {
103+
private void customizeJavaMavenPublication(MavenPublication publication, Project project) {
104+
addMavenOptionalFeature(project);
101105
publication.versionMapping((strategy) -> strategy.usage(Usage.JAVA_API, (mappingStrategy) -> mappingStrategy
102106
.fromResolutionOf(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)));
103107
publication.versionMapping(
104108
(strategy) -> strategy.usage(Usage.JAVA_RUNTIME, VariantVersionMappingStrategy::fromResolutionResult));
105109
}
106110

111+
/**
112+
* Add a feature that allows maven plugins to declare optional dependencies that
113+
* appear in the POM. This is required to make m2e in Eclipse happy.
114+
* @param project the project to add the feature to
115+
*/
116+
private void addMavenOptionalFeature(Project project) {
117+
JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class);
118+
JavaPluginConvention convention = project.getConvention().getPlugin(JavaPluginConvention.class);
119+
extension.registerFeature("mavenOptional",
120+
(feature) -> feature.usingSourceSet(convention.getSourceSets().getByName("main")));
121+
AdhocComponentWithVariants javaComponent = (AdhocComponentWithVariants) project.getComponents()
122+
.findByName("java");
123+
javaComponent.addVariantsFromConfiguration(
124+
project.getConfigurations().findByName("mavenOptionalRuntimeElements"),
125+
ConfigurationVariantDetails::mapToOptional);
126+
}
127+
128+
private void suppressMavenOptionalFeatureWarnings(MavenPublication publication) {
129+
publication.suppressPomMetadataWarningsFor("mavenOptionalApiElements");
130+
publication.suppressPomMetadataWarningsFor("mavenOptionalRuntimeElements");
131+
}
132+
107133
private void customizeOrganization(MavenPomOrganization organization) {
108134
organization.getName().set("Pivotal Software, Inc.");
109135
organization.getUrl().set("https://spring.io");

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies {
3333
intTestImplementation("org.testcontainers:testcontainers")
3434
intTestImplementation("org.testcontainers:junit-jupiter")
3535

36-
optional("org.apache.maven.plugins:maven-shade-plugin")
36+
mavenOptionalImplementation("org.apache.maven.plugins:maven-shade-plugin")
3737

3838
runtimeOnly("org.sonatype.plexus:plexus-build-api")
3939

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.maven;
18+
19+
import java.io.File;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.Arrays;
22+
import java.util.Comparator;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.util.FileCopyUtils;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests to check that our plugin works well with Eclipse m2e.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
public class EclipseM2eIntegrationTests {
36+
37+
@Test // gh-21992
38+
void pluginPomIncludesOptionalShadeDependency() throws Exception {
39+
SpringBootDependenciesBom bom = new SpringBootDependenciesBom();
40+
String version = bom.get("version");
41+
File repository = new File("build/int-test-maven-repository");
42+
File pluginDirectory = new File(repository, "org/springframework/boot/spring-boot-maven-plugin/" + version);
43+
File[] pomFiles = pluginDirectory.listFiles(this::isPomFile);
44+
Arrays.sort(pomFiles, Comparator.comparing(File::getName));
45+
File pomFile = pomFiles[pomFiles.length - 1];
46+
String pomContent = new String(FileCopyUtils.copyToByteArray(pomFile), StandardCharsets.UTF_8);
47+
assertThat(pomContent).contains("maven-shade-plugin");
48+
}
49+
50+
private boolean isPomFile(File file) {
51+
return file.getName().endsWith(".pom");
52+
}
53+
54+
}

0 commit comments

Comments
 (0)