Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exclude annotation processors from the repackaged archive if necessary
This commit identifies all dependencies of scope "annotationProcessor" and exclude them if necessary. As of Spring Boot 2.4, the repackage goal reads metadata that lets it exclude the one that are tagged automatically so this customizer takes that into account to limit the noise in the generated build. Closes gh-528
- Loading branch information
Showing
3 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
.../spring/start/site/extension/build/maven/AnnotationProcessorExclusionBuildCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2012-2020 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 io.spring.start.site.extension.build.maven; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.spring.initializr.generator.buildsystem.maven.MavenBuild; | ||
import io.spring.initializr.generator.spring.build.BuildCustomizer; | ||
import io.spring.initializr.generator.version.Version; | ||
import io.spring.initializr.generator.version.VersionParser; | ||
import io.spring.initializr.generator.version.VersionRange; | ||
import io.spring.initializr.metadata.Dependency; | ||
import io.spring.initializr.metadata.InitializrMetadata; | ||
|
||
/** | ||
* A {@link BuildCustomizer} that automatically excludes annotation processors from the | ||
* repackaged archive if necessary. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
class AnnotationProcessorExclusionBuildCustomizer implements BuildCustomizer<MavenBuild> { | ||
|
||
private static final VersionRange SPRING_BOOT_2_4_0_M3_0R_LATER = VersionParser.DEFAULT.parseRange("2.4.0-M3"); | ||
|
||
private static final List<String> KNOWN_ANNOTATION_PROCESSORS = Collections | ||
.singletonList("configuration-processor"); | ||
|
||
private final InitializrMetadata metadata; | ||
|
||
private final boolean hasSmartExclude; | ||
|
||
AnnotationProcessorExclusionBuildCustomizer(InitializrMetadata metadata, Version platformVersion) { | ||
this.metadata = metadata; | ||
this.hasSmartExclude = SPRING_BOOT_2_4_0_M3_0R_LATER.match(platformVersion); | ||
} | ||
|
||
@Override | ||
public void customize(MavenBuild build) { | ||
if (!build.plugins().has("org.springframework.boot", "spring-boot-maven-plugin")) { | ||
return; | ||
} | ||
List<io.spring.initializr.generator.buildsystem.Dependency> dependencies = build.dependencies().ids() | ||
.filter(this::isAnnotationProcessor) | ||
.filter((id) -> !this.hasSmartExclude || !KNOWN_ANNOTATION_PROCESSORS.contains(id)) | ||
.map((id) -> build.dependencies().get(id)).collect(Collectors.toList()); | ||
if (!dependencies.isEmpty()) { | ||
build.plugins().add("org.springframework.boot", "spring-boot-maven-plugin", (plugin) -> plugin | ||
.configuration((configuration) -> configuration.configure("excludes", (excludes) -> { | ||
for (io.spring.initializr.generator.buildsystem.Dependency dependency : dependencies) { | ||
excludes.add("exclude", (exclude) -> { | ||
exclude.add("groupId", dependency.getGroupId()); | ||
exclude.add("artifactId", dependency.getArtifactId()); | ||
}); | ||
} | ||
}))); | ||
} | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 5; | ||
} | ||
|
||
private boolean isAnnotationProcessor(String id) { | ||
Dependency dependency = this.metadata.getDependencies().get(id); | ||
return (dependency != null) && Dependency.SCOPE_ANNOTATION_PROCESSOR.equals(dependency.getScope()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ing/start/site/extension/build/maven/AnnotationProcessorExclusionBuildCustomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012-2020 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 io.spring.start.site.extension.build.maven; | ||
|
||
import io.spring.initializr.web.project.ProjectRequest; | ||
import io.spring.start.site.extension.AbstractExtensionTests; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link AnnotationProcessorExclusionBuildCustomizer}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
class AnnotationProcessorExclusionBuildCustomizerTest extends AbstractExtensionTests { | ||
|
||
@Test | ||
void annotationProcessorsAreExcludedWithoutMetadata() { | ||
ProjectRequest request = createProjectRequest("lombok", "configuration-processor"); | ||
request.setBootVersion("2.3.0.RELEASE"); | ||
assertThat(mavenPom(request)).lines().containsSequence(" <excludes>", | ||
" <exclude>", " <groupId>org.projectlombok</groupId>", | ||
" <artifactId>lombok</artifactId>", " </exclude>", | ||
" <exclude>", | ||
" <groupId>org.springframework.boot</groupId>", | ||
" <artifactId>spring-boot-configuration-processor</artifactId>", | ||
" </exclude>", " </excludes>"); | ||
|
||
} | ||
|
||
@Test | ||
void annotationProcessorsAreExcludedOnlyIfTheyAreNotHandledWithMetadata() { | ||
ProjectRequest request = createProjectRequest("lombok", "configuration-processor"); | ||
request.setBootVersion("2.4.0"); | ||
assertThat(mavenPom(request)).lines().containsSequence(" <excludes>", | ||
" <exclude>", " <groupId>org.projectlombok</groupId>", | ||
" <artifactId>lombok</artifactId>", " </exclude>", | ||
" </excludes>"); | ||
|
||
} | ||
|
||
@Test | ||
void nonAnnotationProcessorsAreIgnored() { | ||
ProjectRequest request = createProjectRequest("web"); | ||
request.setBootVersion("2.4.0"); | ||
assertThat(mavenPom(request)).lines().doesNotContainSequence(" <exclude>", | ||
" <groupId>org.springframework.boot</groupId>", | ||
" <artifactId>spring-boot-starter-web</artifactId>", | ||
" </exclude>"); | ||
|
||
} | ||
|
||
} |