Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.sbm.boot.common.conditions;

import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.engine.context.ProjectContext;
Expand All @@ -40,14 +41,25 @@ public boolean evaluate(ProjectContext context) {
return context.getApplicationModules()
.stream()
.map(Module::getBuildFile)
.anyMatch(b ->
b.getRequestedManagedDependencies()
.stream()
.anyMatch(k ->
k.getCoordinates()
.matches("org.springframework.boot:spring-boot-dependencies:"
+ versionPattern
)
));
.anyMatch(this::hasBuildFileDependencyImport);
}

private boolean hasBuildFileDependencyImport(BuildFile buildFile) {
return buildFile.getRequestedManagedDependencies().stream()
.map( md -> resolveCoordinates(buildFile, md))
.anyMatch(c -> c.matches("org.springframework.boot:spring-boot-dependencies:"
+ versionPattern));
}

private String resolveCoordinates(BuildFile buildFile, Dependency md) {
String coordinates = md.getCoordinates();
if (md.getVersion().startsWith("${")) {
String version = buildFile.getProperty(md.getVersion().substring(2, md.getVersion().length() - 1));
if (version != null) {
// TODO: check into account properties imported from parent poms
coordinates = md.getGroupId() + ":" + md.getArtifactId() + ":" + version;
}
}
return coordinates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class HasSpringBootDependencyManuallyManaged implements Condition {

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

public void setVersionPattern(String versionPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,42 @@ public void conditionShouldBeFalseForOldVersion() {
assertThat(result).isFalse();
}

@Test
public void conditionShouldBeTrueForVersionInVariable() {
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.withMavenRootBuildFileSource("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>explicit-deps-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>explicit-deps-app</name>
<description>explicit-deps-app</description>
<properties>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""")
.build();

HasSpringBootDependencyImport condition = new HasSpringBootDependencyImport();
condition.setVersionPattern("2\\.7\\..*");

boolean result = condition.evaluate(projectContext);

assertThat(result).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,51 @@ public void conditionToBeTrueIfManagedDependencies() {

assertThat(result).isTrue();
}

@Test
public void conditionToBeTrueIfVersionIsDefinedInProperty() {
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.withMavenRootBuildFileSource("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>explicit-deps-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>explicit-deps-app</name>
<description>explicit-deps-app</description>
<properties>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>4.2.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""")
.build();

HasSpringBootDependencyManuallyManaged condition = new HasSpringBootDependencyManuallyManaged();
condition.setVersionPattern("2\\.7\\..*");

boolean result = condition.evaluate(projectContext);

assertThat(result).isTrue();
}
}