Maven: even more correct profiles activation logic
authorAnton.Makeev <Anton.Makeev@jetbrains.com>
Tue, 22 Dec 2009 12:26:48 +0000 (15:26 +0300)
committerAnton.Makeev <Anton.Makeev@jetbrains.com>
Tue, 22 Dec 2009 12:26:48 +0000 (15:26 +0300)
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectReader.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectReaderTest.java

index 3a644a003b34604a7b2d24381fd473b999f3692e..e63dec2601d4ec958c27fd5382d0ef155ae8da5c 100644 (file)
@@ -19,6 +19,7 @@ import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.Pair;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.util.containers.ContainerUtil;
 import gnu.trove.THashMap;
 import gnu.trove.THashSet;
 import org.apache.maven.model.*;
@@ -337,46 +338,53 @@ public class MavenProjectReader {
   }
 
   private List<Profile> applyProfiles(Model model, File basedir, Collection<String> explicitProfiles, Collection<String> alwaysOnProfiles) {
-    List<Profile> activated = new ArrayList<Profile>();
+    List<Profile> activatedPom = new ArrayList<Profile>();
+    List<Profile> activatedExternal = new ArrayList<Profile>();
     List<Profile> activeByDefault = new ArrayList<Profile>();
 
     List<Profile> rawProfiles = model.getProfiles();
-    List<Profile> expandedProfiles = null;
+    List<Profile> expandedProfilesCache = null;
 
     for (int i = 0; i < rawProfiles.size(); i++) {
       Profile eachRawProfile = rawProfiles.get(i);
 
-      if (explicitProfiles.contains(eachRawProfile.getId())
-          || alwaysOnProfiles.contains(eachRawProfile.getId())) {
-        activated.add(eachRawProfile);
-        continue;
-      }
+      boolean shouldAdd = explicitProfiles.contains(eachRawProfile.getId()) || alwaysOnProfiles.contains(eachRawProfile.getId());
 
       Activation activation = eachRawProfile.getActivation();
-      if (activation == null) continue;
-
-      if (activation.isActiveByDefault()) {
-        activeByDefault.add(eachRawProfile);
-      }
+      if (activation != null) {
+        if (activation.isActiveByDefault()) {
+          activeByDefault.add(eachRawProfile);
+        }
 
-      // expand only if necessary
-      if (expandedProfiles == null) expandedProfiles = expandProperties(model, basedir).getProfiles();
-      Profile eachExpandedProfile = expandedProfiles.get(i);
+        // expand only if necessary
+        if (expandedProfilesCache == null) expandedProfilesCache = expandProperties(model, basedir).getProfiles();
+        Profile eachExpandedProfile = expandedProfilesCache.get(i);
 
-      for (ProfileActivator eachActivator : getProfileActivators()) {
-        try {
-          if (eachActivator.canDetermineActivation(eachExpandedProfile) && eachActivator.isActive(eachExpandedProfile)) {
-            activated.add(eachRawProfile);
-            break;
+        for (ProfileActivator eachActivator : getProfileActivators()) {
+          try {
+            if (eachActivator.canDetermineActivation(eachExpandedProfile) && eachActivator.isActive(eachExpandedProfile)) {
+              shouldAdd = true;
+              break;
+            }
+          }
+          catch (ProfileActivationException e) {
+            MavenLog.LOG.warn(e);
           }
         }
-        catch (ProfileActivationException e) {
-          MavenLog.LOG.warn(e);
+      }
+
+      if (shouldAdd) {
+        if (PROFILE_FROM_POM.equals(eachRawProfile.getSource())) {
+          activatedPom.add(eachRawProfile);
+        }
+        else {
+          activatedExternal.add(eachRawProfile);
         }
       }
     }
 
-    List<Profile> activatedProfiles = activated.isEmpty() ? activeByDefault : activated;
+    List<Profile> activatedProfiles = new ArrayList<Profile>(activatedPom.isEmpty() ? activeByDefault : activatedPom);
+    activatedProfiles.addAll(activatedExternal);
 
     for (Profile each : activatedProfiles) {
       new DefaultProfileInjector().inject(each, model);
index 13267d26de92574baf80fb2c80688c8c6266e28e..1de406927f9e0ea7564f402d6206d2cf51e78990 100644 (file)
@@ -1341,7 +1341,7 @@ public class MavenProjectReaderTest extends MavenTestCase {
     assertActiveProfiles("settings", "implicit");
   }
 
-  public void testDoNotActivateDefaultProfilesWhenThereAreActiveProfilesInSettingsXml() throws Exception {
+  public void testDoNotActivateDefaultProfilesWhenThereAreOlwaysOnProfilesInPomXml() throws Exception {
     updateSettingsXml("<activeProfiles>" +
                       "  <activeProfile>settings</activeProfile>" +
                       "</activeProfiles>");
@@ -1361,6 +1361,53 @@ public class MavenProjectReaderTest extends MavenTestCase {
     assertActiveProfiles("settings");
   }
 
+  public void testActivateDefaultProfilesWhenThereAreActiveProfilesInSettingsXml() throws Exception {
+    updateSettingsXml("<profiles>" +
+                      "  <profile>" +
+                      "    <id>settings</id>" +
+                      "  </profile>" +
+                      "</profiles>" +
+                      "<activeProfiles>" +
+                      "  <activeProfile>settings</activeProfile>" +
+                      "</activeProfiles>");
+
+    createProjectPom("<profiles>" +
+                     "  <profile>" +
+                     "    <id>default</id>" +
+                     "    <activation>" +
+                     "      <activeByDefault>true</activeByDefault>" +
+                     "    </activation>" +
+                     "  </profile>" +
+                     "</profiles>");
+
+    assertActiveProfiles("default", "settings");
+  }
+
+  public void testActivateDefaultProfilesWhenThereAreActiveProfilesInProfilesXml() throws Exception {
+    createFullProfilesXml("<?xml version=\"1.0\"?>" +
+                          "<profilesXml>" +
+                          "  <profiles>" +
+                          "    <profile>" +
+                          "      <id>profiles</id>" +
+                          "    </profile>" +
+                          "  </profiles>" +
+                          "  <activeProfiles>" +
+                          "    <activeProfile>profiles</activeProfile>" +
+                          "  </activeProfiles>" +
+                          "</profilesXml>");
+
+    createProjectPom("<profiles>" +
+                     "  <profile>" +
+                     "    <id>default</id>" +
+                     "    <activation>" +
+                     "      <activeByDefault>true</activeByDefault>" +
+                     "    </activation>" +
+                     "  </profile>" +
+                     "</profiles>");
+
+    assertActiveProfiles("default", "profiles");
+  }
+
   private org.apache.maven.project.MavenProject readProject(VirtualFile file, String... profiles) {
     MavenProjectReaderResult readResult = readProject(file, new NullProjectLocator(), profiles);
     assertProblems(readResult);