Skip to content

Commit

Permalink
[MNG-7959] User controlled rewrite (#1351)
Browse files Browse the repository at this point in the history
Extra handling if redirection happens within same GAV.

---

https://issues.apache.org/jira/browse/MNG-7959
  • Loading branch information
cstamas authored Dec 18, 2023
1 parent 9596255 commit ad5e085
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public static Artifact toPomArtifact(Artifact artifact) {
return pomArtifact;
}

/**
* Creates POM artifact out of passed in artifact by dropping classifier (if exists) and rewriting extension to
* "pom". Unconditionally, unlike {@link #toPomArtifact(Artifact)} that does this only for artifacts without
* classifiers.
*
* @since 4.0.0
*/
public static Artifact toPomArtifactUnconditionally(Artifact artifact) {
return new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
}

public static RemoteRepository toRemoteRepository(Repository repository) {
RemoteRepository.Builder builder =
new RemoteRepository.Builder(repository.getId(), repository.getLayout(), repository.getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.model.Model;
import org.apache.maven.model.building.ArtifactModelSource;
Expand Down Expand Up @@ -129,9 +128,9 @@ private Model loadPom(
throws ArtifactDescriptorException {
RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);

Set<String> visited = new LinkedHashSet<>();
LinkedHashSet<String> visited = new LinkedHashSet<>();
for (Artifact a = request.getArtifact(); ; ) {
Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a);
Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifactUnconditionally(a);
try {
VersionRequest versionRequest =
new VersionRequest(a, request.getRepositories(), request.getRequestContext());
Expand Down Expand Up @@ -239,15 +238,26 @@ private Model loadPom(

Artifact relocatedArtifact = getRelocation(session, result, model);
if (relocatedArtifact != null) {
result.addRelocation(a);
a = relocatedArtifact;
result.setArtifact(a);
if (withinSameGav(relocatedArtifact, a)) {
result.setArtifact(relocatedArtifact);
return model; // they share same model
} else {
result.addRelocation(a);
a = relocatedArtifact;
result.setArtifact(a);
}
} else {
return model;
}
}
}

private boolean withinSameGav(Artifact a1, Artifact a2) {
return Objects.equals(a1.getGroupId(), a2.getGroupId())
&& Objects.equals(a1.getArtifactId(), a2.getArtifactId())
&& Objects.equals(a1.getVersion(), a2.getVersion());
}

private Properties toProperties(Map<String, String> dominant, Map<String, String> recessive) {
Properties props = new Properties();
if (recessive != null) {
Expand Down

0 comments on commit ad5e085

Please sign in to comment.