Skip to content

Commit a9441ed

Browse files
committed
WIP
1 parent c67936c commit a9441ed

File tree

4 files changed

+150
-9
lines changed

4 files changed

+150
-9
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.fasterxml.jackson.annotation.JsonIgnore;
1919
import org.apache.commons.lang3.tuple.ImmutablePair;
2020
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;
2123
import org.openrewrite.ExecutionContext;
2224
import org.springframework.beans.factory.annotation.Autowired;
2325
import org.springframework.sbm.build.api.Module;
@@ -38,14 +40,28 @@
3840

3941
public class CreateAutoconfigurationAction extends AbstractAction {
4042

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

52+
private static final String AUTO_CONFIGURATION_IMPORTS = "src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
53+
54+
public static final Pattern COMMENT_REGEX = Pattern.compile("^#.*(\r|\n)+");
55+
56+
public static final String ENABLE_AUTO_CONFIGURATION_KEY = "org.springframework.boot.autoconfigure.EnableAutoConfiguration";
57+
58+
private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories";
59+
60+
@Override
61+
public boolean isApplicable(ProjectContext context) {
62+
return helper.evaluate(context);
63+
}
64+
4965
@Override
5066
public void apply(ProjectContext context) {
5167
Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context);
@@ -101,7 +117,9 @@ private void removeAutoConfigKeyFromSpringFactories(Properties props,
101117
new StringProjectResource(
102118
projectRootDirectory,
103119
originalResource.getAbsolutePath(),
104-
propertiesWithoutComment, executionContext);
120+
propertiesWithoutComment,
121+
executionContext
122+
);
105123
context.getProjectResources().replace(
106124
originalResource.getAbsolutePath(),
107125
springUpdatedSpringFactories);
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: 37 additions & 3 deletions
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: Prepare for Spring 3.0 dependency
106134
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.IsSpring27Or30ProjectHelper
107135

@@ -132,6 +160,10 @@
132160
contributors:
133161
- "Fabian Krüger[@fabapp2]"
134162

163+
#
164+
# ==================== Now Boot 3 ====================
165+
#
166+
135167
#
136168
# Generated from https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#json-b
137169
#
@@ -288,6 +320,8 @@
288320
gitHubIssue: 150
289321
contributors:
290322
- "Sandeep Nagaraj[@sanagaraj-pivotal]"
323+
324+
291325
- title: Banner support
292326
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.BannerSupportHelper
293327
change: |-
@@ -1706,6 +1740,6 @@
17061740

17071741

17081742
footer: |-
1709-
We want to say thank you to all Contributors:
1710-
1711-
Generated by Spring Boot Migrator (experimental)
1743+
We want to say thank you to all Contributors:
1744+
1745+
Generated by Spring Boot Migrator (experimental)

0 commit comments

Comments
 (0)