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.*;
}
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);
assertActiveProfiles("settings", "implicit");
}
- public void testDoNotActivateDefaultProfilesWhenThereAreActiveProfilesInSettingsXml() throws Exception {
+ public void testDoNotActivateDefaultProfilesWhenThereAreOlwaysOnProfilesInPomXml() throws Exception {
updateSettingsXml("<activeProfiles>" +
" <activeProfile>settings</activeProfile>" +
"</activeProfiles>");
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);