From c2961a1e4aef8d3f914ba93e56465c57dc66a1fd Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 18 May 2023 11:19:05 -0700 Subject: [PATCH] Exclude docker-compose and devtools during AOT processing Update `ProcessAotMojo` so that `spring-boot-docker-compose` and `spring-boot-devtools` are not included on the classpath. Fixes gh-35548 --- .../springframework/boot/maven/AotTests.java | 9 ++++ .../aot-development-only-exclusions/pom.xml | 54 +++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 33 ++++++++++++ .../maven/AbstractDependencyFilterMojo.java | 18 ++++++- .../boot/maven/AbstractPackagerMojo.java | 12 +---- .../boot/maven/ProcessAotMojo.java | 5 +- 6 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/src/main/java/org/test/SampleApplication.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AotTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AotTests.java index f205215898..9d21d9aad7 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AotTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AotTests.java @@ -176,6 +176,15 @@ class AotTests { }); } + @TestTemplate + void whenAotWithDevelopmentOnlyExclusions(MavenBuild mavenBuild) { + mavenBuild.project("aot-development-only-exclusions").goals("package").execute((project) -> { + Path aotDirectory = project.toPath().resolve("target/spring-aot/main"); + assertThat(collectRelativePaths(aotDirectory.resolve("sources"))) + .contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.java")); + }); + } + List collectRelativePaths(Path sourceDirectory) { try (Stream pathStream = Files.walk(sourceDirectory)) { return pathStream.filter(Files::isRegularFile) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/pom.xml new file mode 100644 index 0000000000..3620e07aa0 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + org.springframework.boot.maven.it + aot-development-only-exclusions + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + @java.version@ + @java.version@ + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + process-aot + + + + + + + + + org.springframework.boot + spring-boot + @project.version@ + + + jakarta.servlet + jakarta.servlet-api + @jakarta-servlet.version@ + provided + + + org.springframework.boot + spring-boot-devtools + @project.version@ + true + + + org.springframework.boot + spring-boot-docker-compose + @project.version@ + true + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..b8430b02cb --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/aot-development-only-exclusions/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-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.test; + +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; + +@Configuration(proxyBeanMethods = false) +public class SampleApplication { + + public static void main(String[] args) { + Assert.state(!ClassUtils.isPresent("org.springframework.boot.devtools.autoconfigure.DevToolsProperties", null), "Should not have devtools"); + Assert.state(!ClassUtils.isPresent("org.springframework.boot.docker.compose.core.DockerCompose", null), "Should not have docker-compose"); + SpringApplication.run(SampleApplication.class, args); + } + +} 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 5f534b913c..0a665a0479 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-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. @@ -46,6 +46,22 @@ import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; */ public abstract class AbstractDependencyFilterMojo extends AbstractMojo { + static final ExcludeFilter DEVTOOLS_EXCLUDE_FILTER; + static { + Exclude exclude = new Exclude(); + exclude.setGroupId("org.springframework.boot"); + exclude.setArtifactId("spring-boot-devtools"); + DEVTOOLS_EXCLUDE_FILTER = new ExcludeFilter(exclude); + } + + static final ExcludeFilter DOCKER_COMPOSE_EXCLUDE_FILTER; + static { + Exclude exclude = new Exclude(); + exclude.setGroupId("org.springframework.boot"); + exclude.setArtifactId("spring-boot-docker-compose"); + DOCKER_COMPOSE_EXCLUDE_FILTER = new ExcludeFilter(exclude); + } + /** * The Maven project. * @since 3.0.0 diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractPackagerMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractPackagerMojo.java index 4e9f322aa9..b6dfdc04bb 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractPackagerMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractPackagerMojo.java @@ -198,18 +198,10 @@ public abstract class AbstractPackagerMojo extends AbstractDependencyFilterMojo private ArtifactsFilter[] getAdditionalFilters() { List filters = new ArrayList<>(); if (this.excludeDevtools) { - Exclude exclude = new Exclude(); - exclude.setGroupId("org.springframework.boot"); - exclude.setArtifactId("spring-boot-devtools"); - ExcludeFilter filter = new ExcludeFilter(exclude); - filters.add(filter); + filters.add(DEVTOOLS_EXCLUDE_FILTER); } if (this.excludeDockerCompose) { - Exclude exclude = new Exclude(); - exclude.setGroupId("org.springframework.boot"); - exclude.setArtifactId("spring-boot-docker-compose"); - ExcludeFilter filter = new ExcludeFilter(exclude); - filters.add(filter); + filters.add(DOCKER_COMPOSE_EXCLUDE_FILTER); } if (!this.includeSystemScope) { filters.add(new ScopeFilter(null, Artifact.SCOPE_SYSTEM)); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ProcessAotMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ProcessAotMojo.java index 314f94510b..3dd7a525f5 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ProcessAotMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ProcessAotMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-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. @@ -111,7 +111,8 @@ public class ProcessAotMojo extends AbstractAotMojo { private URL[] getClassPath() throws Exception { File[] directories = new File[] { this.classesDirectory, this.generatedClasses }; - return getClassPath(directories, new ExcludeTestScopeArtifactFilter()); + return getClassPath(directories, new ExcludeTestScopeArtifactFilter(), DEVTOOLS_EXCLUDE_FILTER, + DOCKER_COMPOSE_EXCLUDE_FILTER); } private RunArguments resolveArguments() {