Skip to content

Commit 04282f6

Browse files
411 fixed version property spring dependencies import (#412)
Co-authored-by: Sandeep Nagaraj <[email protected]>
1 parent 8113681 commit 04282f6

File tree

4 files changed

+107
-10
lines changed

4 files changed

+107
-10
lines changed

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/common/conditions/HasSpringBootDependencyImport.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.boot.common.conditions;
1717

18+
import org.springframework.sbm.build.api.BuildFile;
1819
import org.springframework.sbm.build.api.Dependency;
1920
import org.springframework.sbm.build.api.Module;
2021
import org.springframework.sbm.engine.context.ProjectContext;
@@ -40,14 +41,25 @@ public boolean evaluate(ProjectContext context) {
4041
return context.getApplicationModules()
4142
.stream()
4243
.map(Module::getBuildFile)
43-
.anyMatch(b ->
44-
b.getRequestedManagedDependencies()
45-
.stream()
46-
.anyMatch(k ->
47-
k.getCoordinates()
48-
.matches("org.springframework.boot:spring-boot-dependencies:"
49-
+ versionPattern
50-
)
51-
));
44+
.anyMatch(this::hasBuildFileDependencyImport);
45+
}
46+
47+
private boolean hasBuildFileDependencyImport(BuildFile buildFile) {
48+
return buildFile.getRequestedManagedDependencies().stream()
49+
.map( md -> resolveCoordinates(buildFile, md))
50+
.anyMatch(c -> c.matches("org.springframework.boot:spring-boot-dependencies:"
51+
+ versionPattern));
52+
}
53+
54+
private String resolveCoordinates(BuildFile buildFile, Dependency md) {
55+
String coordinates = md.getCoordinates();
56+
if (md.getVersion().startsWith("${")) {
57+
String version = buildFile.getProperty(md.getVersion().substring(2, md.getVersion().length() - 1));
58+
if (version != null) {
59+
// TODO: check into account properties imported from parent poms
60+
coordinates = md.getGroupId() + ":" + md.getArtifactId() + ":" + version;
61+
}
62+
}
63+
return coordinates;
5264
}
5365
}

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/common/conditions/HasSpringBootDependencyManuallyManaged.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class HasSpringBootDependencyManuallyManaged implements Condition {
3030

3131
@Override
3232
public String getDescription() {
33-
return String.format("Check if any Build file has a manually managed dependences with a version matching pattern '%s'.", versionPattern);
33+
return String.format("Check if any Build file has a manually managed dependencies with a version matching pattern '%s'.", versionPattern);
3434
}
3535

3636
public void setVersionPattern(String versionPattern) {

components/sbm-support-boot/src/test/java/org/springframework/sbm/boot/common/conditions/HasSpringBootDependencyImportTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,42 @@ public void conditionShouldBeFalseForOldVersion() {
120120
assertThat(result).isFalse();
121121
}
122122

123+
@Test
124+
public void conditionShouldBeTrueForVersionInVariable() {
125+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
126+
.withMavenRootBuildFileSource("""
127+
<?xml version="1.0" encoding="UTF-8"?>
128+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
129+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
130+
<modelVersion>4.0.0</modelVersion>
131+
<groupId>com.example</groupId>
132+
<artifactId>explicit-deps-app</artifactId>
133+
<version>0.0.1-SNAPSHOT</version>
134+
<name>explicit-deps-app</name>
135+
<description>explicit-deps-app</description>
136+
<properties>
137+
<spring-boot.version>2.7.1</spring-boot.version>
138+
</properties>
139+
<dependencyManagement>
140+
<dependencies>
141+
<dependency>
142+
<groupId>org.springframework.boot</groupId>
143+
<artifactId>spring-boot-dependencies</artifactId>
144+
<version>${spring-boot.version}</version>
145+
<type>pom</type>
146+
<scope>import</scope>
147+
</dependency>
148+
</dependencies>
149+
</dependencyManagement>
150+
</project>
151+
""")
152+
.build();
153+
154+
HasSpringBootDependencyImport condition = new HasSpringBootDependencyImport();
155+
condition.setVersionPattern("2\\.7\\..*");
156+
157+
boolean result = condition.evaluate(projectContext);
158+
159+
assertThat(result).isTrue();
160+
}
123161
}

components/sbm-support-boot/src/test/java/org/springframework/sbm/boot/common/conditions/HasSpringBootDependencyManuallyManagedTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,51 @@ public void conditionToBeTrueIfManagedDependencies() {
189189

190190
assertThat(result).isTrue();
191191
}
192+
193+
@Test
194+
public void conditionToBeTrueIfVersionIsDefinedInProperty() {
195+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
196+
.withMavenRootBuildFileSource("""
197+
<?xml version="1.0" encoding="UTF-8"?>
198+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
199+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
200+
<modelVersion>4.0.0</modelVersion>
201+
<groupId>com.example</groupId>
202+
<artifactId>explicit-deps-app</artifactId>
203+
<version>0.0.1-SNAPSHOT</version>
204+
<name>explicit-deps-app</name>
205+
<description>explicit-deps-app</description>
206+
<properties>
207+
<spring-boot.version>2.7.1</spring-boot.version>
208+
</properties>
209+
210+
<dependencies>
211+
<dependency>
212+
<groupId>org.springframework.boot</groupId>
213+
<artifactId>spring-boot-starter-web</artifactId>
214+
<version>${spring-boot.version}</version>
215+
</dependency>
216+
<dependency>
217+
<groupId>io.dropwizard.metrics</groupId>
218+
<artifactId>metrics-annotation</artifactId>
219+
<version>4.2.8</version>
220+
</dependency>
221+
<dependency>
222+
<groupId>org.springframework.boot</groupId>
223+
<artifactId>spring-boot-starter-test</artifactId>
224+
<version>2.7.3</version>
225+
<scope>test</scope>
226+
</dependency>
227+
</dependencies>
228+
</project>
229+
""")
230+
.build();
231+
232+
HasSpringBootDependencyManuallyManaged condition = new HasSpringBootDependencyManuallyManaged();
233+
condition.setVersionPattern("2\\.7\\..*");
234+
235+
boolean result = condition.evaluate(projectContext);
236+
237+
assertThat(result).isTrue();
238+
}
192239
}

0 commit comments

Comments
 (0)