IDEA-51438 (Maven: Allow explicit disabling of profiles)

Fixed for Maven Run Configuration.
This commit is contained in:
Sergey Evdokimov
2012-02-15 15:07:51 +04:00
parent 4fc494cb65
commit f74fa60139
7 changed files with 171 additions and 33 deletions
+1
View File
@@ -54,6 +54,7 @@
<orderEntry type="module" module-name="testFramework-java" exported="" scope="TEST" />
<orderEntry type="module" module-name="jetgroovy" scope="TEST" />
<orderEntry type="module" module-name="maven2-server-impl" scope="RUNTIME" />
<orderEntry type="library" name="Guava" level="project" />
</component>
<component name="copyright">
<Base>
@@ -37,7 +37,6 @@ import org.jetbrains.idea.maven.utils.MavenUtil;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;
/**
@@ -144,7 +143,7 @@ public class MavenExternalParameters {
parametersList.add(goal);
}
addOption(parametersList, "P", encodeProfiles(parameters.getProfiles()));
addOption(parametersList, "P", encodeProfiles(parameters.getProfilesMap()));
}
private static void addOption(ParametersList cmdList, @NonNls String key, @NonNls String value) {
@@ -233,13 +232,16 @@ public class MavenExternalParameters {
}
}
private static String encodeProfiles(final Collection<String> profiles) {
final StringBuilder stringBuilder = new StringBuilder();
for (String profile : profiles) {
private static String encodeProfiles(Map<String, Boolean> profiles) {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, Boolean> entry : profiles.entrySet()) {
if (stringBuilder.length() != 0) {
stringBuilder.append(",");
}
stringBuilder.append(profile);
if (!entry.getValue()) {
stringBuilder.append("-");
}
stringBuilder.append(entry.getKey());
}
return stringBuilder.toString();
}
@@ -143,10 +143,7 @@ public class MavenRunConfiguration extends RunConfigurationBase implements Locat
if (mySettings.myRunnerParameters == null) mySettings.myRunnerParameters = new MavenRunnerParameters();
// fix old settings format
File workingDir = mySettings.myRunnerParameters.getWorkingDirFile();
if (MavenConstants.POM_XML.equals(workingDir.getName())) {
mySettings.myRunnerParameters.setWorkingDirPath(workingDir.getParent());
}
mySettings.myRunnerParameters.fixAfterLoadingFromOldFormat();
}
}
@@ -15,6 +15,13 @@
*/
package org.jetbrains.idea.maven.execution;
import com.google.common.base.Predicates;
import com.google.common.collect.Maps;
import com.intellij.util.xmlb.annotations.OptionTag;
import com.intellij.util.xmlb.annotations.Transient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.utils.Path;
import java.io.File;
@@ -24,21 +31,53 @@ public class MavenRunnerParameters implements Cloneable {
private boolean isPomExecution;
private Path myWorkingDirPath;
private List<String> myGoals;
private SortedSet<String> myProfiles;
private final Map<String, Boolean> myProfilesMap = new LinkedHashMap<String, Boolean>();
private final Collection<String> myEnabledProfilesForXmlSerializer = new TreeSet<String>();
public MavenRunnerParameters() {
this(true, "", null, null);
this(true, "", null, null, null);
}
public MavenRunnerParameters(boolean isPomExecution, String workingDirPath, List<String> goals, Collection<String> profiles) {
public MavenRunnerParameters(boolean isPomExecution, String workingDirPath,
@Nullable List<String> goals,
@Nullable Collection<String> explicitEnabledProfiles) {
this(isPomExecution, workingDirPath, goals, explicitEnabledProfiles, null);
}
public MavenRunnerParameters(boolean isPomExecution, String workingDirPath,
@Nullable List<String> goals,
@Nullable Collection<String> explicitEnabledProfiles,
@Nullable Collection<String> explicitDisabledProfiles) {
this.isPomExecution = isPomExecution;
setWorkingDirPath(workingDirPath);
setGoals(goals);
setProfiles(profiles);
if (explicitEnabledProfiles != null) {
for (String profile : explicitEnabledProfiles) {
myProfilesMap.put(profile, Boolean.TRUE);
}
}
if (explicitDisabledProfiles != null) {
for (String profile : explicitDisabledProfiles) {
myProfilesMap.put(profile, Boolean.FALSE);
}
}
}
public MavenRunnerParameters(String workingDirPath, boolean isPomExecution,
@Nullable List<String> goals,
@NotNull Map<String, Boolean> profilesMap) {
this.isPomExecution = isPomExecution;
setWorkingDirPath(workingDirPath);
setGoals(goals);
setProfilesMap(profilesMap);
}
public MavenRunnerParameters(MavenRunnerParameters that) {
this(that.isPomExecution, that.getWorkingDirPath(), that.myGoals, that.myProfiles);
this(that.getWorkingDirPath(), that.isPomExecution, that.myGoals, that.myProfilesMap);
}
public boolean isPomExecution() {
@@ -57,6 +96,7 @@ public class MavenRunnerParameters implements Cloneable {
return new File(myWorkingDirPath.getPath());
}
@Nullable
public String getPomFilePath() {
if (!isPomExecution) return null;
return new File(myWorkingDirPath.getPath(), "pom.xml").getPath();
@@ -73,14 +113,68 @@ public class MavenRunnerParameters implements Cloneable {
}
}
public Collection<String> getProfiles() {
return myProfiles;
@Deprecated // Must be used by XML Serializer only!!!
@OptionTag("profiles")
public Collection<String> getEnabledProfilesForXmlSerializer() {
return myEnabledProfilesForXmlSerializer;
}
public void setProfiles(Collection<String> profiles) {
myProfiles = new TreeSet<String>();
@Deprecated // Must be used by XML Serializer only!!!
public void setEnabledProfilesForXmlSerializer(@Nullable Collection<String> enabledProfilesForXmlSerializer) {
if (enabledProfilesForXmlSerializer != null) {
if (myEnabledProfilesForXmlSerializer == enabledProfilesForXmlSerializer) return; // Called from XML Serializer
myEnabledProfilesForXmlSerializer.retainAll(enabledProfilesForXmlSerializer);
myEnabledProfilesForXmlSerializer.addAll(enabledProfilesForXmlSerializer);
}
}
public void fixAfterLoadingFromOldFormat() {
for (String profile : myEnabledProfilesForXmlSerializer) {
myProfilesMap.put(profile, true);
}
myEnabledProfilesForXmlSerializer.clear();
File workingDir = getWorkingDirFile();
if (MavenConstants.POM_XML.equals(workingDir.getName())) {
setWorkingDirPath(workingDir.getParent());
}
}
@OptionTag("profilesMap")
public Map<String, Boolean> getProfilesMap() {
return myProfilesMap;
}
public void setProfilesMap(@NotNull Map<String, Boolean> profilesMap) {
if (myProfilesMap == profilesMap) return; // Called from XML Serializer
myProfilesMap.clear();
for (Map.Entry<String, Boolean> entry : profilesMap.entrySet()) {
if (entry.getValue() != null) {
myProfilesMap.put(entry.getKey(), entry.getValue());
}
}
}
/**
* Was left for compatibility with old plugins.
* @deprecated use getProfileMap()
* @return
*/
@Transient
public Collection<String> getProfiles() {
return Maps.filterValues(myProfilesMap, Predicates.equalTo(true)).keySet();
}
/**
* Was left for compatibility with old plugins.
* @deprecated use getProfileMap()
* @param profiles
*/
public void setProfiles(@Nullable Collection<String> profiles) {
if (profiles != null) {
myProfiles.addAll(profiles);
for (String profile : profiles) {
myProfilesMap.put(profile, true);
}
}
}
@@ -97,7 +191,7 @@ public class MavenRunnerParameters implements Cloneable {
if (isPomExecution != that.isPomExecution) return false;
if (myGoals != null ? !myGoals.equals(that.myGoals) : that.myGoals != null) return false;
if (myWorkingDirPath != null ? !myWorkingDirPath.equals(that.myWorkingDirPath) : that.myWorkingDirPath != null) return false;
if (myProfiles != null ? !myProfiles.equals(that.myProfiles) : that.myProfiles != null) return false;
if (!myProfilesMap.equals(that.myProfilesMap)) return false;
return true;
}
@@ -107,7 +201,7 @@ public class MavenRunnerParameters implements Cloneable {
result = isPomExecution ? 1 : 0;
result = 31 * result + (myWorkingDirPath != null ? myWorkingDirPath.hashCode() : 0);
result = 31 * result + (myGoals != null ? myGoals.hashCode() : 0);
result = 31 * result + (myProfiles != null ? myProfiles.hashCode() : 0);
result = 31 * result + myProfilesMap.hashCode();
return result;
}
}
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.idea.maven.execution.MavenRunnerParametersConfigurable">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
@@ -20,7 +20,7 @@
</component>
<vspacer id="3a9c4">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="2a808" class="com.intellij.openapi.ui.LabeledComponent" binding="goalsComponent">
@@ -44,6 +44,15 @@
<text value="&amp;Profiles (separated with space)"/>
</properties>
</component>
<component id="f825e" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="5" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="10"/>
<text value="(add prefix '-' to disable profile, e.g. &quot;-test&quot;)"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -38,10 +38,7 @@ import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.utils.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
* @author Vladislav.Kaznacheev
@@ -66,6 +63,16 @@ public abstract class MavenRunnerParametersConfigurable implements Configurable,
if (!project.isDefault()) {
MyCompletionProvider profilesCompletionProvider = new MyCompletionProvider(project) {
@NotNull
@Override
protected String getPrefix(@NotNull String currentTextPrefix) {
String prefix = super.getPrefix(currentTextPrefix);
if (prefix.startsWith("-") || prefix.startsWith("!")) {
prefix = prefix.substring(1);
}
return prefix;
}
@Override
protected void addVariants(@NotNull CompletionResultSet result, MavenProjectsManager manager) {
for (String profile : manager.getAvailableProfiles()) {
@@ -155,13 +162,40 @@ public abstract class MavenRunnerParametersConfigurable implements Configurable,
private void setData(final MavenRunnerParameters data) {
data.setWorkingDirPath(workingDirComponent.getComponent().getText());
data.setGoals(Strings.tokenize(goalsComponent.getComponent().getText(), " "));
data.setProfiles(Strings.tokenize(profilesComponent.getComponent().getText(), " "));
Map<String, Boolean> profilesMap = new LinkedHashMap<String, Boolean>();
for (String profile : Strings.tokenize(profilesComponent.getComponent().getText(), " ,;")) {
Boolean isEnabled = true;
if (profile.startsWith("-") || profile.startsWith("!")) {
profile = profile.substring(1);
if (profile.isEmpty()) continue;
isEnabled = false;
}
profilesMap.put(profile, isEnabled);
}
data.setProfilesMap(profilesMap);
}
private void getData(final MavenRunnerParameters data) {
workingDirComponent.getComponent().setText(data.getWorkingDirPath());
goalsComponent.getComponent().setText(Strings.detokenize(data.getGoals(), ' '));
profilesComponent.getComponent().setText(Strings.detokenize(data.getProfiles(), ' '));
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Boolean> entry : data.getProfilesMap().entrySet()) {
if (sb.length() != 0) {
sb.append(" ");
}
if (!entry.getValue()) {
sb.append("-");
}
sb.append(entry.getKey());
}
profilesComponent.getComponent().setText(sb.toString());
}
protected abstract MavenRunnerParameters getParameters();
@@ -179,7 +213,7 @@ public abstract class MavenRunnerParametersConfigurable implements Configurable,
profilesComponent.setAnchor(anchor);
}
private static abstract class MyCompletionProvider extends TextFieldCompletionProvider {
private abstract class MyCompletionProvider extends TextFieldCompletionProvider {
private final Project myProject;
protected MyCompletionProvider(Project project) {
@@ -15,6 +15,7 @@
*/
package org.jetbrains.idea.maven.execution;
import com.google.common.collect.ImmutableMap;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
@@ -26,7 +27,7 @@ public class MavenRunConfigurationTest extends IdeaTestCase {
MavenRunConfiguration.MavenSettings s = new MavenRunConfiguration.MavenSettings(myProject);
s.myRunnerParameters.setWorkingDirPath("some path");
s.myRunnerParameters.setGoals(Arrays.asList("clean validate"));
s.myRunnerParameters.setProfiles(Arrays.asList("prof1 prof2"));
s.myRunnerParameters.setProfilesMap(ImmutableMap.of("prof1", true, "prof2", true, "prof3", false));
Element xml = XmlSerializer.serialize(s);
MavenRunConfiguration.MavenSettings loaded
@@ -34,6 +35,6 @@ public class MavenRunConfigurationTest extends IdeaTestCase {
assertEquals(s.myRunnerParameters.getWorkingDirPath(), loaded.myRunnerParameters.getWorkingDirPath());
assertEquals(s.myRunnerParameters.getGoals(), loaded.myRunnerParameters.getGoals());
assertEquals(s.myRunnerParameters.getProfiles(), loaded.myRunnerParameters.getProfiles());
assertEquals(s.myRunnerParameters.getProfilesMap(), loaded.myRunnerParameters.getProfilesMap());
}
}