From 61d4121b7f929e512afd43c24b8d99ccebecc4db Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Tue, 5 Mar 2024 20:01:31 +0200 Subject: [PATCH 1/4] Make excludes configurable via property --- .../springframework/boot/maven/FilterableDependency.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java index 2b4ead934f2a..b8d119124ed3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java @@ -68,4 +68,12 @@ void setClassifier(String classifier) { this.classifier = classifier; } + // Maven looks for this method if giving excludes as property + // e.g. -Dspring-boot.excludes=foo:bar,foo:baz + public void set(String propertyInput) { + String[] parts = propertyInput.split(":"); + groupId = parts[0]; + artifactId = parts[1]; + } + } From ee5a7f6ccadc667b6d5e9cdd4c7bc40aaf3cf77f Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Tue, 5 Mar 2024 20:16:19 +0200 Subject: [PATCH 2/4] Moved the method to a public class to pass style checks --- .../main/java/org/springframework/boot/maven/Exclude.java | 8 ++++++++ .../springframework/boot/maven/FilterableDependency.java | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java index abacf74a3519..480285ab15b2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java @@ -24,4 +24,12 @@ */ public class Exclude extends FilterableDependency { + // Maven looks for this public method if giving excludes as property + // e.g. -Dspring-boot.excludes=foo:bar,foo:baz + public void set(String propertyInput) { + String[] parts = propertyInput.split(":"); + setGroupId(parts[0]); + setArtifactId(parts[1]); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java index b8d119124ed3..2b4ead934f2a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/FilterableDependency.java @@ -68,12 +68,4 @@ void setClassifier(String classifier) { this.classifier = classifier; } - // Maven looks for this method if giving excludes as property - // e.g. -Dspring-boot.excludes=foo:bar,foo:baz - public void set(String propertyInput) { - String[] parts = propertyInput.split(":"); - groupId = parts[0]; - artifactId = parts[1]; - } - } From edc0eecaae35b3e6c60c45cf3579292fb84c0492 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 6 Mar 2024 13:14:52 +0200 Subject: [PATCH 3/4] Enhancements to excludes property support: * JavaDoc showing the syntax * Support for optional classifier * More meaningful error with invalid parameters --- .../boot/maven/AbstractDependencyFilterMojo.java | 3 ++- .../java/org/springframework/boot/maven/Exclude.java | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractDependencyFilterMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractDependencyFilterMojo.java index 0a665a047989..037b608a898c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractDependencyFilterMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractDependencyFilterMojo.java @@ -82,7 +82,8 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo { /** * Collection of artifact definitions to exclude. The {@link Exclude} element defines * mandatory {@code groupId} and {@code artifactId} properties and an optional - * {@code classifier} property. + * {@code classifier} property. If passing in excludes as a property the syntax is + *
-Dspring-boot.excludes=groupId1:artifactId1,groupId2:artifactId2:optional-qualifier
* @since 1.1.0 */ @Parameter(property = "spring-boot.excludes") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java index 480285ab15b2..9a795f11df52 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/Exclude.java @@ -16,6 +16,8 @@ package org.springframework.boot.maven; +import org.springframework.util.Assert; + /** * A model for a dependency to exclude. * @@ -25,11 +27,16 @@ public class Exclude extends FilterableDependency { // Maven looks for this public method if giving excludes as property - // e.g. -Dspring-boot.excludes=foo:bar,foo:baz + // e.g. -Dspring-boot.excludes=myGroupId:myArtifactId:my-optional-classifier,foo:baz public void set(String propertyInput) { String[] parts = propertyInput.split(":"); + Assert.isTrue(parts.length == 2 || parts.length == 3, + "Exclude must be in the form groupId:artifactId:optional-classifier"); setGroupId(parts[0]); setArtifactId(parts[1]); + if (parts.length == 3) { + setClassifier(parts[2]); + } } } From 24f9ce240b7265421456aad0bb82b4b6a931ee17 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 6 Mar 2024 13:16:09 +0200 Subject: [PATCH 4/4] Added integration test to give excludes using property Also fixed a related existing test that has been false positive since spring boot 3. --- .../boot/maven/JarIntegrationTests.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java index d96b959fe64c..dc6b441e2885 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java @@ -195,10 +195,25 @@ void whenAnEntryIsExcludedItDoesNotAppearInTheRepackagedJar(MavenBuild mavenBuil .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-context") .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-core") .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-jcl") - .doesNotHaveEntryWithName("BOOT-INF/lib/servlet-api-2.5.jar"); + .doesNotHaveEntryWithNameStartingWith("BOOT-INF/lib/jakarta.servlet-api-"); }); } + @TestTemplate + void whenAnEntryIsExcludedWithPropertyItDoesNotAppearInTheRepackagedJar(MavenBuild mavenBuild) { + mavenBuild.project("jar") + .systemProperty("spring-boot.excludes", "jakarta.servlet:jakarta.servlet-api") + .goals("install") + .execute((project) -> { + File repackaged = new File(project, "target/jar-0.0.1.BUILD-SNAPSHOT.jar"); + assertThat(jar(repackaged)).hasEntryWithNameStartingWith("BOOT-INF/classes/") + .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-context") + .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-core") + .hasEntryWithNameStartingWith("BOOT-INF/lib/spring-jcl") + .doesNotHaveEntryWithNameStartingWith("BOOT-INF/lib/jakarta.servlet-api-"); + }); + } + @TestTemplate void whenAGroupIsExcludedNoEntriesInThatGroupAppearInTheRepackagedJar(MavenBuild mavenBuild) { mavenBuild.project("jar-exclude-group").goals("install").execute((project) -> {