Skip to content

Commit

Permalink
Exclude annotation processors from the repackaged archive if necessary
Browse files Browse the repository at this point in the history
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
snicoll committed Dec 14, 2020
1 parent 962b9af commit 9d2e2d6
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
@@ -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());
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand All @@ -20,6 +20,7 @@
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.metadata.InitializrMetadata;

import org.springframework.context.annotation.Bean;

Expand All @@ -37,4 +38,10 @@ MavenBuildSystemHelpDocumentCustomizer mavenBuildSystemHelpDocumentCustomizer(Pr
return new MavenBuildSystemHelpDocumentCustomizer(description);
}

@Bean
AnnotationProcessorExclusionBuildCustomizer annotationProcessorExclusionBuildCustomizer(InitializrMetadata metadata,
ProjectDescription description) {
return new AnnotationProcessorExclusionBuildCustomizer(metadata, description.getPlatformVersion());
}

}
@@ -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>");

}

}

0 comments on commit 9d2e2d6

Please sign in to comment.