Skip to content

Move Auto-config Registration to new Upgrade Report #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/build-sbm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build sbm-utils
on:
push:
branches:
- "**"
paths-ignore:
- "sbm-support-rewrite/**"
jobs:
build:
runs-on: ubuntu-latest
# defaults:
# run:
# working-directory: .
steps:
- name: build-sbm
uses: actions/checkout@v3
- name: setup-java
uses: actions/setup-java@v2
with:
distribution: zulu
java-version: 17
cache: maven
- name: build-project
run: mvn -DskipITs --batch-mode clean install
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.boot.upgrade_27_30.report.helper.AutoConfigurationRegistrationHelper;
import org.openrewrite.ExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.build.api.Module;
Expand All @@ -38,14 +40,28 @@

public class CreateAutoconfigurationAction extends AbstractAction {

private static final String SPRING_FACTORIES_PATH = "/**/src/main/resources/META-INF/spring.factories";
private static final String AUTO_CONFIGURATION_IMPORTS = "src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
public static final String ENABLE_AUTO_CONFIGURATION_KEY = "org.springframework.boot.autoconfigure.EnableAutoConfiguration";
public static final Pattern COMMENT_REGEX = Pattern.compile("^#.*(\r|\n)+");

@Autowired
@JsonIgnore
private AutoConfigurationRegistrationHelper helper;

@JsonIgnore
@Autowired
private ExecutionContext executionContext;

private static final String AUTO_CONFIGURATION_IMPORTS = "src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";

public static final Pattern COMMENT_REGEX = Pattern.compile("^#.*(\r|\n)+");

public static final String ENABLE_AUTO_CONFIGURATION_KEY = "org.springframework.boot.autoconfigure.EnableAutoConfiguration";

private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories";

@Override
public boolean isApplicable(ProjectContext context) {
return helper.evaluate(context);
}

@Override
public void apply(ProjectContext context) {
Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context);
Expand Down Expand Up @@ -101,7 +117,9 @@ private void removeAutoConfigKeyFromSpringFactories(Properties props,
new StringProjectResource(
projectRootDirectory,
originalResource.getAbsolutePath(),
propertiesWithoutComment, executionContext);
propertiesWithoutComment,
executionContext
);
context.getProjectResources().replace(
originalResource.getAbsolutePath(),
springUpdatedSpringFactories);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2021 - 2022 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 org.springframework.sbm.boot.upgrade_27_30.report.helper;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.ProjectResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

/**
* @author Fabian Krüger
*/
public class AutoConfigurationRegistrationHelper extends SpringBootUpgradeReportSectionHelper<List<Pair<Properties, ProjectResource>>> {

public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)";

private static final String SPRING_FACTORIES_PATH = "/**/src/**/resources/META-INF/spring.factories";

private Map<String, List<Pair<Properties, ProjectResource>>> matchingSpringFactoriesFiles;

@Override
public boolean evaluate(ProjectContext context) {
IsSpringBootProject isSpringBootProjectCondition = new IsSpringBootProject();
isSpringBootProjectCondition.setVersionPattern(VERSION_PATTERN);
boolean isSpringBoot3Application = isSpringBootProjectCondition.evaluate(context);
if(! isSpringBoot3Application) {
return false;
}

Optional<Pair<Properties, ProjectResource>> props = getSpringFactoriesProperties(context);

if (props.isEmpty()) {
return false;
}

this.matchingSpringFactoriesFiles = Map.of("matchingSpringFactoriesFiles", List.of(props.get()));
return true;
}

@Override
public Map<String, List<Pair<Properties, ProjectResource>>> getData() {
return matchingSpringFactoriesFiles;
}


private Optional<Pair<Properties, ProjectResource>> getSpringFactoriesProperties(ProjectContext context) {
List<ProjectResource> search = context.search(
new PathPatternMatchingProjectResourceFinder(
SPRING_FACTORIES_PATH
));

if (search.size() > 0) {
String oldConfigFile = search.get(0).print();
Properties prop = new Properties();

try {
prop.load(new ByteArrayInputStream(oldConfigFile.getBytes()));
return Optional.of(new ImmutablePair<>(prop, search.get(0)));
} catch (IOException e) {
throw new RuntimeException("Error whilst reading property file " + SPRING_FACTORIES_PATH, e);
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- name: boot-autoconfiguration-update
- name: sbu30-156-auto-configuration-registration
description: Create org.springframework.boot.autoconfigure.AutoConfiguration.imports file for new spring 2.7
condition:
type: org.springframework.sbm.common.migration.conditions.TrueCondition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@
# contributors:
# - "Fabian Krüger[@fabapp2]"

#
# ==================== Still Boot 2.7 ====================
#

- title: Auto-configuration Registration
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.AutoConfigurationRegistrationHelper
change: |-
Spring Boot 2.7 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#changes-to-auto-configuration[introduced]
a new `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` file for registering auto-configurations,
while maintaining backwards compatibility with registration in `spring.factories`.
With this release, support for registering auto-configurations in `spring.factories` has been removed in favor of the imports file.
affected: |-
The scan found `spring.factories` files.

<#list matchingFiles as match>
* file://${match.absolutePath}[`${match.sourcePath}`]<#lt>
</#list>
remediation:
description: |-
`spring.factories` were deprecated in 2.7 and must be replaced with `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`
before upgrading to 3.0.
recipe: sbu30-156-auto-configuration-registration
contributors:
- "Fabian Krüger[@fabapp2]"
- "Oleksandr[@ijusti]"
gitHubIssue: 156


- title: Prepare for Spring 3.0 dependency
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.IsSpring27Or30ProjectHelper

Expand Down Expand Up @@ -132,6 +160,10 @@
contributors:
- "Fabian Krüger[@fabapp2]"

#
# ==================== Now Boot 3 ====================
#

#
# Generated from https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#json-b
#
Expand Down Expand Up @@ -288,6 +320,8 @@
gitHubIssue: 150
contributors:
- "Sandeep Nagaraj[@sanagaraj-pivotal]"


- title: Banner support
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.BannerSupportHelper
change: |-
Expand Down Expand Up @@ -1706,6 +1740,6 @@


footer: |-
We want to say thank you to all Contributors:
Generated by Spring Boot Migrator (experimental)
We want to say thank you to all Contributors:

Generated by Spring Boot Migrator (experimental)
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ void enumConstantsTest() {
+ "public class TestController {\n"
+ "\n"
+ " public void test() {\n"
+ " HttpStatus.Series f1 = HttpStatus.Series.INFORMATIONAL;\n"
+ " HttpStatus.Series f2 = HttpStatus.Series.SUCCESSFUL;\n"
+ " HttpStatus.Series f3 = HttpStatus.Series.REDIRECTION;\n"
+ " HttpStatus.Series f4 = HttpStatus.Series.CLIENT_ERROR;\n"
+ " HttpStatus.Series f5 = HttpStatus.Series.SERVER_ERROR;\n"
+ " HttpStatus.Series f1 = Series.INFORMATIONAL;\n"
+ " HttpStatus.Series f2 = Series.SUCCESSFUL;\n"
+ " HttpStatus.Series f3 = Series.REDIRECTION;\n"
+ " HttpStatus.Series f4 = Series.CLIENT_ERROR;\n"
+ " HttpStatus.Series f5 = Series.SERVER_ERROR;\n"
+ " \n"
+ " int code = 201;\n"
+ " HttpStatus.Series custom = HttpStatus.Series.resolve(code);\n"
Expand Down