Skip to content

Commit fc09a59

Browse files
sanagaraj-pivotalBoykoAlex
authored andcommitted
Adding spring factories migration
1 parent d13bc9a commit fc09a59

File tree

4 files changed

+154
-6
lines changed

4 files changed

+154
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
19+
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
20+
import org.springframework.sbm.engine.context.ProjectContext;
21+
import org.springframework.sbm.project.resource.ProjectResource;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.IOException;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Properties;
29+
30+
public class SpringFactoriesHelper extends SpringBootUpgradeReportSectionHelper<List<String>> {
31+
32+
private List<String> files;
33+
34+
@Override
35+
public boolean evaluate(ProjectContext context) {
36+
List<ProjectResource> search = context
37+
.search(
38+
new PathPatternMatchingProjectResourceFinder(
39+
"/**/src/main/resources/META-INF/spring.factories"
40+
));
41+
42+
files = search.stream().map(k -> k.getAbsolutePath().toString()).toList();
43+
44+
return search
45+
.stream()
46+
.anyMatch(r -> isRightProperty(r.print()));
47+
}
48+
49+
private boolean isRightProperty(String propertyString) {
50+
51+
Properties prop = new Properties();
52+
try {
53+
prop.load(new ByteArrayInputStream(propertyString.getBytes()));
54+
String enableAutoConfig = prop.getProperty("org.springframework.boot.autoconfigure.EnableAutoConfiguration");
55+
56+
return enableAutoConfig != null;
57+
} catch (IOException e) {
58+
59+
return false;
60+
}
61+
}
62+
63+
@Override
64+
public Map<String, List<String>> getData() {
65+
return Map.of("files", files);
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- name: sbu30-auto-configuration
2+
description: Move EnableAutoConfiguration Property from spring.factories to AutoConfiguration.imports
3+
condition:
4+
type: org.springframework.sbm.common.migration.conditions.TrueCondition
5+
actions:
6+
- type: org.springframework.sbm.boot.upgrade.common.actions.CreateAutoconfigurationAction
7+
condition:
8+
type: org.springframework.sbm.boot.upgrade_27_30.report.helper.SpringFactoriesHelper
9+
description: Move EnableAutoConfiguration Property from spring.factories to AutoConfiguration.imports

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,16 @@
102102
# contributors:
103103
# - "Fabian Krüger[@fabapp2]"
104104

105-
- title: Upgrade Dependencies
105+
- title: Prepare for Spring 3.0 dependency
106106
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.UpgradeDependenciesHelper
107107
change: |-
108-
Spring Boot 3.0 upgraded many used dependencies.
109-
Also, dependencies previously in the `javax` packages use the new `jakarta` packages now.
110-
WARNING: THis is a dummy and needs to be replaced with the matching changes from the release notes!
108+
Some of the dependencies artifacts are removed/deprecated/moved to new co ordinates, this automated recipe helps prepare SBM to migrate
111109
affected: |-
112-
List the found dependencies here?
110+
*
113111
remediation:
114112
description: |-
115-
A comprehensive list of affected dependencies and their new replacements
113+
* adds ehcache classifier
114+
* auto corrects right co ordinates of wiremock if used
116115
recipe: sbu30-upgrade-dependencies
117116
contributors:
118117
- "Fabian Krüger[@fabapp2]"
@@ -244,6 +243,28 @@
244243
contributors:
245244
- "Fabian Krüger[@fabapp2]"
246245

246+
- title: Deprecation of Spring.factories
247+
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.SpringFactoriesHelper
248+
change: |-
249+
In Spring 2.7 custom auto configurations defined in spring.factories file has been moved to `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`
250+
affected: |-
251+
The scan found spring.factories file:
252+
253+
<#list files as file>
254+
* `${file}`
255+
</#list>
256+
remediation:
257+
description: ""
258+
possibilities:
259+
- title: Move spring.factories properties
260+
description: |-
261+
Move the contents of spring.factories file into `org.springframework.boot.autoconfigure.AutoConfiguration.imports`
262+
resources:
263+
- https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#auto-configuration-registration[Auto Configuration Registration Docs]
264+
recipe: sbu30-auto-configuration
265+
gitHubIssue: 150
266+
contributors:
267+
- "Sandeep Nagaraj[@sanagaraj-pivotal]"
247268
- title: Banner support
248269
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.BannerSupportHelper
249270
change: |-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.junit.jupiter.api.Test;
19+
import org.springframework.sbm.engine.context.ProjectContext;
20+
import org.springframework.sbm.java.api.JavaSource;
21+
import org.springframework.sbm.project.resource.TestProjectContext;
22+
23+
import java.util.List;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
class SpringFactoriesHelperTest {
28+
29+
@Test
30+
public void detectsFileWithSpringFactories() {
31+
32+
ProjectContext context = TestProjectContext.buildProjectContext()
33+
.addProjectResource(
34+
"src/main/resources/META-INF/spring.factories",
35+
"""
36+
hello.world=something
37+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=XYZ
38+
"""
39+
)
40+
.build();
41+
42+
SpringFactoriesHelper sut = new SpringFactoriesHelper();
43+
boolean evaluate = sut.evaluate(context);
44+
45+
assertThat(evaluate).isTrue();
46+
47+
assertThat(sut.getData()).isNotNull();
48+
assertThat(sut.getData().get("files")).hasSize(1);
49+
assertThat(sut.getData().get("files").get(0)).contains("src/main/resources/META-INF/spring.factories");
50+
}
51+
}

0 commit comments

Comments
 (0)