-
Notifications
You must be signed in to change notification settings - Fork 90
Remove spring managed dependency with version consideration #602
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5067785
Initial proposal for Spring Managed Dependencies removal
ravigkant 3c77f70
Remove spring managed dependency with version consideration
ravigkant 0415398
Merge branch 'main' into main
fabapp2 365bf62
Merge branch 'main' into main
fabapp2 8ad9a54
Fixed review comments
ravigkant 8175be7
Merge remote-tracking branch 'origin/main'
ravigkant af34a29
Fixed review comments
ravigkant 92ad801
Merge branch 'main' into main
ravig-kant b3a5839
Added fixme to make RemoveManagedDependencies multi-module ready
ravigkant 40f29d6
Merge remote-tracking branch 'origin/main'
ravigkant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...s/sbm-core/src/main/java/org/springframework/sbm/build/api/SpringManagedDependencies.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.springframework.sbm.build.api; | ||
|
||
import org.openrewrite.maven.internal.MavenPomDownloader; | ||
import org.openrewrite.maven.tree.GroupArtifactVersion; | ||
import org.openrewrite.maven.tree.MavenRepository; | ||
import org.springframework.sbm.openrewrite.RewriteExecutionContext; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class holds all the dependencies included in a spring artifact | ||
*/ | ||
public class SpringManagedDependencies { | ||
|
||
private static List<MavenRepository> SPRING_REPOSITORIES = List.of( | ||
new MavenRepository("spring-release", "https://repo.spring.io/release", true, false, null, null) | ||
); | ||
|
||
private List<org.openrewrite.maven.tree.Dependency> dependencies; | ||
private static Map<GroupArtifactVersion, SpringManagedDependencies> INSTANCES = new HashMap<>(); | ||
|
||
public static SpringManagedDependencies by(String groupId, String artifact, String version){ | ||
final GroupArtifactVersion groupArtifactVersion = | ||
new GroupArtifactVersion(groupId, artifact, version); | ||
|
||
INSTANCES.computeIfAbsent(groupArtifactVersion, SpringManagedDependencies::new); | ||
return INSTANCES.get(groupArtifactVersion); | ||
} | ||
|
||
private SpringManagedDependencies(GroupArtifactVersion groupArtifactVersion){ | ||
dependencies = new MavenPomDownloader(Collections.emptyMap(), new RewriteExecutionContext()) | ||
.download(groupArtifactVersion, null, null, SPRING_REPOSITORIES) | ||
.getDependencies(); | ||
} | ||
|
||
public Stream<Dependency> stream(){ | ||
return dependencies.stream() | ||
.map(d -> Dependency.builder() | ||
.groupId(d.getGroupId()) | ||
.artifactId(d.getArtifactId()) | ||
.version(d.getVersion()) | ||
.build() | ||
); | ||
} | ||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
.../main/java/org/springframework/sbm/build/migration/actions/RemoveManagedDependencies.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.springframework.sbm.build.migration.actions; | ||
|
||
import org.springframework.sbm.build.api.Dependency; | ||
import org.springframework.sbm.build.api.SpringManagedDependencies; | ||
import org.springframework.sbm.engine.context.ProjectContext; | ||
import org.springframework.sbm.engine.recipe.AbstractAction; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.openrewrite.maven.tree.Scope.Compile; | ||
|
||
/** | ||
* The action removes the dependencies directly managed by Spring from the project dependencies | ||
* Add this action at the end of recipe so that any spring artifact inclusions as part of the | ||
* other actions are also included while removing the dependencies. | ||
*/ | ||
public class RemoveManagedDependencies extends AbstractAction { | ||
|
||
@Override | ||
public void apply(ProjectContext context) { | ||
//FIXME handle multi-module projects | ||
final List<Dependency> springManagedDependencies = context.getBuildFile() | ||
.getDeclaredDependencies(Compile) | ||
.stream() | ||
.filter(this::isSpringFrameworkDependency) | ||
.map(d -> SpringManagedDependencies.by(d.getGroupId(),d.getArtifactId(),d.getVersion())) | ||
.flatMap(SpringManagedDependencies::stream) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
|
||
Predicate<Dependency> isAlreadyManagedBySpring = d -> springManagedDependencies | ||
.stream() | ||
.filter(d::equals) | ||
.anyMatch(s -> s.isRecentThen(d)); | ||
|
||
final List<Dependency> dependenciesToBeRemoved = context.getBuildFile() | ||
.getDeclaredDependencies(Compile) | ||
.stream() | ||
.filter(isAlreadyManagedBySpring) | ||
.collect(Collectors.toList()); | ||
|
||
RemoveDependencies removeDependenciesAction = new RemoveDependencies(); | ||
removeDependenciesAction.setDependencies(dependenciesToBeRemoved); | ||
removeDependenciesAction.apply(context); | ||
} | ||
|
||
private boolean isSpringFrameworkDependency(Dependency dependency){ | ||
return dependency.getGroupId().startsWith("org.springframework"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...m-core/src/test/java/org/springframework/sbm/build/api/SpringManagedDependenciesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.springframework.sbm.build.api; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SpringManagedDependenciesTest { | ||
|
||
@Test | ||
public void pullBootStarter274Dependencies_expectJakartaAnnotationDependency(){ | ||
String jakartaCoordinates = "jakarta.annotation:jakarta.annotation-api:1.3.5"; | ||
|
||
assertThat( SpringManagedDependencies.by("org.springframework.boot", "spring-boot-starter", "2.7.4") | ||
.stream() | ||
.map(Dependency::getCoordinates) | ||
.anyMatch(jakartaCoordinates::equals) | ||
).isTrue(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...t/java/org/springframework/sbm/build/migration/actions/RemoveManagedDependenciesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.springframework.sbm.build.migration.actions; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.semver.LatestRelease; | ||
import org.springframework.sbm.build.api.Dependency; | ||
import org.springframework.sbm.engine.context.ProjectContext; | ||
import org.springframework.sbm.project.resource.TestProjectContext; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class RemoveManagedDependenciesTest { | ||
|
||
@Test | ||
public void givenProjectWithManagedDependency_removeSpringManagedDependencies_expectHibernateDependencyRemoved(){ | ||
|
||
LatestRelease latestRelease = new LatestRelease(null); | ||
System.out.println(latestRelease.compare(null, "5.6.11.Final", "5.6.11.Final")); | ||
|
||
final String hibernateCoordinates = "org.hibernate:hibernate-core:5.6.11.Final"; | ||
final String springBootDataJpaCoordinates = "org.springframework.boot:spring-boot-starter-data-jpa:2.7.4"; | ||
|
||
final ProjectContext projectContext = TestProjectContext.buildProjectContext() | ||
.withBuildFileHavingDependencies(hibernateCoordinates, springBootDataJpaCoordinates) | ||
.build(); | ||
|
||
RemoveManagedDependencies removeManagedDependencies = new RemoveManagedDependencies(); | ||
removeManagedDependencies.apply(projectContext); | ||
|
||
assertThat(projectContext.getBuildFile() | ||
.getDeclaredDependencies() | ||
.stream() | ||
.map(Dependency::getCoordinates) | ||
.anyMatch(hibernateCoordinates::equals) | ||
).isFalse(); | ||
} | ||
|
||
@Test | ||
public void givenProjectWithLowerVersionedManagedDependency_removeSpringManagedDependencies_expectDependencyRemoved(){ | ||
final String hibernateCoordinates = "org.hibernate:hibernate-core:5.3.2.Final"; | ||
final String springBootDataJpaCoordinates = "org.springframework.boot:spring-boot-starter-data-jpa:2.7.4"; | ||
|
||
final ProjectContext projectContext = TestProjectContext.buildProjectContext() | ||
.withBuildFileHavingDependencies(hibernateCoordinates, springBootDataJpaCoordinates) | ||
.build(); | ||
|
||
RemoveManagedDependencies removeManagedDependencies = new RemoveManagedDependencies(); | ||
removeManagedDependencies.apply(projectContext); | ||
|
||
assertThat(projectContext.getBuildFile() | ||
.getDeclaredDependencies() | ||
.stream() | ||
.map(Dependency::getCoordinates) | ||
.anyMatch(hibernateCoordinates::equals) | ||
).isFalse(); | ||
} | ||
|
||
@Test | ||
public void givenProjectWithHigherVersionedManagedDependency_removeSpringManagedDependencies_expectDependencyRemoved(){ | ||
final String hibernateCoordinates = "org.hibernate:hibernate-core:5.12.2.Final"; | ||
final String springBootDataJpaCoordinates = "org.springframework.boot:spring-boot-starter-data-jpa:2.7.4"; | ||
|
||
final ProjectContext projectContext = TestProjectContext.buildProjectContext() | ||
.withBuildFileHavingDependencies(hibernateCoordinates, springBootDataJpaCoordinates) | ||
.build(); | ||
|
||
RemoveManagedDependencies removeManagedDependencies = new RemoveManagedDependencies(); | ||
removeManagedDependencies.apply(projectContext); | ||
|
||
assertThat(projectContext.getBuildFile() | ||
.getDeclaredDependencies() | ||
.stream() | ||
.map(Dependency::getCoordinates) | ||
.anyMatch(hibernateCoordinates::equals) | ||
).isTrue(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.