|
| 1 | +/* |
| 2 | + * Copyright 2012 - present 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 io.spring.initializr.generator.spring.build.maven; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import io.spring.initializr.generator.buildsystem.Dependency; |
| 25 | +import io.spring.initializr.generator.buildsystem.maven.MavenBuild; |
| 26 | +import io.spring.initializr.generator.buildsystem.maven.MavenPluginContainer; |
| 27 | +import io.spring.initializr.generator.spring.build.BuildCustomizer; |
| 28 | +import io.spring.initializr.generator.version.VersionProperty; |
| 29 | +import io.spring.initializr.generator.version.VersionReference; |
| 30 | +import org.jspecify.annotations.Nullable; |
| 31 | + |
| 32 | +import org.springframework.core.Ordered; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * A {@link BuildCustomizer} that converts annotation processor dependencies to |
| 37 | + * {@code maven-compiler-plugin} {@code annotationProcessorPaths} configuration. |
| 38 | + * |
| 39 | + * @author Moritz Halbritter |
| 40 | + */ |
| 41 | +class ConvertAnnotationProcessorsToPluginConfigBuildCustomizer implements BuildCustomizer<MavenBuild> { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void customize(MavenBuild build) { |
| 45 | + Set<String> ids = build.dependencies().ids().collect(Collectors.toSet()); |
| 46 | + List<Dependency> annotationProcessors = new ArrayList<>(); |
| 47 | + List<Dependency> testAnnotationProcessors = new ArrayList<>(); |
| 48 | + for (String id : ids) { |
| 49 | + Dependency dependency = build.dependencies().get(id); |
| 50 | + Assert.state(dependency != null, "'dependency' must not be null"); |
| 51 | + if (dependency.getScope() == null) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + switch (dependency.getScope()) { |
| 55 | + case ANNOTATION_PROCESSOR -> { |
| 56 | + build.dependencies().remove(id); |
| 57 | + annotationProcessors.add(dependency); |
| 58 | + } |
| 59 | + case TEST_ANNOTATION_PROCESSOR -> { |
| 60 | + build.dependencies().remove(id); |
| 61 | + testAnnotationProcessors.add(dependency); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + configureCompilerPlugin(build.plugins(), annotationProcessors, "default-compile", "compile", "compile"); |
| 66 | + configureCompilerPlugin(build.plugins(), testAnnotationProcessors, "default-testCompile", "test-compile", |
| 67 | + "testCompile"); |
| 68 | + } |
| 69 | + |
| 70 | + private void configureCompilerPlugin(MavenPluginContainer plugins, List<Dependency> annotationProcessors, |
| 71 | + String executionId, String phase, String goal) { |
| 72 | + if (annotationProcessors.isEmpty()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + plugins.add("org.apache.maven.plugins", "maven-compiler-plugin", |
| 76 | + (plugin) -> plugin.execution(executionId, (execution) -> { |
| 77 | + execution.phase(phase); |
| 78 | + execution.goal(goal); |
| 79 | + execution.configuration((config) -> { |
| 80 | + config.add("annotationProcessorPaths", (paths) -> { |
| 81 | + for (Dependency annotationProcessor : annotationProcessors) { |
| 82 | + paths.add("path", (path) -> { |
| 83 | + path.add("groupId", annotationProcessor.getGroupId()); |
| 84 | + path.add("artifactId", annotationProcessor.getArtifactId()); |
| 85 | + if (annotationProcessor.getClassifier() != null) { |
| 86 | + path.add("classifier", annotationProcessor.getClassifier()); |
| 87 | + } |
| 88 | + String version = determineVersion(annotationProcessor.getVersion()); |
| 89 | + if (version != null) { |
| 90 | + path.add("version", version); |
| 91 | + } |
| 92 | + if (annotationProcessor.getType() != null) { |
| 93 | + path.add("type", annotationProcessor.getType()); |
| 94 | + } |
| 95 | + if (!annotationProcessor.getExclusions().isEmpty()) { |
| 96 | + path.add("exclusions", (exclusions) -> { |
| 97 | + for (Dependency.Exclusion exclusion : annotationProcessor.getExclusions()) { |
| 98 | + exclusions.add("exclusion", (exclusionElement) -> { |
| 99 | + exclusionElement.add("groupId", exclusion.getGroupId()); |
| 100 | + exclusionElement.add("artifactId", exclusion.getArtifactId()); |
| 101 | + }); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + })); |
| 110 | + } |
| 111 | + |
| 112 | + private @Nullable String determineVersion(@Nullable VersionReference versionReference) { |
| 113 | + if (versionReference == null) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + VersionProperty property = versionReference.getProperty(); |
| 117 | + if (property != null) { |
| 118 | + return "${" + property.toStandardFormat() + "}"; |
| 119 | + } |
| 120 | + return versionReference.getValue(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public int getOrder() { |
| 125 | + return Ordered.LOWEST_PRECEDENCE; |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments