Skip to content

Commit fb3796d

Browse files
committed
Merge branch '2.3.x' into 2.4.x
Closes gh-26121
2 parents 1f01270 + fcb2210 commit fb3796d

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.maven.MavenPom;
@@ -49,8 +52,6 @@
4952
* </ul>
5053
* </ul>
5154
*
52-
* <p/>
53-
*
5455
* @author Andy Wilkinson
5556
* @author Christoph Dreis
5657
* @author Mike Smithson
@@ -80,6 +81,7 @@ private void customizeMavenPublication(MavenPublication publication, Project pro
8081
customizePom(publication.getPom(), project);
8182
project.getPlugins().withType(JavaPlugin.class)
8283
.all((javaPlugin) -> customizeJavaMavenPublication(publication, project));
84+
suppressMavenOptionalFeatureWarnings(publication);
8385
}
8486

8587
private void customizePom(MavenPom pom, Project project) {
@@ -98,12 +100,35 @@ private void customizePom(MavenPom pom, Project project) {
98100
}
99101

100102
private void customizeJavaMavenPublication(MavenPublication publication, Project project) {
103+
addMavenOptionalFeature(project);
101104
publication.versionMapping((strategy) -> strategy.usage(Usage.JAVA_API, (mappingStrategy) -> mappingStrategy
102105
.fromResolutionOf(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)));
103106
publication.versionMapping((strategy) -> strategy.usage(Usage.JAVA_RUNTIME,
104107
(mappingStrategy) -> mappingStrategy.fromResolutionResult()));
105108
}
106109

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