Skip to content

Commit

Permalink
Match fake artifact to artifacts from other checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Feb 15, 2024
1 parent aaeabe1 commit d861375
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
33 changes: 31 additions & 2 deletions java/maven/src/org/netbeans/modules/maven/NbArtifactFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.Level;
Expand Down Expand Up @@ -100,15 +101,43 @@ public class NbArtifactFixer implements ArtifactFixer {
} else {
LOG.log(Level.INFO, "Cycle in NbArtifactFixer resolution (issue #234586): {0}", Arrays.toString(gavSet.toArray()));
}

// NOTE: this catches metadata request for all artifacts not locally cached, but all will be repored as POMs.
try {
File f = createFallbackPOM(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
//instead of workarounds down the road, we set the artifact's file here.
// some stacktraces to maven/aether do set it after querying our code, but some don't for reasons unknown to me.
artifact.setFile(f);
Set<Artifact> s = CAPTURE_FAKE_ARTIFACTS.get();
if (s != null) {
s.add(artifact);
String c = artifact.getProperty("nbResolvingArtifact.classifier", null);
String e = artifact.getProperty("nbResolvingArtifact.extension", null);

if ("".equals(c)) {
c = null;
}
// If it really resolves a POM dependency, add to missing artifacts.
if ((c == null && e == null) ||
(Objects.equals(c, artifact.getClassifier()) && Objects.equals(e, artifact.getExtension()))) {
s.add(artifact);
} else {
if (local.getLayout() != null) { // #189807: for unknown reasons, there is no layout when running inside MavenCommandLineExecutor.run

//the special snapshot handling is important in case of SNAPSHOT or x-SNAPSHOT versions, for some reason aether has slightly different
//handling of baseversion compared to maven artifact. we need to manually set the baseversion here..
boolean isSnapshot = artifact.isSnapshot();
DefaultArtifact art = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null, e, c, new DefaultArtifactHandler(e));
if (isSnapshot) {
art.setBaseVersion(artifact.getBaseVersion());
}
String path = local.pathOf(art);
File af = new File(local.getBasedir(), path);
art.setFile(af);
org.eclipse.aether.artifact.DefaultArtifact da = new org.eclipse.aether.artifact.DefaultArtifact(
art.getGroupId(), art.getArtifactId(), art.getClassifier(), art.getType(),
art.getVersion(), artifact.getProperties(), af);
s.add(da);
}
}
}
return f;
} catch (IOException x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
Expand Down Expand Up @@ -321,6 +324,11 @@ private void addMissingArtifact(Artifact a) {
problemReporter.addMissingArtifact(a, checkMissing);
}

private static String artifactId(Artifact a) {
return a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() + ":" +
(a.getClassifier() == null ? "" : a.getClassifier()) + "/" + a.getType();
}

@NbBundle.Messages({
"ERR_SystemScope=A 'system' scope dependency was not found. Code completion is affected.",
"MSG_SystemScope=There is a 'system' scoped dependency in the project but the path to the binary is not valid.\n"
Expand Down Expand Up @@ -368,7 +376,9 @@ public Collection<ProjectProblem> doArtifactChecks(@NonNull MavenProject project

Collection<Artifact> toCheck = new HashSet<>(project.getArtifacts());
if (fakes != null) {
toCheck.addAll(fakes);
// the fake artifacts are typically without a scope, so ignore scope when merging with other reported pieces.
Set<String> ids = toCheck.stream().map(MavenModelProblemsProvider::artifactId).collect(Collectors.toSet());
fakes.stream().filter(a -> !ids.contains(artifactId(a))).forEach(toCheck::add);
}

for (Artifact art : toCheck) {
Expand Down

0 comments on commit d861375

Please sign in to comment.