1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
3 package org.jetbrains.idea.maven.project.importing;
5 import com.intellij.compiler.CompilerConfiguration;
6 import com.intellij.compiler.CompilerConfigurationImpl;
7 import com.intellij.openapi.module.LanguageLevelUtil;
8 import com.intellij.openapi.util.io.FileUtil;
9 import com.intellij.openapi.util.text.StringUtil;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.pom.java.LanguageLevel;
12 import org.jetbrains.idea.maven.MavenMultiVersionImportingTestCase;
13 import org.jetbrains.idea.maven.model.MavenArtifactNode;
14 import org.jetbrains.idea.maven.model.MavenPlugin;
15 import org.jetbrains.idea.maven.model.MavenRemoteRepository;
16 import org.jetbrains.idea.maven.project.MavenProject;
17 import org.jetbrains.idea.maven.project.MavenProjectsManager;
18 import org.jetbrains.idea.maven.utils.MavenJDOMUtil;
19 import org.junit.Assert;
20 import org.junit.Test;
23 import java.io.IOException;
25 import java.util.stream.Collectors;
27 public class MavenProjectTest extends MavenMultiVersionImportingTestCase {
29 public void testCollectingPlugins() {
30 importProject("<groupId>test</groupId>" +
31 "<artifactId>project</artifactId>" +
32 "<version>1</version>" +
36 " <groupId>group1</groupId>" +
37 " <artifactId>id1</artifactId>" +
38 " <version>1</version>" +
41 " <groupId>group1</groupId>" +
42 " <artifactId>id2</artifactId>" +
45 " <groupId>group2</groupId>" +
46 " <artifactId>id1</artifactId>" +
51 assertModules("project");
53 assertDeclaredPlugins(p("group1", "id1"), p("group1", "id2"), p("group2", "id1"));
57 public void testPluginsContainDefaultPlugins() {
58 importProject("<groupId>test</groupId>" +
59 "<artifactId>project</artifactId>" +
60 "<version>1</version>" +
64 " <groupId>group1</groupId>" +
65 " <artifactId>id1</artifactId>" +
66 " <version>1</version>" +
71 assertModules("project");
73 assertContain(p(getMavenProject().getPlugins()), p("group1", "id1"), p("org.apache.maven.plugins", "maven-compiler-plugin"));
77 public void testDefaultPluginsAsDeclared() {
78 importProject("<groupId>test</groupId>" +
79 "<artifactId>project</artifactId>" +
80 "<version>1</version>" +
84 " <groupId>org.apache.maven.plugins</groupId>" +
85 " <artifactId>maven-compiler-plugin</artifactId>" +
90 assertModules("project");
92 assertDeclaredPlugins(p("org.apache.maven.plugins", "maven-compiler-plugin"));
96 public void testDoNotDuplicatePluginsFromBuildAndManagement() {
97 importProject("<groupId>test</groupId>" +
98 "<artifactId>project</artifactId>" +
99 "<version>1</version>" +
103 " <groupId>org.apache.maven.plugins</groupId>" +
104 " <artifactId>maven-compiler-plugin</artifactId>" +
107 " <pluginManagement>" +
110 " <groupId>org.apache.maven.plugins</groupId>" +
111 " <artifactId>maven-compiler-plugin</artifactId>" +
114 " </pluginManagement>" +
117 assertModules("project");
119 assertDeclaredPlugins(p("org.apache.maven.plugins", "maven-compiler-plugin"));
123 public void testCollectingPluginsFromProfilesAlso() {
124 importProject("<groupId>test</groupId>" +
125 "<artifactId>project</artifactId>" +
126 "<version>1</version>" +
131 " <groupId>group</groupId>" +
132 " <artifactId>id</artifactId>" +
133 " <version>1</version>" +
140 " <id>profile1</id>" +
144 " <groupId>group1</groupId>" +
145 " <artifactId>id1</artifactId>" +
151 " <id>profile2</id>" +
155 " <groupId>group2</groupId>" +
156 " <artifactId>id2</artifactId>" +
163 assertModules("project");
165 assertDeclaredPlugins(p("group", "id"));
167 importProjectWithProfiles("profile1");
168 assertDeclaredPlugins(p("group", "id"), p("group1", "id1"));
170 importProjectWithProfiles("profile2");
171 assertDeclaredPlugins(p("group", "id"), p("group2", "id2"));
173 importProjectWithProfiles("profile1", "profile2");
174 assertDeclaredPlugins(p("group", "id"), p("group1", "id1"), p("group2", "id2"));
178 public void testFindingPlugin() {
179 importProject("<groupId>test</groupId>" +
180 "<artifactId>project</artifactId>" +
181 "<version>1</version>" +
186 " <groupId>group</groupId>" +
187 " <artifactId>id</artifactId>" +
188 " <version>1</version>" +
195 " <id>profile1</id>" +
199 " <groupId>group1</groupId>" +
200 " <artifactId>id1</artifactId>" +
206 " <id>profile2</id>" +
210 " <groupId>group2</groupId>" +
211 " <artifactId>id2</artifactId>" +
218 assertModules("project");
220 assertEquals(p("group", "id"), p(findPlugin("group", "id")));
221 assertNull(findPlugin("group1", "id1"));
223 importProjectWithProfiles("profile1");
224 assertEquals(p("group1", "id1"), p(findPlugin("group1", "id1")));
225 assertNull(findPlugin("group2", "id2"));
229 public void testFindingDefaultPlugin() {
230 importProject("<groupId>test</groupId>" +
231 "<artifactId>project</artifactId>" +
232 "<version>1</version>" +
237 " <groupId>group</groupId>" +
238 " <artifactId>id</artifactId>" +
239 " <version>1</version>" +
244 assertModules("project");
246 assertNotNull(findPlugin("group", "id"));
247 assertNotNull(findPlugin("org.apache.maven.plugins", "maven-compiler-plugin"));
251 public void testFindingMavenGroupPluginWithDefaultPluginGroup() {
252 importProject("<groupId>test</groupId>" +
253 "<artifactId>project</artifactId>" +
254 "<version>1</version>" +
259 " <artifactId>some.plugin.id</artifactId>" +
263 assertModules("project");
265 assertEquals(p("org.apache.maven.plugins", "some.plugin.id"),
266 p(findPlugin("org.apache.maven.plugins", "some.plugin.id")));
267 assertNull(findPlugin("some.other.group.id", "some.plugin.id"));
271 public void testPluginConfiguration() {
272 importProject("<groupId>test</groupId>" +
273 "<artifactId>project</artifactId>" +
274 "<version>1</version>" +
279 " <groupId>group</groupId>" +
280 " <artifactId>id1</artifactId>" +
281 " <version>1</version>" +
284 " <groupId>group</groupId>" +
285 " <artifactId>id2</artifactId>" +
286 " <version>1</version>" +
288 " </configuration>" +
291 " <groupId>group</groupId>" +
292 " <artifactId>id3</artifactId>" +
293 " <version>1</version>" +
298 " </configuration>" +
303 assertNull(findPluginConfig("group", "id1", "one.two"));
304 assertNull(findPluginConfig("group", "id2", "one.two"));
305 assertEquals("foo", findPluginConfig("group", "id3", "one.two"));
306 assertNull(findPluginConfig("group", "id3", "one.two.three"));
310 public void testPluginGoalConfiguration() {
311 importProject("<groupId>test</groupId>" +
312 "<artifactId>project</artifactId>" +
313 "<version>1</version>" +
318 " <groupId>group</groupId>" +
319 " <artifactId>id</artifactId>" +
320 " <version>1</version>" +
325 " <goal>compile</goal>" +
331 " </configuration>" +
336 " <goal>testCompile</goal>" +
342 " </configuration>" +
349 assertNull(findPluginGoalConfig("group", "id", "package", "one.two"));
350 assertEquals("a", findPluginGoalConfig("group", "id", "compile", "one.two"));
351 assertEquals("b", findPluginGoalConfig("group", "id", "testCompile", "one.two"));
355 public void testPluginConfigurationHasResolvedVariables() {
356 importProject("<groupId>test</groupId>" +
357 "<artifactId>project</artifactId>" +
358 "<version>1</version>" +
361 " <some.path>somePath</some.path>" +
367 " <groupId>group</groupId>" +
368 " <artifactId>id</artifactId>" +
369 " <version>1</version>" +
371 " <one>${some.path}</one>" +
372 " </configuration>" +
377 assertEquals("somePath", findPluginConfig("group", "id", "one"));
381 public void testPluginConfigurationWithStandardVariable() {
382 importProject("<groupId>test</groupId>" +
383 "<artifactId>project</artifactId>" +
384 "<version>1</version>" +
389 " <groupId>group</groupId>" +
390 " <artifactId>id</artifactId>" +
391 " <version>1</version>" +
393 " <one>${project.build.directory}</one>" +
394 " </configuration>" +
399 assertEquals(getProjectPath() + "/target",
400 FileUtil.toSystemIndependentName(findPluginConfig("group", "id", "one")));
404 public void testPluginConfigurationWithColons() {
405 importProject("<groupId>test</groupId>" +
406 "<artifactId>project</artifactId>" +
407 "<version>1</version>" +
411 " <groupId>group</groupId>" +
412 " <artifactId>id</artifactId>" +
413 " <version>1</version>" +
415 " <two:three>xxx</two:three>" +
416 " </configuration>" +
421 assertNull(findPluginConfig("group", "id", "two:three"));
425 public void testMergingPluginConfigurationFromBuildAndProfiles() {
426 createProjectPom("<groupId>test</groupId>" +
427 "<artifactId>project</artifactId>" +
428 "<version>1</version>" +
436 " <groupId>org.apache.maven.plugins</groupId>" +
437 " <artifactId>maven-compiler-plugin</artifactId>" +
439 " <target>1.4</target>" +
440 " </configuration>" +
450 " <groupId>org.apache.maven.plugins</groupId>" +
451 " <artifactId>maven-compiler-plugin</artifactId>" +
453 " <source>1.4</source>" +
454 " </configuration>" +
464 " <groupId>org.apache.maven.plugins</groupId>" +
465 " <artifactId>maven-compiler-plugin</artifactId>" +
467 " <debug>true</debug>" +
468 " </configuration>" +
472 importProjectWithProfiles("one", "two");
474 MavenPlugin plugin = findPlugin("org.apache.maven.plugins", "maven-compiler-plugin");
475 assertEquals("1.4", plugin.getConfigurationElement().getChildText("source"));
476 assertEquals("1.4", plugin.getConfigurationElement().getChildText("target"));
477 assertEquals("true", plugin.getConfigurationElement().getChildText("debug"));
481 public void testCompilerPluginConfigurationFromProperties() {
482 createProjectPom("<groupId>test</groupId>" +
483 "<artifactId>project</artifactId>" +
484 "<version>1</version>" +
487 " <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n" +
488 " <maven.compiler.source>1.7</maven.compiler.source>\n" +
489 " <maven.compiler.target>1.7</maven.compiler.target>\n" +
494 assertEquals("1.7", getMavenProject().getSourceLevel());
495 assertEquals("1.7", getMavenProject().getTargetLevel());
499 public void testCompilerPluginConfigurationFromPropertiesOverride() {
500 createProjectPom("<groupId>test</groupId>" +
501 "<artifactId>project</artifactId>" +
502 "<version>1</version>" +
505 " <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n" +
506 " <maven.compiler.source>1.7</maven.compiler.source>\n" +
507 " <maven.compiler.target>1.7</maven.compiler.target>\n" +
513 " <groupId>org.apache.maven.plugins</groupId>" +
514 " <artifactId>maven-compiler-plugin</artifactId>" +
516 " <target>1.4</target>" +
517 " <source>1.4</source>" +
518 " </configuration>" +
525 assertEquals("1.4", getMavenProject().getSourceLevel());
526 assertEquals("1.4", getMavenProject().getTargetLevel());
530 public void testCompilerPluginConfigurationRelease() {
531 createProjectPom("<groupId>test</groupId>" +
532 "<artifactId>project</artifactId>" +
533 "<version>1</version>" +
538 " <groupId>org.apache.maven.plugins</groupId>" +
539 " <artifactId>maven-compiler-plugin</artifactId>" +
540 " <version>3.6.0</version>" +
542 " <release>7</release>" +
543 " </configuration>" +
550 assertEquals(LanguageLevel.JDK_1_7, LanguageLevel.parse(getMavenProject().getReleaseLevel()));
551 assertEquals(LanguageLevel.JDK_1_7, LanguageLevelUtil.getCustomLanguageLevel(getModule("project")));
552 assertEquals(LanguageLevel.JDK_1_7,
553 LanguageLevel.parse(CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(getModule("project"))));
557 public void testCompilerPluginConfigurationCompilerArguments() {
558 importProject("<groupId>test</groupId>" +
559 "<artifactId>project</artifactId>" +
560 "<version>1</version>" +
565 " <groupId>org.apache.maven.plugins</groupId>" +
566 " <artifactId>maven-compiler-plugin</artifactId>" +
568 " <compilerArguments>" +
569 " <Averbose>true</Averbose>" +
570 " <parameters></parameters>" +
571 " <bootclasspath>rt.jar_path_here</bootclasspath>" +
572 " </compilerArguments>" +
573 " </configuration>" +
578 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
579 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
580 assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")),
581 "-Averbose=true", "-parameters", "-bootclasspath", "rt.jar_path_here");
585 public void testCompilerPluginConfigurationCompilerArgumentsParameters() {
586 importProject("<groupId>test</groupId>" +
587 "<artifactId>project</artifactId>" +
588 "<version>1</version>" +
592 " <groupId>org.apache.maven.plugins</groupId>" +
593 " <artifactId>maven-compiler-plugin</artifactId>" +
595 " <parameters>true</parameters>" +
596 " </configuration>" +
601 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
602 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
603 assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")),"-parameters");
607 public void testCompilerPluginConfigurationCompilerArgumentsParametersFalse() {
608 importProject("<groupId>test</groupId>" +
609 "<artifactId>project</artifactId>" +
610 "<version>1</version>" +
614 " <groupId>org.apache.maven.plugins</groupId>" +
615 " <artifactId>maven-compiler-plugin</artifactId>" +
617 " <parameters>false</parameters>" +
618 " </configuration>" +
623 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
624 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
625 assertEmpty(compilerConfiguration.getAdditionalOptions(getModule("project")));
629 public void testCompilerPluginConfigurationCompilerArgumentsParametersPropertyOverride() {
630 importProject("<groupId>test</groupId>" +
631 "<artifactId>project</artifactId>" +
632 "<version>1</version>" +
634 " <maven.compiler.parameters>true</maven.compiler.parameters>" +
639 " <groupId>org.apache.maven.plugins</groupId>" +
640 " <artifactId>maven-compiler-plugin</artifactId>" +
642 " <parameters>false</parameters>" +
643 " </configuration>" +
648 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
649 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
650 assertEmpty(compilerConfiguration.getAdditionalOptions(getModule("project")));
654 public void testCompilerPluginConfigurationCompilerArgumentsParametersPropertyOverride1() {
655 importProject("<groupId>test</groupId>" +
656 "<artifactId>project</artifactId>" +
657 "<version>1</version>" +
659 " <maven.compiler.parameters>false</maven.compiler.parameters>" +
664 " <groupId>org.apache.maven.plugins</groupId>" +
665 " <artifactId>maven-compiler-plugin</artifactId>" +
667 " <parameters>true</parameters>" +
668 " </configuration>" +
673 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
674 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
675 assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")),"-parameters");
679 public void testCompilerPluginConfigurationCompilerArgumentsParametersProperty() {
680 importProject("<groupId>test</groupId>" +
681 "<artifactId>project</artifactId>" +
682 "<version>1</version>" +
684 " <maven.compiler.parameters>true</maven.compiler.parameters>" +
689 " <groupId>org.apache.maven.plugins</groupId>" +
690 " <artifactId>maven-compiler-plugin</artifactId>" +
695 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
696 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
697 assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")),"-parameters");
701 public void testCompilerPluginConfigurationCompilerArgumentsParametersPropertyFalse() {
702 importProject("<groupId>test</groupId>" +
703 "<artifactId>project</artifactId>" +
704 "<version>1</version>" +
706 " <maven.compiler.parameters>false</maven.compiler.parameters>" +
711 " <groupId>org.apache.maven.plugins</groupId>" +
712 " <artifactId>maven-compiler-plugin</artifactId>" +
717 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
718 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
719 assertEmpty(compilerConfiguration.getAdditionalOptions(getModule("project")));
723 public void testCompilerPluginConfigurationUnresolvedCompilerArguments() {
724 importProject("<groupId>test</groupId>" +
725 "<artifactId>project</artifactId>" +
726 "<version>1</version>" +
731 " <groupId>org.apache.maven.plugins</groupId>" +
732 " <artifactId>maven-compiler-plugin</artifactId>" +
734 " <compilerId>${maven.compiler.compilerId}</compilerId>" +
735 " <compilerArgument>${unresolvedArgument}</compilerArgument>" +
736 " <compilerArguments>" +
737 " <d>path/with/braces_${</d>" +
738 " <anotherStrangeArg>${_${foo}</anotherStrangeArg>" +
739 " </compilerArguments>" +
741 " <arg>${anotherUnresolvedArgument}</arg>" +
742 " <arg>-myArg</arg>" +
744 " </configuration>" +
749 CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
750 assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
751 assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")),
752 "-myArg", "-d", "path/with/braces_${");
755 // commenting the test as the errorProne module is not available to IJ community project
756 // TODO move the test to the errorProne module
757 //public void stestCompilerPluginErrorProneConfiguration() {
758 // importProject("<groupId>test</groupId>" +
759 // "<artifactId>project</artifactId>" +
760 // "<version>1</version>" +
765 // " <groupId>org.apache.maven.plugins</groupId>" +
766 // " <artifactId>maven-compiler-plugin</artifactId>" +
767 // " <configuration>" +
768 // " <compilerId>javac-with-errorprone</compilerId>" +
769 // " <compilerArgs>" +
770 // " <arg>-XepAllErrorsAsWarnings</arg>" +
771 // " </compilerArgs>" +
772 // " </configuration>" +
777 // CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
778 // assertEquals("error-prone", compilerConfiguration.getDefaultCompiler().getId());
779 // assertUnorderedElementsAreEqual(compilerConfiguration.getAdditionalOptions(getModule("project")), "-XepAllErrorsAsWarnings");
781 // importProject("<groupId>test</groupId>" +
782 // "<artifactId>project</artifactId>" +
783 // "<version>1</version>");
785 // assertEquals("Javac", compilerConfiguration.getDefaultCompiler().getId());
786 // assertEmpty(compilerConfiguration.getAdditionalOptions(getModule("project")));
790 public void testMergingPluginConfigurationFromBuildProfilesAndPluginsManagement() {
791 createProjectPom("<groupId>test</groupId>" +
792 "<artifactId>project</artifactId>" +
793 "<version>1</version>" +
801 " <groupId>org.apache.maven.plugins</groupId>" +
802 " <artifactId>maven-compiler-plugin</artifactId>" +
804 " <target>1.4</target>" +
805 " </configuration>" +
815 " <groupId>org.apache.maven.plugins</groupId>" +
816 " <artifactId>maven-compiler-plugin</artifactId>" +
818 " <debug>true</debug>" +
819 " </configuration>" +
822 " <pluginManagement>" +
825 " <groupId>org.apache.maven.plugins</groupId>" +
826 " <artifactId>maven-compiler-plugin</artifactId>" +
828 " <source>1.4</source>" +
829 " </configuration>" +
832 " </pluginManagement>" +
834 importProjectWithProfiles("one");
836 MavenPlugin plugin = findPlugin("org.apache.maven.plugins", "maven-compiler-plugin");
837 assertEquals("1.4", plugin.getConfigurationElement().getChildText("source"));
838 assertEquals("1.4", plugin.getConfigurationElement().getChildText("target"));
839 assertEquals("true", plugin.getConfigurationElement().getChildText("debug"));
843 public void testDoesNotCollectProfilesWithoutId() {
844 importProject("<groupId>test</groupId>" +
845 "<artifactId>project</artifactId>" +
846 "<version>1</version>" +
856 assertUnorderedElementsAreEqual(getMavenProject().getProfilesIds(), "one", "default");
860 public void testCollectingRepositories() {
861 importProject("<groupId>test</groupId>" +
862 "<artifactId>project</artifactId>" +
863 "<version>1</version>" +
868 " <url>https://repository.one.com</url>" +
872 " <url>https://repository.two.com</url>" +
876 List<MavenRemoteRepository> result = getMavenProject().getRemoteRepositories();
877 assertEquals(3, result.size());
878 assertEquals("one", result.get(0).getId());
879 assertEquals("two", result.get(1).getId());
880 assertEquals("central", result.get(2).getId());
884 public void testOverridingCentralRepository() {
885 importProject("<groupId>test</groupId>" +
886 "<artifactId>project</artifactId>" +
887 "<version>1</version>" +
891 " <id>central</id>" +
892 " <url>https://my.repository.com</url>" +
896 List<MavenRemoteRepository> result = getMavenProject().getRemoteRepositories();
897 assertEquals(1, result.size());
898 assertEquals("central", result.get(0).getId());
899 assertEquals("https://my.repository.com", result.get(0).getUrl());
903 public void testCollectingRepositoriesFromParent() {
904 VirtualFile m1 = createModulePom("p1",
905 "<groupId>test</groupId>" +
906 "<artifactId>p1</artifactId>" +
907 "<version>1</version>" +
908 "<packaging>pom</packaging>" +
913 " <url>https://repository.one.com</url>" +
917 " <url>https://repository.two.com</url>" +
921 VirtualFile m2 = createModulePom("p2",
922 "<groupId>test</groupId>" +
923 "<artifactId>p2</artifactId>" +
924 "<version>1</version>" +
927 " <groupId>test</groupId>" +
928 " <artifactId>p1</artifactId>" +
929 " <version>1</version>" +
932 importProjects(m1, m2);
934 List<MavenRemoteRepository> result = myProjectsTree.getRootProjects().get(0).getRemoteRepositories();
935 assertEquals(3, result.size());
936 assertEquals("one", result.get(0).getId());
937 assertEquals("two", result.get(1).getId());
938 assertEquals("central", result.get(2).getId());
940 result = myProjectsTree.getRootProjects().get(1).getRemoteRepositories();
941 assertEquals(3, result.size());
942 assertEquals("one", result.get(0).getId());
943 assertEquals("two", result.get(1).getId());
944 assertEquals("central", result.get(2).getId());
948 public void testResolveRemoteRepositories() throws IOException {
949 updateSettingsXml("<mirrors>\n" +
951 " <id>mirror</id>\n" +
952 " <url>https://test/mirror</url>\n" +
953 " <mirrorOf>repo,repo-pom</mirrorOf>\n" +
958 " <id>repo-test</id>\n" +
959 " <repositories>\n" +
962 " <url>https://settings/repo</url>" +
966 " <url>https://settings/repo1</url>" +
968 " </repositories>\n" +
971 "<activeProfiles>\n" +
972 " <activeProfile>repo-test</activeProfile>\n" +
973 "</activeProfiles>");
975 VirtualFile projectPom = createProjectPom("<groupId>test</groupId>" +
976 "<artifactId>test</artifactId>" +
977 "<version>1</version>" +
981 " <id>repo-pom</id>" +
982 " <url>https://pom/repo</url>" +
985 " <id>repo-pom1</id>" +
986 " <url>https://pom/repo1</url>" +
991 MavenProject project = MavenProjectsManager.getInstance(myProject).findProject(projectPom);
992 Assert.assertNotNull(project);
993 Set<String> repoIds = project.getRemoteRepositories().stream()
995 .collect(Collectors.toSet());
996 System.out.println(repoIds);
997 Assert.assertTrue(repoIds.contains("mirror"));
998 Assert.assertTrue(repoIds.contains("repo-pom1"));
999 Assert.assertTrue(repoIds.contains("repo1"));
1000 Assert.assertFalse(repoIds.contains("repo-pom"));
1001 Assert.assertFalse(repoIds.contains("repo"));
1005 public void testMavenModelMap() {
1006 importProject("<groupId>test</groupId>" +
1007 "<artifactId>project</artifactId>" +
1008 "<version>1</version>" +
1010 " <finalName>foo</finalName>" +
1013 " <groupId>group1</groupId>" +
1014 " <artifactId>id1</artifactId>" +
1015 " <version>1</version>" +
1020 MavenProject p = getMavenProject();
1021 Map<String,String> map = p.getModelMap();
1023 assertEquals("test", map.get("groupId"));
1024 assertEquals("foo", map.get("build.finalName"));
1025 assertEquals(new File(p.getDirectory(), "target").toString(), map.get("build.directory"));
1026 assertNull(map.get("build.plugins"));
1027 assertNull(map.get("build.pluginMap"));
1031 public void testDependenciesTree() {
1032 VirtualFile m1 = createModulePom("p1",
1033 "<groupId>test</groupId>" +
1034 "<artifactId>m1</artifactId>" +
1035 "<version>1</version>" +
1039 " <groupId>test</groupId>" +
1040 " <artifactId>m2</artifactId>" +
1041 " <version>1</version>" +
1044 " <groupId>test</groupId>" +
1045 " <artifactId>lib1</artifactId>" +
1046 " <version>1</version>" +
1050 VirtualFile m2 = createModulePom("p2",
1051 "<groupId>test</groupId>" +
1052 "<artifactId>m2</artifactId>" +
1053 "<version>1</version>" +
1057 " <groupId>junit</groupId>" +
1058 " <artifactId>junit</artifactId>" +
1059 " <version>4.0</version>" +
1062 " <groupId>test</groupId>" +
1063 " <artifactId>lib2</artifactId>" +
1064 " <version>1</version>" +
1068 importProjects(m1, m2);
1069 resolveDependenciesAndImport();
1071 assertDependenciesNodes(myProjectsTree.getRootProjects().get(0).getDependencyTree(),
1072 "test:m2:jar:1->(junit:junit:jar:4.0->(),test:lib2:jar:1->()),test:lib1:jar:1->()");
1076 public void testDependenciesTreeWithTypesAndClassifiers() {
1077 VirtualFile m1 = createModulePom("p1",
1078 "<groupId>test</groupId>" +
1079 "<artifactId>m1</artifactId>" +
1080 "<version>1</version>" +
1084 " <groupId>test</groupId>" +
1085 " <artifactId>m2</artifactId>" +
1086 " <version>1</version>" +
1087 " <type>pom</type>" +
1088 " <classifier>test</classifier>" +
1092 VirtualFile m2 = createModulePom("p2",
1093 "<groupId>test</groupId>" +
1094 "<artifactId>m2</artifactId>" +
1095 "<version>1</version>" +
1099 " <groupId>test</groupId>" +
1100 " <artifactId>lib</artifactId>" +
1101 " <version>1</version>" +
1105 importProjects(m1, m2);
1106 resolveDependenciesAndImport();
1108 assertDependenciesNodes(myProjectsTree.getRootProjects().get(0).getDependencyTree(),
1109 "test:m2:pom:test:1->(test:lib:jar:1->())");
1113 public void testDependenciesTreeWithConflict() {
1114 VirtualFile m1 = createModulePom("p1",
1115 "<groupId>test</groupId>" +
1116 "<artifactId>m1</artifactId>" +
1117 "<version>1</version>" +
1121 " <groupId>test</groupId>" +
1122 " <artifactId>m2</artifactId>" +
1123 " <version>1</version>" +
1126 " <groupId>test</groupId>" +
1127 " <artifactId>lib</artifactId>" +
1128 " <version>1</version>" +
1132 VirtualFile m2 = createModulePom("p2",
1133 "<groupId>test</groupId>" +
1134 "<artifactId>m2</artifactId>" +
1135 "<version>1</version>" +
1139 " <groupId>test</groupId>" +
1140 " <artifactId>lib</artifactId>" +
1141 " <version>2</version>" +
1145 importProjects(m1, m2);
1146 resolveDependenciesAndImport();
1148 List<MavenArtifactNode> nodes = myProjectsTree.getRootProjects().get(0).getDependencyTree();
1149 assertDependenciesNodes(nodes,
1150 "test:m2:jar:1->(test:lib:jar:2[CONFLICT:test:lib:jar:1]->())," +
1151 "test:lib:jar:1->()");
1152 assertSame(nodes.get(0).getDependencies().get(0).getRelatedArtifact(),
1153 nodes.get(1).getArtifact());
1157 public void testDependencyTreeDuplicates() {
1158 VirtualFile m1 = createModulePom("p1",
1159 "<groupId>test</groupId>" +
1160 "<artifactId>m1</artifactId>" +
1161 "<version>1</version>" +
1165 " <groupId>test</groupId>" +
1166 " <artifactId>m2</artifactId>" +
1167 " <version>1</version>" +
1170 " <groupId>test</groupId>" +
1171 " <artifactId>m3</artifactId>" +
1172 " <version>1</version>" +
1176 VirtualFile m2 = createModulePom("p2",
1177 "<groupId>test</groupId>" +
1178 "<artifactId>m2</artifactId>" +
1179 "<version>1</version>" +
1183 " <groupId>test</groupId>" +
1184 " <artifactId>lib</artifactId>" +
1185 " <version>1</version>" +
1189 VirtualFile m3 = createModulePom("p3",
1190 "<groupId>test</groupId>" +
1191 "<artifactId>m3</artifactId>" +
1192 "<version>1</version>" +
1196 " <groupId>test</groupId>" +
1197 " <artifactId>lib</artifactId>" +
1198 " <version>1</version>" +
1202 importProjects(m1, m2, m3);
1203 resolveDependenciesAndImport();
1205 List<MavenArtifactNode> nodes = myProjectsTree.findProject(m1).getDependencyTree();
1206 assertDependenciesNodes(nodes, "test:m2:jar:1->(test:lib:jar:1->()),test:m3:jar:1->(test:lib:jar:1[DUPLICATE:test:lib:jar:1]->())");
1208 assertSame(nodes.get(0).getDependencies().get(0).getArtifact(),
1209 nodes.get(1).getDependencies().get(0).getRelatedArtifact());
1212 protected void assertDependenciesNodes(List<MavenArtifactNode> nodes, String expected) {
1213 assertEquals(expected, StringUtil.join(nodes, ","));
1216 private String findPluginConfig(String groupId, String artifactId, String path) {
1217 return MavenJDOMUtil.findChildValueByPath(getMavenProject().getPluginConfiguration(groupId, artifactId), path);
1220 private String findPluginGoalConfig(String groupId, String artifactId, String goal, String path) {
1221 return MavenJDOMUtil.findChildValueByPath(getMavenProject().getPluginGoalConfiguration(groupId, artifactId, goal), path);
1224 private void assertDeclaredPlugins(PluginInfo... expected) {
1225 List<PluginInfo> defaultPlugins = Arrays.asList(
1226 p("org.apache.maven.plugins", "maven-site-plugin"),
1227 p("org.apache.maven.plugins", "maven-deploy-plugin"),
1228 p("org.apache.maven.plugins", "maven-compiler-plugin"),
1229 p("org.apache.maven.plugins", "maven-install-plugin"),
1230 p("org.apache.maven.plugins", "maven-jar-plugin"),
1231 p("org.apache.maven.plugins", "maven-clean-plugin"),
1232 p("org.apache.maven.plugins", "maven-resources-plugin"),
1233 p("org.apache.maven.plugins", "maven-surefire-plugin"));
1234 List<PluginInfo> expectedList = new ArrayList<>();
1235 expectedList.addAll(defaultPlugins);
1236 expectedList.addAll(Arrays.asList(expected));
1237 assertUnorderedElementsAreEqual(p(getMavenProject().getDeclaredPlugins()), expectedList);
1240 private MavenPlugin findPlugin(String groupId, String artifactId) {
1241 return getMavenProject().findPlugin(groupId, artifactId);
1244 private MavenProject getMavenProject() {
1245 return myProjectsTree.getRootProjects().get(0);
1248 private static PluginInfo p(String groupId, String artifactId) {
1249 return new PluginInfo(groupId, artifactId);
1252 private static PluginInfo p(MavenPlugin mavenPlugin) {
1253 return new PluginInfo(mavenPlugin.getGroupId(), mavenPlugin.getArtifactId());
1256 private List<PluginInfo> p(Collection<MavenPlugin> mavenPlugins) {
1257 List<PluginInfo> res = new ArrayList<>(mavenPlugins.size());
1258 for (MavenPlugin mavenPlugin : mavenPlugins) {
1259 res.add(p(mavenPlugin));
1265 private static final class PluginInfo {
1269 private PluginInfo(String groupId, String artifactId) {
1270 this.groupId = groupId;
1271 this.artifactId = artifactId;
1275 public String toString() {
1276 return groupId + ":" + artifactId;
1280 public boolean equals(Object o) {
1281 if (this == o) return true;
1282 if (o == null || getClass() != o.getClass()) return false;
1284 PluginInfo info = (PluginInfo)o;
1286 if (artifactId != null ? !artifactId.equals(info.artifactId) : info.artifactId != null) return false;
1287 if (groupId != null ? !groupId.equals(info.groupId) : info.groupId != null) return false;
1293 public int hashCode() {
1294 int result = groupId != null ? groupId.hashCode() : 0;
1295 result = 31 * result + (artifactId != null ? artifactId.hashCode() : 0);