Repository: apache/netbeans
Issue: ArtifactResolutionExceptions when working with certain maven projects

### Apache NetBeans version

Apache NetBeans 19 rc 1

### What happened

Tried to open the parent project of https://github.com/apache/maven-indexer/ and got several exceptions. The exceptions already start before the project is open, as soon it is selected in the project open dialog (NB is loading the model to try to find sub projects).

Once the project is open the exceptions will keep showing up while working with the pom etc.

```
[org.netbeans.modules.project.ui.ProjectChooserAccessory$ModelUpdater] INFO org.eclipse.aether.internal.impl.DefaultArtifactResolver - Artifact org.apache:apache:pom:29 is present in the local repository, but cached from a remote repository ID that is unavailable in current build context, verifying that is downloadable from [apache.snapshots (https://repository.apache.org/snapshots, default, snapshots)]
SEVERE [org.openide.util.Exceptions]
org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.apache:apache:pom:29
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:450)
Caused: org.eclipse.aether.resolution.ArtifactResolutionException: The following artifacts could not be resolved: org.apache:apache:pom:29 (present, but unavailable): Could not find artifact org.apache:apache:pom:29
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:459)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:259)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:242)
	at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:277)
	at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:197)
Caused: org.apache.maven.artifact.resolver.ArtifactNotFoundException: The following artifacts could not be resolved: org.apache:apache:pom:29 (present, but unavailable): Could not find artifact org.apache:apache:pom:29

  org.apache:apache:pom:29

from the specified remote repositories:
  apache.snapshots (https://repository.apache.org/snapshots, releases=false, snapshots=true)

	at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:200)
	at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolveAlways(DefaultArtifactResolver.java:152)
	at org.netbeans.modules.maven.embedder.MavenEmbedder.resolve(MavenEmbedder.java:380)
[catch] at org.netbeans.modules.maven.embedder.NBRepositoryModelResolver.resolveModel(NBRepositoryModelResolver.java:74)
	at org.netbeans.modules.maven.embedder.NBRepositoryModelResolver.resolveModel(NBRepositoryModelResolver.java:88)
	at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:1009)
	at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:801)
	at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:327)
	at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:243)
	at org.netbeans.modules.maven.embedder.impl.NBModelBuilder.build(NBModelBuilder.java:53)
	at org.netbeans.modules.maven.embedder.MavenEmbedder.executeModelBuilder(MavenEmbedder.java:445)
	at org.netbeans.modules.maven.NbMavenProjectImpl.getRawModel(NbMavenProjectImpl.java:216)
	at org.netbeans.modules.maven.api.NbMavenProject.getRawModel(NbMavenProject.java:392)
	at org.netbeans.modules.maven.MavenProjectPropsImpl.lambda$get$0(MavenProjectPropsImpl.java:93)
	at org.netbeans.modules.openide.util.DefaultMutexImplementation.readAccess(DefaultMutexImplementation.java:188)
	at org.openide.util.Mutex.readAccess(Mutex.java:199)
	at org.netbeans.modules.maven.MavenProjectPropsImpl.get(MavenProjectPropsImpl.java:78)
	at org.netbeans.modules.maven.MavenProjectPropsImpl.get(MavenProjectPropsImpl.java:74)
	at org.netbeans.modules.maven.MavenProjectPropsImpl$PackagingProviderImpl.packaging(MavenProjectPropsImpl.java:306)
	at org.netbeans.modules.maven.api.NbMavenProject.getPackagingType(NbMavenProject.java:378)
	at org.netbeans.modules.maven.NbMavenProjectImpl$PackagingTypeDependentLookup.check(NbMavenProjectImpl.java:1050)
	at org.netbeans.modules.maven.NbMavenProjectImpl$PackagingTypeDependentLookup.<init>(NbMavenProjectImpl.java:1008)
	at org.netbeans.modules.maven.NbMavenProjectImpl.<init>(NbMavenProjectImpl.java:274)
	at org.netbeans.modules.maven.NbMavenProjectFactory.loadProject(NbMavenProjectFactory.java:98)
	at org.netbeans.modules.projectapi.nb.NbProjectManager.createProject(NbProjectManager.java:376)
	at org.netbeans.modules.projectapi.nb.NbProjectManager.access$300(NbProjectManager.java:69)
	at org.netbeans.modules.projectapi.nb.NbProjectManager$2.run(NbProjectManager.java:289)
	at org.netbeans.modules.projectapi.nb.NbProjectManager$2.run(NbProjectManager.java:218)
	at org.netbeans.modules.openide.util.DefaultMutexImplementation.readAccess(DefaultMutexImplementation.java:188)
	at org.openide.util.Mutex.readAccess(Mutex.java:232)
	at org.netbeans.modules.projectapi.nb.NbProjectManager.findProject(NbProjectManager.java:218)
	at org.netbeans.api.project.ProjectManager.findProject(ProjectManager.java:142)
	at org.netbeans.modules.project.ui.OpenProjectList.fileToProject(OpenProjectList.java:1257)
	at org.netbeans.modules.project.ui.ProjectChooserAccessory.getProject(ProjectChooserAccessory.java:363)
	at org.netbeans.modules.project.ui.ProjectChooserAccessory.access$000(ProjectChooserAccessory.java:67)
	at org.netbeans.modules.project.ui.ProjectChooserAccessory$1.run(ProjectChooserAccessory.java:236)
	at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1419)
	at org.netbeans.modules.openide.util.GlobalLookup.execute(GlobalLookup.java:45)
	at org.openide.util.lookup.Lookups.executeWith(Lookups.java:287)
	at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2034)
```

### How to reproduce

open the above mentioned project

### Did this work correctly in an earlier version?

18

### Operating System

linux

### JDK

20

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

_No response_

### Are you willing to submit a pull request?

Maybe

Changed files:
java/maven/test/unit/src/org/netbeans/modules/maven/classpath/AbstractProjectClassPathImplTest.java
java/maven.embedder/src/org/netbeans/modules/maven/embedder/MavenEmbedder.java

diff --git a/java/maven.embedder/src/org/netbeans/modules/maven/embedder/MavenEmbedder.java b/java/maven.embedder/src/org/netbeans/modules/maven/embedder/MavenEmbedder.java
index 7608be2203..4bf4c38954 100644
--- a/java/maven.embedder/src/org/netbeans/modules/maven/embedder/MavenEmbedder.java
+++ b/java/maven.embedder/src/org/netbeans/modules/maven/embedder/MavenEmbedder.java
@@ -45,7 +45,6 @@ import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
-import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor;
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 import org.apache.maven.execution.DefaultMavenExecutionResult;
@@ -94,7 +93,6 @@ import org.openide.util.Exceptions;
 import org.openide.util.BaseUtilities;
 import org.eclipse.aether.repository.Authentication;
 import org.eclipse.aether.DefaultRepositorySystemSession;
-import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
 import org.eclipse.aether.repository.LocalRepository;
 import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
@@ -372,13 +370,17 @@ public final class MavenEmbedder {
      */
     public void resolve(Artifact sources, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository) throws ArtifactResolutionException, ArtifactNotFoundException {
         setUpLegacySupport();
-        
-        // must call internal Resolver API directly, as the RepositorySystem does not report an exception, 
-        // even in ArtifactResolutionResult: resolve(ArtifactResolutionRequest request) catches the exception and
-        // swallows ArtifactNotFoundException.
-        // The existing calling code that handles these exception cannot work, in fact, when using resolve(ArtifactResolutionRequest request) API.
-        lookupComponent(ArtifactResolver.class).resolveAlways(sources, remoteRepositories, localRepository);
+        ArtifactResolutionRequest req = new ArtifactResolutionRequest();
+        req.setLocalRepository(localRepository);
+        req.setRemoteRepositories(remoteRepositories);
+        req.setArtifact(sources);
+        req.setOffline(isOffline());
+        ArtifactResolutionResult result = repositorySystem.resolve(req);
         normalizePath(sources);
+        // XXX check result for exceptions and throw them now?
+        for (Exception ex : result.getExceptions()) {
+            LOG.log(Level.FINE, null, ex);
+        }
     }
 
     //TODO possibly rename.. build sounds like something else..
@@ -509,7 +511,7 @@ public final class MavenEmbedder {
 
         return req;
     }
-    
+
     /**
      * Needed to avoid an NPE in {@link org.eclipse.org.eclipse.aether.DefaultArtifactResolver#resolveArtifacts} under some conditions.
      * (Also {@link org.eclipse.org.eclipse.aether.DefaultMetadataResolver#resolve}; wherever a {@link org.eclipse.aether.RepositorySystemSession} is used.)
@@ -522,7 +524,7 @@ public final class MavenEmbedder {
         }
         DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
         session.setOffline(isOffline());
-        EnhancedLocalRepositoryManagerFactory f = lookupComponent(EnhancedLocalRepositoryManagerFactory.class);
+        SimpleLocalRepositoryManagerFactory f = new SimpleLocalRepositoryManagerFactory();        
         try {
             session.setLocalRepositoryManager(f.newInstance(session, new LocalRepository(getLocalRepository().getBasedir())));
         } catch (NoLocalRepositoryManagerException ex) {
diff --git a/java/maven/test/unit/src/org/netbeans/modules/maven/classpath/AbstractProjectClassPathImplTest.java b/java/maven/test/unit/src/org/netbeans/modules/maven/classpath/AbstractProjectClassPathImplTest.java
index c1f5edfd78..78a54701ca 100644
--- a/java/maven/test/unit/src/org/netbeans/modules/maven/classpath/AbstractProjectClassPathImplTest.java
+++ b/java/maven/test/unit/src/org/netbeans/modules/maven/classpath/AbstractProjectClassPathImplTest.java
@@ -23,7 +23,6 @@ import java.io.File;
 import java.util.Collections;
 import java.util.logging.Level;
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.codehaus.plexus.util.FileUtils;
 import org.netbeans.junit.NbTestCase;
 import org.netbeans.modules.maven.embedder.EmbedderFactory;
@@ -61,11 +60,7 @@ public class AbstractProjectClassPathImplTest extends NbTestCase {
         File downloaded = new File(repo, "nbtest/grp/art/1.10-SNAPSHOT/art-1.10-20210520.222429-1.jar");
         Artifact a = EmbedderFactory.getProjectEmbedder().createArtifact("nbtest.grp", "art", "1.10-20210520.222429-1", "jar");
         assertNull(AbstractProjectClassPathImpl.getFile(a));
-        try {
-            EmbedderFactory.getProjectEmbedder().resolve(a, Collections.emptyList(), EmbedderFactory.getProjectEmbedder().getLocalRepository());
-        } catch (ArtifactNotFoundException ex) {
-            // the downloaded artifact was not found, expected as only -SNAPSHOT is installed.
-        }
+        EmbedderFactory.getProjectEmbedder().resolve(a, Collections.emptyList(), EmbedderFactory.getProjectEmbedder().getLocalRepository());
         assertEquals(installed, a.getFile());
         assertEquals(installed, AbstractProjectClassPathImpl.getFile(a));
         FileUtils.mkdir(downloaded.getParent());