Skip to content

Commit

Permalink
Find reactor projects by location even if their file location changed
Browse files Browse the repository at this point in the history
Currently we cache the "known location" for an artifact, but for a
reactor project this location can change (e.g. when it is finally packed
to a jar). This leads to the unfortunate situation that we probably no
longer find it in the map.

This now first lookup the project with the map key, but if it is not
found performs a deeper analysis of the artifacts and check if the
location actually maps to the artifact location of the reactor project.
If that is the case we use that instead of return nothing.

Fix #4653

(cherry picked from commit 4d75930)
  • Loading branch information
laeubi committed Jan 29, 2025
1 parent 33e6e92 commit eaa6224
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ArtifactCollection {

protected final Map<ArtifactKey, ArtifactDescriptor> artifacts = new LinkedHashMap<>();

protected final Map<File, Map<String, ArtifactDescriptor>> artifactsWithKnownLocation = new LinkedHashMap<>();
private final Map<File, Map<String, ArtifactDescriptor>> artifactsWithKnownLocation = new LinkedHashMap<>();

public List<ArtifactDescriptor> getArtifacts(String type) {
return getArtifacts(key -> key.getType().equals(type));
Expand Down Expand Up @@ -296,18 +296,46 @@ public ReactorProject getMavenProject(File location) {
// #addArtifact enforces all artifacts at the same location have the same reactor project
return classified.values().iterator().next().getMavenProject();
}
for (Entry<ArtifactKey, ArtifactDescriptor> entry : artifacts.entrySet()) {
ArtifactDescriptor value = entry.getValue();
ReactorProject mavenProject = value.getMavenProject();
if (mavenProject != null) {
if (Objects.equals(location, mavenProject.getArtifact())) {
return mavenProject;
}
}
}
return null;
}

/**
* This triggers fetch of all dependencies.
*
* @param location
* @return
* @return a map of classifier to ArtifactDescriptor (while <code>null</code> represents the
* default artifact)
*/
public Map<String, ArtifactDescriptor> getArtifact(File location) {
artifacts.values().forEach(artifact -> artifact.getLocation(true));
return artifactsWithKnownLocation.get(normalizeLocation(location));
File normalized = normalizeLocation(location);
Map<String, ArtifactDescriptor> map = artifactsWithKnownLocation.get(normalized);
if (map == null) {
LinkedHashMap<String, ArtifactDescriptor> hashMap = new LinkedHashMap<>();
for (ArtifactDescriptor descriptor : artifacts.values()) {
ReactorProject mavenProject = descriptor.getMavenProject();
if (mavenProject != null) {
if (Objects.equals(location, mavenProject.getArtifact())) {
hashMap.put(descriptor.getClassifier(), descriptor);
}
}
}
if (hashMap.isEmpty()) {
return null;
}
artifactsWithKnownLocation.put(normalizeLocation(location), hashMap);
return hashMap;
}
return map;
}

public ArtifactDescriptor getArtifact(ArtifactKey key) {
Expand Down

0 comments on commit eaa6224

Please sign in to comment.