[maven] IDEA-91662 doesn't take into account mirror repositories as Indexed Maven...
authorgrigoriy.myasoedov <grigoriy.myasoedov@jetbrains.com>
Sat, 27 Nov 2021 11:29:16 +0000 (14:29 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Sat, 27 Nov 2021 14:41:47 +0000 (14:41 +0000)
get RemoteArtifactRepositories from mavenProject instead maven model

GitOrigin-RevId: adf090edea111b4706b4d12a9e4abd8d91a0ca4b

plugins/maven/maven2-server-impl/src/org/jetbrains/idea/maven/server/embedder/Maven2ModelConverter.java
plugins/maven/maven2-server-impl/src/org/jetbrains/idea/maven/server/embedder/Maven2ServerEmbedderImpl.java
plugins/maven/maven3-server-common/src/org/jetbrains/idea/maven/server/MavenModelConverter.java
plugins/maven/maven3-server-impl/src/org/jetbrains/idea/maven/server/Maven3XServerEmbedder.java
plugins/maven/maven30-server-impl/src/org/jetbrains/idea/maven/server/Maven30ServerEmbedderImpl.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndexHolder.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndicesManager.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectReader.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectResolver.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsTree.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/project/importing/MavenProjectTest.java

index 278c48b17f1f29a6e11a2ed2164c3895875f69df..b6336240fcd99f276fd68f974662715de62f2dbe 100644 (file)
@@ -5,6 +5,8 @@ import com.intellij.util.ReflectionUtilRt;
 import org.apache.maven.archetype.catalog.Archetype;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 import org.apache.maven.model.*;
 import org.apache.maven.shared.dependency.tree.DependencyNode;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
@@ -125,6 +127,27 @@ public final class Maven2ModelConverter {
     return result;
   }
 
+  public static List<MavenRemoteRepository> convertRemoteRepositories(List<? extends ArtifactRepository> repositories) {
+    if (repositories == null) return new ArrayList<MavenRemoteRepository>();
+
+    List<MavenRemoteRepository> result = new ArrayList<MavenRemoteRepository>(repositories.size());
+    for (ArtifactRepository each : repositories) {
+      result.add(new MavenRemoteRepository(each.getId(),
+                                           each.getId(),
+                                           each.getUrl(),
+                                           "default",
+                                           convertPolicy(each.getReleases()),
+                                           convertPolicy(each.getSnapshots())));
+    }
+    return result;
+  }
+
+  private static MavenRemoteRepository.Policy convertPolicy(ArtifactRepositoryPolicy policy) {
+    return policy != null
+           ? new MavenRemoteRepository.Policy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy())
+           : null;
+  }
+
   private static MavenRemoteRepository.Policy convertPolicy(RepositoryPolicy policy) {
     return policy != null
            ? new MavenRemoteRepository.Policy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy())
index 723447e5b6d198b7489dbe18fe06c2a9729ef30d..577a43175f482d72df6b5add00137875be907e42 100644 (file)
@@ -63,6 +63,8 @@ import java.rmi.server.UnicastRemoteObject;
 import java.util.*;
 import java.util.concurrent.*;
 
+import static org.jetbrains.idea.maven.server.embedder.Maven2ModelConverter.convertRemoteRepositories;
+
 public final class Maven2ServerEmbedderImpl extends MavenRemoteObject implements MavenServerEmbedder {
   private final MavenEmbedder myImpl;
   private final Maven2ServerConsoleWrapper myConsoleWrapper;
@@ -325,6 +327,9 @@ public final class Maven2ServerEmbedderImpl extends MavenRemoteObject implements
                                                          mavenProject.getExtensionArtifacts(),
                                                          getLocalRepositoryFile());
 
+    List<MavenRemoteRepository> remoteRepositories = convertRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
+    model.setRemoteRepositories(remoteRepositories);
+
     RemoteNativeMavenProjectHolder holder = new RemoteNativeMavenProjectHolder(mavenProject);
     try {
       UnicastRemoteObject.exportObject(holder, 0);
index 6a7818f712e1a2c1a3b3331205cafa271b803339..98a6cff0a133bbad418d0b0763f5d3d5a11202d0 100644 (file)
@@ -5,6 +5,8 @@ import com.intellij.util.ReflectionUtilRt;
 import org.apache.maven.archetype.catalog.Archetype;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 import org.apache.maven.model.*;
 import org.apache.maven.shared.dependency.tree.DependencyNode;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
@@ -128,12 +130,34 @@ public class MavenModelConverter {
     return result;
   }
 
+  public static List<MavenRemoteRepository> convertRemoteRepositories(List<? extends ArtifactRepository> repositories) {
+    if (repositories == null) return new ArrayList<MavenRemoteRepository>();
+
+    List<MavenRemoteRepository> result = new ArrayList<MavenRemoteRepository>(repositories.size());
+    for (ArtifactRepository each : repositories) {
+      result.add(new MavenRemoteRepository(each.getId(),
+                                           each.getId(),
+                                           each.getUrl(),
+                                           each.getLayout() != null ? each.getLayout().getId() : "default",
+                                           convertPolicy(each.getReleases()),
+                                           convertPolicy(each.getSnapshots())));
+    }
+    return result;
+  }
+
+
   private static MavenRemoteRepository.Policy convertPolicy(RepositoryPolicy policy) {
     return policy != null
            ? new MavenRemoteRepository.Policy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy())
            : null;
   }
 
+  private static MavenRemoteRepository.Policy convertPolicy(ArtifactRepositoryPolicy policy) {
+    return policy != null
+           ? new MavenRemoteRepository.Policy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy())
+           : null;
+  }
+
   public static List<MavenArtifact> convertArtifacts(Collection<? extends Artifact> artifacts,
                                                      Map<Artifact, MavenArtifact> nativeToConvertedMap,
                                                      File localRepository) {
index 4ce9889d813e175c7dc2da6ef3742ab55b248308..b6307eb16401fa960880521f2065eb32b5318d05 100644 (file)
@@ -94,6 +94,8 @@ import java.rmi.RemoteException;
 import java.rmi.server.UnicastRemoteObject;
 import java.util.*;
 
+import static org.jetbrains.idea.maven.server.MavenModelConverter.convertRemoteRepositories;
+
 /**
  * Overridden maven components:
  * <p/>
@@ -1160,10 +1162,12 @@ public abstract class Maven3XServerEmbedder extends Maven3ServerEmbedder {
       }
     }
     catch (Exception e) {
-      collectProblems(mavenProject.getFile(), Collections.singleton(e),
-                      result == null ? Collections.<ModelProblem>emptyList() : result.getModelProblems(), problems);
+      collectProblems(mavenProject.getFile(), Collections.singleton(e), result.getModelProblems(), problems);
     }
 
+    List<MavenRemoteRepository> remoteRepositories = convertRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
+    model.setRemoteRepositories(remoteRepositories);
+
     RemoteNativeMavenProjectHolder holder = new RemoteNativeMavenProjectHolder(mavenProject);
     try {
       UnicastRemoteObject.exportObject(holder, 0);
index d864f8815e70f45478ea64d48d6409604f9cceb2..54c1100d863a9e1134d67533f835f1abe408a8d5 100644 (file)
@@ -86,6 +86,8 @@ import java.rmi.RemoteException;
 import java.rmi.server.UnicastRemoteObject;
 import java.util.*;
 
+import static org.jetbrains.idea.maven.server.MavenModelConverter.convertRemoteRepositories;
+
 /**
  * Overridden maven components:
  * <p/>
@@ -925,6 +927,9 @@ public class Maven30ServerEmbedderImpl extends Maven3ServerEmbedder {
       validate(mavenProject.getFile(), Collections.singleton(e), problems, null);
     }
 
+    List<MavenRemoteRepository> remoteRepositories = convertRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
+    model.setRemoteRepositories(remoteRepositories);
+
     RemoteNativeMavenProjectHolder holder = new RemoteNativeMavenProjectHolder(mavenProject);
     try {
       UnicastRemoteObject.exportObject(holder, 0);
index 392bc715fd529583143803c7c3b6654f6411fc4d..fe0fc59bac3221c124f8fe2e946aa9de9e63a3fe 100644 (file)
@@ -38,6 +38,7 @@ public class MavenIndexHolder {
 
   public boolean isEquals(@NotNull Set<String> remoteUrls, @Nullable String localPath) {
     if (!FileUtilRt.pathsEqual(myLocalIndex != null ? myLocalIndex.getRepositoryPathOrUrl() : null, localPath)) return false;
+    if (remoteUrls.size() != myRemoteIndices.size()) return false;
     for (MavenSearchIndex index : myRemoteIndices) {
       if (!remoteUrls.contains(index.getRepositoryPathOrUrl())) return false;
     }
index a74bcd7c111aa22703c75a3a5d88f4c28aefcd4a..951acfaa772a2fbebf210d7078170ad60a62b0fd 100644 (file)
@@ -30,10 +30,7 @@ import org.jetbrains.idea.reposearch.DependencySearchService;
 
 import java.io.File;
 import java.nio.file.Path;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
+import java.util.*;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
 import static org.jetbrains.idea.maven.indices.MavenArtifactManager.loadUserArchetypes;
@@ -129,17 +126,11 @@ public final class MavenIndicesManager implements Disposable {
       public void activated() {
         scheduleUpdateIndicesList(null);
       }
-    });
+    }, this);
 
     projectsManager.addProjectsTreeListener(new MavenProjectsTree.Listener() {
       @Override
-      public void projectsUpdated(@NotNull List<Pair<MavenProject, MavenProjectChanges>> updated, @NotNull List<MavenProject> deleted) {
-        scheduleUpdateIndicesList(null);
-      }
-
-      @Override
-      public void projectResolved(@NotNull Pair<MavenProject, MavenProjectChanges> projectWithChanges,
-                                  @Nullable NativeMavenProjectHolder nativeMavenProject) {
+      public void allProjectsResolved() {
         scheduleUpdateIndicesList(null);
       }
     }, this);
index 21425f90baf00e52cf7ca0dedabaec6899e276be..c5ab383a1c640051dbca287e10546b3ad917a42f 100644 (file)
@@ -188,7 +188,7 @@ public final class MavenProjectReader {
 
     Element parentXmlProject = readXml(parentFile, problems, MavenProjectProblem.ProblemType.SYNTAX);
     version = MavenJDOMUtil.findChildValueByPath(parentXmlProject, "version");
-    if(version!=null) {
+    if (version != null) {
       return version;
     }
     return calculateParentVersion(parentXmlProject, problems, parentFile);
@@ -475,7 +475,8 @@ public final class MavenProjectReader {
 
           @Override
           protected Pair<VirtualFile, RawModelReadResult> doProcessParent(VirtualFile parentFile) {
-            RawModelReadResult result = doReadProjectModel(generalSettings, projectPomDir, parentFile, explicitProfiles, recursionGuard, locator).first;
+            RawModelReadResult result =
+              doReadProjectModel(generalSettings, projectPomDir, parentFile, explicitProfiles, recursionGuard, locator).first;
             return Pair.create(parentFile, result);
           }
         }.process(generalSettings, file, parentDesc[0]);
@@ -490,7 +491,8 @@ public final class MavenProjectReader {
                                                        MavenProjectProblem.ProblemType.PARENT, false));
       }
 
-      model = MavenServerManager.getInstance().getConnector(myProject, projectPomDir.getAbsolutePath()).assembleInheritance(model, parentModel);
+      model = MavenServerManager.getInstance().getConnector(myProject, projectPomDir.getAbsolutePath())
+        .assembleInheritance(model, parentModel);
 
       // todo: it is a quick-hack here - we add inherited dummy profiles to correctly collect activated profiles in 'applyProfiles'.
       List<MavenProfile> profiles = model.getProfiles();
@@ -513,7 +515,8 @@ public final class MavenProjectReader {
                                                              MavenEmbedderWrapper embedder,
                                                              Collection<VirtualFile> files,
                                                              final MavenExplicitProfiles explicitProfiles,
-                                                             final MavenProjectReaderProjectLocator locator) throws MavenProcessCanceledException {
+                                                             final MavenProjectReaderProjectLocator locator)
+    throws MavenProcessCanceledException {
     try {
       Collection<MavenServerExecutionResult> executionResults = embedder
         .resolveProject(files, explicitProfiles.getEnabledProfiles(), explicitProfiles.getDisabledProfiles());
@@ -533,13 +536,13 @@ public final class MavenProjectReader {
           }
         }
         else {
-          readerResults.add(new MavenProjectReaderResult(projectData.mavenModel,
-                                                    projectData.mavenModelMap,
-                                                    new MavenExplicitProfiles(projectData.activatedProfiles,
-                                                                              explicitProfiles.getDisabledProfiles()),
-                                                    projectData.nativeMavenProject,
-                                                    result.problems,
-                                                    result.unresolvedArtifacts));
+          readerResults.add(new MavenProjectReaderResult(
+            projectData.mavenModel,
+            projectData.mavenModelMap,
+            new MavenExplicitProfiles(projectData.activatedProfiles, explicitProfiles.getDisabledProfiles()),
+            projectData.nativeMavenProject,
+            result.problems,
+            result.unresolvedArtifacts));
         }
       }
 
index b422de2454aeb2127000a23a7191655532790829..efb8aaee2ae9e6c6aa9bdfafaac6703ca49b1ed7 100644 (file)
@@ -94,6 +94,7 @@ public class MavenProjectResolver {
 
       MavenUtil.restartConfigHighlightning(project, mavenProjects);
     }
+    myTree.fireAllProjectsResolved();
   }
 
 
@@ -153,7 +154,6 @@ public class MavenProjectResolver {
 
       if (mavenProjectCandidate == null) continue;
 
-
       MavenProjectChanges changes = mavenProjectCandidate
         .set(result, generalSettings, false, MavenProjectReaderResult.shouldResetDependenciesAndFolders(result), false);
       if (result.nativeMavenProject != null) {
index a33f047730d7c5ace08582a3609667290354a8ca..d06593d64e909e274e7a438675063252ac9d0881 100644 (file)
@@ -1302,6 +1302,12 @@ public final class MavenProjectsTree {
     }
   }
 
+  void fireAllProjectsResolved() {
+    for (Listener each : myListeners) {
+      each.allProjectsResolved();
+    }
+  }
+
   void firePluginsResolved(@NotNull MavenProject project) {
     for (Listener each : myListeners) {
       each.pluginsResolved(project);
@@ -1501,6 +1507,8 @@ public final class MavenProjectsTree {
 
     default void artifactsDownloaded(@NotNull MavenProject project) {
     }
+
+    default void allProjectsResolved() {}
   }
 
   @ApiStatus.Internal
index 3838828cd8fef7f59fde00868ca5a0064164b3f5..fe32b8f318622555a60cd3f6315bbf3e80947d45 100644 (file)
@@ -14,11 +14,15 @@ import org.jetbrains.idea.maven.model.MavenArtifactNode;
 import org.jetbrains.idea.maven.model.MavenPlugin;
 import org.jetbrains.idea.maven.model.MavenRemoteRepository;
 import org.jetbrains.idea.maven.project.MavenProject;
+import org.jetbrains.idea.maven.project.MavenProjectsManager;
 import org.jetbrains.idea.maven.utils.MavenJDOMUtil;
+import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
+import java.util.stream.Collectors;
 
 public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
   @Test 
@@ -861,11 +865,11 @@ public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
                   "<repositories>" +
                   "  <repository>" +
                   "    <id>one</id>" +
-                  "    <url>http://repository.one.com</url>" +
+                  "    <url>https://repository.one.com</url>" +
                   "  </repository>" +
                   "  <repository>" +
                   "    <id>two</id>" +
-                  "    <url>http://repository.two.com</url>" +
+                  "    <url>https://repository.two.com</url>" +
                   "  </repository>" +
                   "</repositories>");
 
@@ -885,14 +889,14 @@ public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
                   "<repositories>" +
                   "  <repository>" +
                   "    <id>central</id>" +
-                  "    <url>http://my.repository.com</url>" +
+                  "    <url>https://my.repository.com</url>" +
                   "  </repository>" +
                   "</repositories>");
 
     List<MavenRemoteRepository> result = getMavenProject().getRemoteRepositories();
     assertEquals(1, result.size());
     assertEquals("central", result.get(0).getId());
-    assertEquals("http://my.repository.com", result.get(0).getUrl());
+    assertEquals("https://my.repository.com", result.get(0).getUrl());
   }
 
   @Test 
@@ -906,11 +910,11 @@ public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
                                      "<repositories>" +
                                      "  <repository>" +
                                      "    <id>one</id>" +
-                                     "    <url>http://repository.one.com</url>" +
+                                     "    <url>https://repository.one.com</url>" +
                                      "  </repository>" +
                                      "  <repository>" +
                                      "    <id>two</id>" +
-                                     "    <url>http://repository.two.com</url>" +
+                                     "    <url>https://repository.two.com</url>" +
                                      "  </repository>" +
                                      "</repositories>");
 
@@ -940,6 +944,63 @@ public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
     assertEquals("central", result.get(2).getId());
   }
 
+  @Test
+  public void testResolveRemoteRepositories() throws IOException {
+    updateSettingsXml("<mirrors>\n" +
+                      "  <mirror>\n" +
+                      "    <id>mirror</id>\n" +
+                      "    <url>https://test/mirror</url>\n" +
+                      "    <mirrorOf>repo,repo-pom</mirrorOf>\n" +
+                      "  </mirror>\n" +
+                      "</mirrors>\n" +
+                      "<profiles>\n" +
+                      "  <profile>\n" +
+                      "    <id>repo-test</id>\n" +
+                      "    <repositories>\n" +
+                      "      <repository>" +
+                      "        <id>repo</id>" +
+                      "        <url>https://settings/repo</url>" +
+                      "      </repository>" +
+                      "      <repository>" +
+                      "        <id>repo1</id>" +
+                      "        <url>https://settings/repo1</url>" +
+                      "      </repository>" +
+                      "    </repositories>\n" +
+                      "  </profile>\n" +
+                      "</profiles>\n" +
+                      "<activeProfiles>\n" +
+                      "   <activeProfile>repo-test</activeProfile>\n" +
+                      "</activeProfiles>");
+
+    VirtualFile projectPom = createProjectPom("<groupId>test</groupId>" +
+                                              "<artifactId>test</artifactId>" +
+                                              "<version>1</version>" +
+
+                                              "<repositories>\n" +
+                                              "  <repository>\n" +
+                                              "    <id>repo-pom</id>" +
+                                              "    <url>https://pom/repo</url>" +
+                                              "  </repository>\n" +
+                                              "  <repository>\n" +
+                                              "    <id>repo-pom1</id>" +
+                                              "    <url>https://pom/repo1</url>" +
+                                              "  </repository>\n" +
+                                              "</repositories>");
+    importProject();
+
+    MavenProject project = MavenProjectsManager.getInstance(myProject).findProject(projectPom);
+    Assert.assertNotNull(project);
+    Set<String> repoIds = project.getRemoteRepositories().stream()
+      .map(r -> r.getId())
+      .collect(Collectors.toSet());
+    System.out.println(repoIds);
+    Assert.assertTrue(repoIds.contains("mirror"));
+    Assert.assertTrue(repoIds.contains("repo-pom1"));
+    Assert.assertTrue(repoIds.contains("repo1"));
+    Assert.assertFalse(repoIds.contains("repo-pom"));
+    Assert.assertFalse(repoIds.contains("repo"));
+  }
+
   @Test 
   public void testMavenModelMap() {
     importProject("<groupId>test</groupId>" +
@@ -962,8 +1023,8 @@ public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
     assertEquals("test", map.get("groupId"));
     assertEquals("foo", map.get("build.finalName"));
     assertEquals(new File(p.getDirectory(), "target").toString(), map.get("build.directory"));
-    assertEquals(null, map.get("build.plugins"));
-    assertEquals(null, map.get("build.pluginMap"));
+    assertNull(map.get("build.plugins"));
+    assertNull(map.get("build.pluginMap"));
   }
 
   @Test