Skip to content

Commit

Permalink
Add parent and bom dependencies recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
jwharm committed Jul 25, 2024
1 parent d1e78ea commit 7de4d66
Showing 1 changed file with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import java.util.regex.Pattern;

/**
Expand All @@ -31,20 +32,19 @@
final class PomHandler {

private static final Pattern VAR_PATTERN = Pattern.compile("\\$\\{(.+?)}");

private final ArtifactResolver resolver;
private final XMLInputFactory xmlInputFactory;

private final boolean recursive;
private HashMap<String, String> properties;

// Use getInstance()
private PomHandler(ArtifactResolver resolver) {
private PomHandler(ArtifactResolver resolver, boolean recursive) {
this.resolver = resolver;
this.xmlInputFactory = XMLInputFactory.newInstance();
this.recursive = recursive;
}

static PomHandler getInstance(ArtifactResolver resolver) {
return new PomHandler(resolver);
return new PomHandler(resolver, false);
}

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ else if (nextEvent.isEndElement()) {
String name = nextEvent.asEndElement().getName().getLocalPart();

if (xpath.inProperties())
properties.put(name, characters.toString());
properties.put(name, parse(characters.toString()));
else if (xpath.inProjectProperty())
properties.put("project." + name, characters.toString());

Expand All @@ -112,17 +112,23 @@ else if (xpath.inProjectProperty())
String id = "%s:%s:%s".formatted(groupId, artifactId, versionId);
var dep = DependencyDetails.of(id);

var isBom = name.equals("dependency")
&& artifactId.toString().endsWith("-bom");

// Download and add the parent pom to the list
var parent = resolver.tryResolve(dep, repository, dep.filename("pom"));
Optional<byte[]> parent = Optional.empty();
if (name.equals("parent") || isBom || !recursive)
parent = resolver.tryResolve(dep, repository, dep.filename("pom"));

// Reset string builders
groupId = new StringBuilder();
artifactId = new StringBuilder();
versionId = new StringBuilder();

// Recursively add the parent pom of the parent pom
if (name.equals("parent"))
parent.ifPresent(bytes -> addParentPOMs(bytes, repository));
if (name.equals("parent") || isBom)
parent.ifPresent(bytes -> new PomHandler(resolver, true)
.addParentPOMs(bytes, repository));
}
default -> {} // ignored
}
Expand All @@ -145,10 +151,14 @@ else if (xpath.inProjectProperty())
* @return the text with property references replaced by their value
*/
private String parse(Object raw) {
var matcher = VAR_PATTERN.matcher(raw.toString());
return matcher.find()
? parse(matcher.replaceFirst(properties.get(matcher.group(1))))
: raw.toString();
try {
var matcher = VAR_PATTERN.matcher(raw.toString());
return matcher.find()
? parse(matcher.replaceFirst(properties.get(matcher.group(1))))
: raw.toString();
} catch (Exception e) {
return raw.toString();
}
}

/**
Expand Down

0 comments on commit 7de4d66

Please sign in to comment.