Skip to content

Commit 38062c0

Browse files
committed
WIP
1 parent d92c71c commit 38062c0

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade/common/actions/CreateAutoconfigurationAction.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.springframework.sbm.boot.upgrade.common.actions;
1717

18+
import com.fasterxml.jackson.annotation.JsonIgnore;
1819
import org.apache.commons.lang3.tuple.ImmutablePair;
1920
import org.apache.commons.lang3.tuple.Pair;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.sbm.boot.upgrade_27_30.report.helper.AutoConfigurationRegistrationHelper;
2023
import org.springframework.sbm.build.api.Module;
2124
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
2225
import org.springframework.sbm.engine.context.ProjectContext;
@@ -35,11 +38,24 @@
3538

3639
public class CreateAutoconfigurationAction extends AbstractAction {
3740

38-
private static final String SPRING_FACTORIES_PATH = "/**/src/main/resources/META-INF/spring.factories";
41+
42+
@Autowired
43+
@JsonIgnore
44+
private AutoConfigurationRegistrationHelper helper;
45+
3946
private static final String AUTO_CONFIGURATION_IMPORTS = "src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
40-
public static final String ENABLE_AUTO_CONFIGURATION_KEY = "org.springframework.boot.autoconfigure.EnableAutoConfiguration";
47+
4148
public static final Pattern COMMENT_REGEX = Pattern.compile("^#.*(\r|\n)+");
4249

50+
public static final String ENABLE_AUTO_CONFIGURATION_KEY = "org.springframework.boot.autoconfigure.EnableAutoConfiguration";
51+
52+
private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories";
53+
54+
@Override
55+
public boolean isApplicable(ProjectContext context) {
56+
return helper.evaluate(context);
57+
}
58+
4359
@Override
4460
public void apply(ProjectContext context) {
4561
Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2021 - 2022 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+
package org.springframework.sbm.boot.upgrade_27_30.report.helper;
17+
18+
import org.apache.commons.lang3.tuple.ImmutablePair;
19+
import org.apache.commons.lang3.tuple.Pair;
20+
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
21+
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
22+
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
23+
import org.springframework.sbm.engine.context.ProjectContext;
24+
import org.springframework.sbm.project.resource.ProjectResource;
25+
26+
import java.io.ByteArrayInputStream;
27+
import java.io.IOException;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Optional;
31+
import java.util.Properties;
32+
33+
/**
34+
* @author Fabian Krüger
35+
*/
36+
}
37+
public class AutoConfigurationRegistrationHelper extends SpringBootUpgradeReportSection.AbstractHelper<List<Pair<Properties, ProjectResource>>> {
38+
39+
public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)";
40+
41+
private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories";
42+
43+
private Map<String, List<Pair<Properties, ProjectResource>>> matchingSpringFactoriesFiles;
44+
45+
@Override
46+
public boolean evaluate(ProjectContext context) {
47+
IsSpringBootProject isSpringBootProjectCondition = new IsSpringBootProject();
48+
isSpringBootProjectCondition.setVersionPattern(VERSION_PATTERN);
49+
boolean isSpringBoot3Application = isSpringBootProjectCondition.evaluate(context);
50+
if(! isSpringBoot3Application) {
51+
return false;
52+
}
53+
54+
Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context);
55+
56+
if (props.isEmpty()) {
57+
return false;
58+
}
59+
60+
this.matchingSpringFactoriesFiles = props.get();
61+
return true;
62+
}
63+
64+
@Override
65+
public Map<String, List<Pair<Properties, ProjectResource>>> getData() {
66+
return matchingSpringFactoriesFiles;
67+
}
68+
69+
70+
private Optional<Pair<Properties, ProjectResource>> getSpringFactoriesProperties(ProjectContext context) {
71+
List<ProjectResource> search = context.search(
72+
new PathPatternMatchingProjectResourceFinder(
73+
SPRING_FACTORIES_PATH
74+
));
75+
76+
if (search.size() > 0) {
77+
String oldConfigFile = search.get(0).print();
78+
Properties prop = new Properties();
79+
80+
try {
81+
prop.load(new ByteArrayInputStream(oldConfigFile.getBytes()));
82+
return Optional.of(new ImmutablePair<>(prop, search.get(0)));
83+
} catch (IOException e) {
84+
throw new RuntimeException("Error whilst reading property file " + SPRING_FACTORIES_PATH, e);
85+
}
86+
}
87+
return Optional.empty();
88+
}
89+
}

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/boot-autoconfiguration-update.yaml renamed to components/sbm-recipes-boot-upgrade/src/main/resources/recipes/27_30/migration/sbu30-156-auto-configuration-registration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- name: boot-autoconfiguration-update
1+
- name: sbu30-156-auto-configuration-registration
22
description: Create org.springframework.boot.autoconfigure.AutoConfiguration.imports file for new spring 2.7
33
condition:
44
type: org.springframework.sbm.common.migration.conditions.TrueCondition

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/27_30/report/sbu30-report.yaml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,34 @@
102102
# contributors:
103103
# - "Fabian Krüger[@fabapp2]"
104104

105+
#
106+
# ==================== Still Boot 2.7 ====================
107+
#
108+
109+
- title: Auto-configuration Registration
110+
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.AutoConfigurationRegistrationHelper
111+
change: |-
112+
Spring Boot 2.7 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#changes-to-auto-configuration[introduced]
113+
a new `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` file for registering auto-configurations,
114+
while maintaining backwards compatibility with registration in `spring.factories`.
115+
With this release, support for registering auto-configurations in `spring.factories` has been removed in favor of the imports file.
116+
affected: |-
117+
The scan found `spring.factories` files.
118+
119+
<#list matchingFiles as match>
120+
* file://${match.absolutePath}[`${match.sourcePath}`]<#lt>
121+
</#list>
122+
remediation:
123+
description: |-
124+
`spring.factories` were deprecated in 2.7 and must be replaced with `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`
125+
before upgrading to 3.0.
126+
recipe: sbu30-156-auto-configuration-registration
127+
contributors:
128+
- "Fabian Krüger[@fabapp2]"
129+
- "Oleksandr[@ijusti]"
130+
gitHubIssue: 156
131+
132+
105133
- title: Upgrade Dependencies
106134
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.UpgradeDependenciesHelper
107135
change: |-
@@ -132,7 +160,10 @@
132160
contributors:
133161
- "Fabian Krüger[@fabapp2]"
134162

135-
163+
#
164+
# ==================== Now Boot 3 ====================
165+
#
166+
136167
- title: Actuator Endpoints Sanitization
137168
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.ActuatorEndpointsSanitizationHelper
138169
change: |-

0 commit comments

Comments
 (0)