Skip to content

Commit c56bd15

Browse files
authored
Add java.configuration.maven.defaultMojoExecutionAction (#2426)
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
1 parent f08dfcb commit c56bd15

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporter.java

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.eclipse.m2e.core.embedder.MavenModelManager;
5252
import org.eclipse.m2e.core.internal.IMavenConstants;
5353
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
54+
import org.eclipse.m2e.core.lifecyclemapping.model.PluginExecutionAction;
5455
import org.eclipse.m2e.core.project.IMavenProjectImportResult;
5556
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
5657
import org.eclipse.m2e.core.project.LocalProjectScanner;
@@ -159,6 +160,8 @@ public void importToWorkspace(IProgressMonitor monitor) throws CoreException, Op
159160
MavenConfigurationImpl configurationImpl = (MavenConfigurationImpl)MavenPlugin.getMavenConfiguration();
160161
configurationImpl.setDownloadSources(JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isMavenDownloadSources());
161162
configurationImpl.setNotCoveredMojoExecutionSeverity(JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getMavenNotCoveredPluginExecutionSeverity());
163+
PluginExecutionAction action = PluginExecutionAction.valueOf(JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getMavenDefaultMojoExecutionAction());
164+
configurationImpl.setDefaultMojoExecutionAction(action);
162165
SubMonitor subMonitor = SubMonitor.convert(monitor, 105);
163166
subMonitor.setTaskName(IMPORTING_MAVEN_PROJECTS);
164167
Set<MavenProjectInfo> files = getMavenProjectInfo(subMonitor.split(5));

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
*/
7979
public class Preferences {
8080

81+
private static final String IGNORE = "ignore";
8182
public static final String LINE = "line";
8283
/**
8384
* Specifies the folder path to the JDK .
@@ -306,6 +307,8 @@ public class Preferences {
306307

307308
public static final String MAVEN_NOT_COVERED_PLUGIN_EXECUTION_SEVERITY = "java.configuration.maven.notCoveredPluginExecutionSeverity";
308309

310+
public static final String MAVEN_DEFAULT_MOJO_EXECUTION_ACTION = "java.configuration.maven.defaultMojoExecutionAction";
311+
309312
/**
310313
* Preference key to enable/disable the 'completion'.
311314
*/
@@ -596,6 +599,7 @@ public class Preferences {
596599
private String mavenUserSettings;
597600
private String mavenGlobalSettings;
598601
private String mavenNotCoveredPluginExecutionSeverity;
602+
private String mavenDefaultMojoExecutionAction;
599603

600604
private List<String> javaCompletionFavoriteMembers;
601605
private List<?> gradleWrapperList;
@@ -853,7 +857,8 @@ public Preferences() {
853857
includeSourceMethodDeclarations = false;
854858
insertSpaces = true;
855859
tabSize = DEFAULT_TAB_SIZE;
856-
mavenNotCoveredPluginExecutionSeverity = "ignore";
860+
mavenNotCoveredPluginExecutionSeverity = IGNORE;
861+
mavenDefaultMojoExecutionAction = IGNORE;
857862
inlayHintsParameterMode = InlayHintsParameterMode.LITERALS;
858863
projectEncoding = ProjectEncodingMode.IGNORE;
859864
avoidVolatileChanges = true;
@@ -1080,9 +1085,12 @@ public static Preferences createFrom(Map<String, Object> configuration) {
10801085
String mavenGlobalSettings = getString(configuration, MAVEN_GLOBAL_SETTINGS_KEY, null);
10811086
prefs.setMavenGlobalSettings(mavenGlobalSettings);
10821087

1083-
String mavenNotCoveredPluginExecution = getString(configuration, MAVEN_NOT_COVERED_PLUGIN_EXECUTION_SEVERITY, "ignore");
1088+
String mavenNotCoveredPluginExecution = getString(configuration, MAVEN_NOT_COVERED_PLUGIN_EXECUTION_SEVERITY, IGNORE);
10841089
prefs.setMavenNotCoveredPluginExecutionSeverity(mavenNotCoveredPluginExecution);
10851090

1091+
String mavenDefaultMojoExecution = getString(configuration, MAVEN_DEFAULT_MOJO_EXECUTION_ACTION, IGNORE);
1092+
prefs.setMavenDefaultMojoExecutionAction(mavenDefaultMojoExecution);
1093+
10861094
String sortOrder = getString(configuration, MEMBER_SORT_ORDER, null);
10871095
prefs.setMembersSortOrder(sortOrder);
10881096

@@ -1816,8 +1824,31 @@ public String getMavenNotCoveredPluginExecutionSeverity() {
18161824
return mavenNotCoveredPluginExecutionSeverity;
18171825
}
18181826

1819-
public void setMavenNotCoveredPluginExecutionSeverity(String mavenNotCoveredPluginExecutionSeverity) {
1827+
public Preferences setMavenNotCoveredPluginExecutionSeverity(String mavenNotCoveredPluginExecutionSeverity) {
18201828
this.mavenNotCoveredPluginExecutionSeverity = mavenNotCoveredPluginExecutionSeverity;
1829+
return this;
1830+
}
1831+
1832+
public String getMavenDefaultMojoExecutionAction() {
1833+
return mavenDefaultMojoExecutionAction;
1834+
}
1835+
1836+
public Preferences setMavenDefaultMojoExecutionAction(String mavenDefaultMojoExecutionAction) {
1837+
if (mavenDefaultMojoExecutionAction == null) {
1838+
mavenDefaultMojoExecutionAction = IGNORE;
1839+
}
1840+
switch (mavenDefaultMojoExecutionAction) {
1841+
case IGNORE:
1842+
case "execute":
1843+
case "warn":
1844+
case "error":
1845+
break;
1846+
default:
1847+
mavenDefaultMojoExecutionAction = IGNORE;
1848+
break;
1849+
}
1850+
this.mavenDefaultMojoExecutionAction = mavenDefaultMojoExecutionAction;
1851+
return this;
18211852
}
18221853

18231854
public String[] getImportOrder() {

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/StandardPreferenceManager.java

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
3838
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;
3939
import org.eclipse.m2e.core.internal.preferences.ProblemSeverity;
40+
import org.eclipse.m2e.core.lifecyclemapping.model.PluginExecutionAction;
4041

4142
/**
4243
* Preference manager
@@ -132,13 +133,29 @@ public void update(Preferences preferences) {
132133
}
133134
String newMavenNotCoveredExecutionSeverity = preferences.getMavenNotCoveredPluginExecutionSeverity();
134135
String oldMavenNotCoveredExecutionSeverity = getMavenConfiguration().getNotCoveredMojoExecutionSeverity();
136+
boolean updateMavenProjects = false;
135137
if (!Objects.equals(newMavenNotCoveredExecutionSeverity, oldMavenNotCoveredExecutionSeverity)) {
136138
try {
137139
((MavenConfigurationImpl) getMavenConfiguration()).setNotCoveredMojoExecutionSeverity(newMavenNotCoveredExecutionSeverity);
140+
updateMavenProjects = true;
138141
} catch (CoreException e) {
139142
JavaLanguageServerPlugin.logException("failed to set not covered Maven plugin execution severity", e);
140143
}
141144
}
145+
String newMavenDefaultMavenExecutionAction = preferences.getMavenDefaultMojoExecutionAction();
146+
String oldMavenDefaultMavenExecutionAction = getMavenConfiguration().getDefaultMojoExecutionAction() == null ? null : getMavenConfiguration().getDefaultMojoExecutionAction().name();
147+
if (!Objects.equals(newMavenDefaultMavenExecutionAction, oldMavenDefaultMavenExecutionAction)) {
148+
PluginExecutionAction action = PluginExecutionAction.valueOf(newMavenDefaultMavenExecutionAction == null ? "ignore" : newMavenDefaultMavenExecutionAction);
149+
getMavenConfiguration().setDefaultMojoExecutionAction(action);
150+
updateMavenProjects = true;
151+
}
152+
if (updateMavenProjects) {
153+
for (IProject project : ProjectUtils.getAllProjects()) {
154+
if (ProjectUtils.isMavenProject(project)) {
155+
JavaLanguageServerPlugin.getProjectsManager().updateProject(project, true);
156+
}
157+
}
158+
}
142159
updateParallelBuild(preferences.getMaxConcurrentBuilds());
143160
boolean mavenOffline = preferences.isMavenOffline();
144161
IEclipsePreferences store = DefaultScope.INSTANCE.getNode(IMavenConstants.PLUGIN_ID);

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void setup() throws Exception {
7878
when(p.getProjectConfigurations()).thenReturn(null);
7979
when(preferenceManager.getPreferences()).thenReturn(p);
8080
when(p.isRenameEnabled()).thenReturn(true);
81+
when(p.getMavenDefaultMojoExecutionAction()).thenReturn("ignore");
8182
handler = new RenameHandler(preferenceManager);
8283
}
8384

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SignatureHelpHandlerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void setup() throws Exception {
8383
when(p.isImportMavenEnabled()).thenReturn(true);
8484
when(p.isSignatureHelpEnabled()).thenReturn(true);
8585
when(p.isSignatureHelpDescriptionEnabled()).thenReturn(false);
86+
when(p.getMavenDefaultMojoExecutionAction()).thenReturn("ignore");
8687
handler = new SignatureHelpHandler(preferenceManager);
8788
}
8889

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManagerTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
4141
import org.eclipse.m2e.core.internal.IMavenConstants;
4242
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;
43+
import org.eclipse.m2e.core.lifecyclemapping.model.PluginExecutionAction;
4344
import org.junit.Before;
4445
import org.junit.Test;
4546
import org.junit.runner.RunWith;
@@ -73,6 +74,7 @@ public void testUpdateMavenSettings() throws Exception {
7374
reset(mavenConfig);
7475
when(mavenConfig.getUserSettingsFile()).thenReturn(path);
7576
when(mavenConfig.getNotCoveredMojoExecutionSeverity()).thenReturn("ignore");
77+
when(mavenConfig.getDefaultMojoExecutionAction()).thenReturn(PluginExecutionAction.ignore);
7678
preferenceManager.update(preferences);
7779
verify(mavenConfig, never()).setUserSettingsFile(anyString());
7880

@@ -250,4 +252,24 @@ private boolean getDisableTestFlag() throws CoreException {
250252
}
251253
return disableTest;
252254
}
255+
256+
@Test
257+
public void testMavenDefaultMojoExecution() throws Exception {
258+
try {
259+
PreferenceManager.initialize();
260+
Preferences preferences = new Preferences();
261+
preferenceManager.update(preferences);
262+
assertEquals("ignore", preferenceManager.getPreferences().getMavenDefaultMojoExecutionAction());
263+
preferences.setMavenDefaultMojoExecutionAction("warn");
264+
preferenceManager.update(preferences);
265+
assertEquals("warn", preferenceManager.getPreferences().getMavenDefaultMojoExecutionAction());
266+
preferences.setMavenDefaultMojoExecutionAction("unknown");
267+
preferenceManager.update(preferences);
268+
assertEquals("ignore", preferenceManager.getPreferences().getMavenDefaultMojoExecutionAction());
269+
} finally {
270+
Preferences preferences = new Preferences();
271+
preferenceManager.update(preferences);
272+
assertEquals("ignore", preferenceManager.getPreferences().getMavenDefaultMojoExecutionAction());
273+
}
274+
}
253275
}

0 commit comments

Comments
 (0)