Skip to content

Commit eb3d6ed

Browse files
384 migrate explicit depependencies (#393)
Co-authored-by: Sandeep Nagaraj <[email protected]>
1 parent 8411333 commit eb3d6ed

File tree

20 files changed

+2101
-23
lines changed

20 files changed

+2101
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
17+
package org.springframework.sbm;
18+
19+
import org.intellij.lang.annotations.Language;
20+
import org.jetbrains.annotations.NotNull;
21+
import org.junit.jupiter.api.Tag;
22+
import org.junit.jupiter.api.Test;
23+
import org.openrewrite.InMemoryExecutionContext;
24+
import org.openrewrite.maven.MavenParser;
25+
import org.openrewrite.maven.internal.MavenPomDownloader;
26+
import org.openrewrite.maven.tree.Dependency;
27+
import org.openrewrite.maven.tree.MavenResolutionResult;
28+
import org.openrewrite.maven.tree.ResolvedManagedDependency;
29+
import org.openrewrite.xml.tree.Xml;
30+
31+
import java.nio.file.Path;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Optional;
36+
37+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
38+
39+
public class BootUpgrade_27_30_ManuallyManaged_IntegrationTest extends IntegrationTestBaseClass {
40+
@Override
41+
protected String getTestSubDir() {
42+
return "boot-migration-27-30-manual-managed";
43+
}
44+
45+
@Test
46+
@Tag("integration")
47+
void migrateManuallyManagedApplication() {
48+
intializeTestProject();
49+
50+
scanProject();
51+
52+
applyRecipe("boot-2.7-3.0-dependency-version-update");
53+
54+
buildProject();
55+
56+
Xml.Document rootBuildFile = getRootBuildFile();
57+
verifyManagedDependency(rootBuildFile, "spring-boot-starter-test", "3.0.0-M3");
58+
verifyManagedDependency(rootBuildFile, "metrics-annotation", "4.2.9");
59+
60+
Xml.Document applicationBuildFile = getApplicationBuildFile();
61+
verifyProperty(applicationBuildFile, "spring-boot-starter-web.version", "3.0.0-M3");
62+
verifyDependencyWithClassifier(applicationBuildFile, "ehcache", "3.10.0", "jakarta");
63+
64+
verifyConstructorBindingRemoval();
65+
}
66+
67+
private void verifyManagedDependency(Xml.Document mavenAsXMLDocument, String artifactId, String version) {
68+
Optional<ResolvedManagedDependency> managedDependency = getManagedDependencyByArtifactId(mavenAsXMLDocument, artifactId);
69+
assertThat(managedDependency).isPresent();
70+
assertThat(managedDependency.get().getVersion()).isEqualTo(version);
71+
}
72+
73+
private void verifyDependency(Xml.Document mavenAsXMLDocument, String artifactId, String version) {
74+
verifyDependencyWithClassifier(mavenAsXMLDocument, artifactId, version, null);
75+
}
76+
77+
private void verifyDependencyWithClassifier(Xml.Document mavenAsXMLDocument, String artifactId, String version, String classifier) {
78+
Optional<Dependency> dependency = getDependencyByArtifactId(mavenAsXMLDocument, artifactId);
79+
assertThat(dependency).isPresent();
80+
assertThat(dependency.get().getVersion()).isEqualTo(version);
81+
if (classifier != null) {
82+
assertThat(dependency.get().getClassifier()).isEqualTo(classifier);
83+
}
84+
}
85+
86+
@NotNull
87+
private Optional<Dependency> getDependencyByArtifactId(Xml.Document mavenAsXMLDocument, String artifactId) {
88+
List<Dependency> dependencies = getDependencies(mavenAsXMLDocument);
89+
return dependencies
90+
.stream()
91+
.filter(dependency -> dependency.getArtifactId().equals(artifactId))
92+
.findAny();
93+
}
94+
95+
@NotNull
96+
private List<Dependency> getDependencies(Xml.Document mavenAsXMLDocument) {
97+
return mavenAsXMLDocument
98+
.getMarkers()
99+
.findFirst(MavenResolutionResult.class)
100+
.get()
101+
.getPom()
102+
.getRequestedDependencies();
103+
}
104+
105+
private void verifyProperty(Xml.Document mavenAsXMLDocument, String name, String value) {
106+
Map<String, String> props = getProperties(mavenAsXMLDocument);
107+
assertThat(props.containsKey(name)).isTrue();
108+
assertThat(props.get(name)).isEqualTo(value);
109+
}
110+
111+
@NotNull
112+
private Map<String, String> getProperties(Xml.Document mavenAsXMLDocument) {
113+
return mavenAsXMLDocument
114+
.getMarkers()
115+
.findFirst(MavenResolutionResult.class)
116+
.get()
117+
.getPom()
118+
.getProperties();
119+
}
120+
121+
@NotNull
122+
Optional<ResolvedManagedDependency> getManagedDependencyByArtifactId(Xml.Document mavenAsXMLDocument, String artifactId) {
123+
return getManagedDependencies(mavenAsXMLDocument)
124+
.stream()
125+
.filter(md -> md.getArtifactId().equals(artifactId))
126+
.findAny();
127+
}
128+
129+
@NotNull
130+
private List<ResolvedManagedDependency> getManagedDependencies(Xml.Document mavenAsXMLDocument) {
131+
return mavenAsXMLDocument
132+
.getMarkers()
133+
.findFirst(MavenResolutionResult.class)
134+
.get()
135+
.getPom()
136+
.getDependencyManagement();
137+
}
138+
139+
@NotNull
140+
private Xml.Document getRootBuildFile() {
141+
return parsePom(loadFile(Path.of("pom.xml")));
142+
}
143+
144+
@NotNull
145+
private Xml.Document getApplicationBuildFile() {
146+
return parsePom(loadFile(Path.of("spring-app/pom.xml")), loadFile(Path.of("pom.xml")));
147+
}
148+
149+
@NotNull
150+
private Xml.Document parsePom(@Language("xml") String... pomContents) {
151+
MavenParser mavenParser = new MavenParser.Builder().build();
152+
return mavenParser.parse(pomContents).get(0);
153+
}
154+
155+
private void verifyConstructorBindingRemoval() {
156+
String constructorBindingConfigClass = loadJavaFileFromSubmodule("spring-app/", "org.springboot.example.upgrade", "ConstructorBindingConfig");
157+
assertThat(constructorBindingConfigClass).isEqualTo("package org.springboot.example.upgrade;\n" +
158+
"\n" +
159+
"import org.springframework.boot.context.properties.ConfigurationProperties;\n" +
160+
"\n" +
161+
"@ConfigurationProperties(prefix = \"mail\")\n" +
162+
"public class ConstructorBindingConfig {\n" +
163+
" private String hostName;\n" +
164+
"\n" +
165+
" public ConstructorBindingConfig(String hostName) {\n" +
166+
" this.hostName = hostName;\n" +
167+
" }\n" +
168+
"}" +
169+
"\n");
170+
}
171+
172+
private void buildProject() {
173+
executeMavenGoals(getTestDir(), "clean", "verify");
174+
}
175+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**Features used in the current project**
2+
3+
* ehcache in pom is mentioned this helps validate our migration to spring 3 where ehcache classifier has to be used to resolve version.
4+
* Uses Constructor binding on classes which will be removed when migrating to Spring 3
5+
* Uses PagingAndSortingRepo interface where CrudRepo has been removed.
6+
* Uses properties that will be removed/moved on spring 3 migration
7+
* Uses micrometer packages which are moved to a new structure.
8+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<packaging>pom</packaging>
6+
<modules>
7+
<module>spring-app</module>
8+
</modules>
9+
10+
<groupId>com.example</groupId>
11+
<artifactId>boot-upgrade-27_30</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<name>boot-upgrade-27_30</name>
14+
<description>boot-upgrade-27_30</description>
15+
<properties>
16+
<java.version>17</java.version>
17+
</properties>
18+
19+
<repositories>
20+
<repository>
21+
<id>spring-snapshot</id>
22+
<url>https://repo.spring.io/snapshot</url>
23+
<releases>
24+
<enabled>false</enabled>
25+
</releases>
26+
</repository>
27+
<repository>
28+
<id>spring-milestone</id>
29+
<url>https://repo.spring.io/milestone</url>
30+
<snapshots>
31+
<enabled>false</enabled>
32+
</snapshots>
33+
</repository>
34+
<repository>
35+
<id>spring-release</id>
36+
<url>https://repo.spring.io/release</url>
37+
<snapshots>
38+
<enabled>false</enabled>
39+
</snapshots>
40+
</repository>
41+
</repositories>
42+
43+
<dependencyManagement>
44+
<dependencies>
45+
<dependency>
46+
<groupId>io.dropwizard.metrics</groupId>
47+
<artifactId>metrics-annotation</artifactId>
48+
<version>4.2.8</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<version>2.7.0</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.glassfish.jaxb</groupId>
58+
<artifactId>jaxb-runtime</artifactId>
59+
<version>2.3.6</version>
60+
</dependency>
61+
</dependencies>
62+
</dependencyManagement>
63+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2021 - 2022 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>boot-upgrade-27_30</artifactId>
23+
<groupId>com.example</groupId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>spring-app</artifactId>
29+
30+
<properties>
31+
<maven.compiler.source>17</maven.compiler.source>
32+
<maven.compiler.target>17</maven.compiler.target>
33+
<spring-boot-starter-web.version>2.7.0</spring-boot-starter-web.version>
34+
</properties>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.ehcache</groupId>
38+
<artifactId>ehcache</artifactId>
39+
<version>3.0.0</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
<version>${spring-boot-starter-web.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springboot.example.upgrade;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class BootUpgrade2730Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(BootUpgrade2730Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springboot.example.upgrade;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.boot.context.properties.ConstructorBinding;
5+
6+
@ConfigurationProperties(prefix = "mail")
7+
@ConstructorBinding
8+
public class ConstructorBindingConfig {
9+
private String hostName;
10+
11+
public ConstructorBindingConfig(String hostName) {
12+
this.hostName = hostName;
13+
}
14+
}

components/sbm-core/src/main/java/org/springframework/sbm/build/api/BuildFile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public interface BuildFile extends ProjectResource {
8787

8888
List<Dependency> getDependencyManagement();
8989

90+
List<Dependency> getRequestedManagedDependencies();
91+
9092
void addToDependencyManagement(Dependency dependency);
9193

9294
void addToDependencyManagementInner(Dependency dependency);

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/OpenRewriteMavenBuildFile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,19 @@ public List<Dependency> getDependencyManagement() {
485485
.collect(Collectors.toList());
486486
}
487487

488+
@Override
489+
public List<Dependency> getRequestedManagedDependencies() {
490+
return this.getPom().getPom().getRequested()
491+
.getDependencyManagement()
492+
.stream()
493+
.map(md -> Dependency.builder()
494+
.artifactId(md.getArtifactId())
495+
.version(md.getVersion())
496+
.groupId(md.getGroupId())
497+
.build())
498+
.collect(Collectors.toList());
499+
}
500+
488501
private Dependency getDependency(ResolvedManagedDependency d) {
489502
return Dependency.builder()
490503
.groupId(d.getGroupId())

0 commit comments

Comments
 (0)