Skip to content

Commit

Permalink
Parse properties section if placeholders are used
Browse files Browse the repository at this point in the history
  • Loading branch information
mwalter committed May 26, 2022
1 parent 5406a11 commit 8f2532e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## [Unreleased]

## [1.5.0] - 2022-05-15
### Added
- Parse versions from properties section if placeholders are used

## [1.4.0] - 2022-05-11
### Added
- Dependencies in dependency management section are checked as well
Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
// Kotlin support
id("org.jetbrains.kotlin.jvm") version "1.6.0"
// Gradle IntelliJ Plugin
id("org.jetbrains.intellij") version "1.4.0"
id("org.jetbrains.intellij") version "1.5.3"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "1.3.1"
// Gradle Qodana Plugin
Expand All @@ -35,7 +35,9 @@ intellij {

dependencies {
implementation("org.json:json:20211205")
implementation("org.apache.maven:maven-model:3.8.4")
implementation("org.apache.maven:maven-model:3.8.4") {
exclude(group = "org.apache.logging.log4j", module = "log4j-to-slf4j")
}

testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
testImplementation("org.mockito:mockito-core:4.2.0")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = ch.newinstance.plugin.mavendependencychecker
pluginName = MavenDependencyChecker
# SemVer format -> https://semver.org
pluginVersion = 1.4.0
pluginVersion = 1.5.0

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class DependencyParser {

Expand All @@ -35,10 +36,12 @@ public List<Dependency> parseMavenDependencies() {
try {
Model model = reader.read(new StringReader(pomFile.getText()));
List<Dependency> dependencies = new ArrayList<>(model.getDependencies());

if (model.getDependencyManagement() != null) {
dependencies.addAll(model.getDependencyManagement().getDependencies());
}
return dependencies;

return parsePropertyPlaceholder(model, dependencies);
} catch (IOException | XmlPullParserException ex) {
return Collections.emptyList();
}
Expand All @@ -62,4 +65,20 @@ public Map<String, String> parseModuleDependencies() {
return libraryMap;
}

private List<Dependency> parsePropertyPlaceholder(Model model, List<Dependency> dependencies) {
Properties properties = model.getProperties();
if (properties.isEmpty()) {
return dependencies;
}

for (Dependency dependency : dependencies) {
String version = dependency.getVersion();
if (version != null && version.startsWith("${")) {
String propertyVersion = (String) properties.get(version.substring(2, version.length() - 1)); // remove the '${}' from version string
dependency.setVersion(propertyVersion);
}
}
return dependencies;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -47,6 +48,18 @@ void parseMavenDependencies_someDependencies_shouldReturnDependencies() {
assertEquals(6, result.size());
}

@Test
void parseMavenDependencies_oneDependencyWithPlaceholder_shouldReturnVersionFromPropertiesSection() {
when(pomFile.getText()).thenReturn(readPomFile());

List<Dependency> result = parser.parseMavenDependencies();
Optional<Dependency> dependencyWithReplacedVersion = result.stream()
.filter(dependency -> dependency.getArtifactId().equals("spring-cloud-dependencies"))
.findFirst();
assertTrue(dependencyWithReplacedVersion.isPresent());
assertEquals("2021.0.1", dependencyWithReplacedVersion.get().getVersion());
}

private String readPomFile() {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource("pom.xml");
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -51,6 +52,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down

0 comments on commit 8f2532e

Please sign in to comment.