Skip to content

Commit 2ef6b03

Browse files
committed
Use constants for Maven CLI arguments.
1 parent 6f443d2 commit 2ef6b03

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

org.eclipse.m2e.core.tests/src/org/eclipse/m2e/internal/launch/MavenLaunchDelegateTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Optional;
2424

2525
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26+
import org.apache.maven.cli.CLIManager;
2627
import org.eclipse.core.resources.IProject;
2728
import org.eclipse.core.runtime.CoreException;
2829
import org.eclipse.core.runtime.NullProgressMonitor;
@@ -93,7 +94,7 @@ public void testGetBestMatchingVM_1XversionRange() throws InvalidVersionSpecific
9394
*/
9495
@Test
9596
public void testGlobalSettings() throws Exception {
96-
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalSettingsFile, "-gs", "./resources/settings/empty_settings/settings_empty.xml");
97+
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalSettingsFile, CLIManager.ALTERNATE_GLOBAL_SETTINGS, "./resources/settings/empty_settings/settings_empty.xml");
9798
}
9899

99100
/**
@@ -103,7 +104,7 @@ public void testGlobalSettings() throws Exception {
103104
*/
104105
@Test
105106
public void testGlobalSettings_GoalOverride() throws Exception {
106-
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalSettingsFile, "-gs", "./resources/settings/empty_settings/settings_empty.xml");
107+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalSettingsFile, CLIManager.ALTERNATE_GLOBAL_SETTINGS, "./resources/settings/empty_settings/settings_empty.xml");
107108
}
108109

109110
/**
@@ -122,7 +123,7 @@ public void testGlobalSettings_Invalid() throws Exception {
122123
*/
123124
@Test
124125
public void testGlobalToolchains() throws Exception {
125-
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalToolchainsFile, "-gt", "./resources/settings/empty_settings/toolchains_empty.xml");
126+
assertMavenLaunchFileSetting(IMavenConfiguration::setGlobalToolchainsFile, CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS, "./resources/settings/empty_settings/toolchains_empty.xml");
126127
}
127128

128129
/**
@@ -132,7 +133,7 @@ public void testGlobalToolchains() throws Exception {
132133
*/
133134
@Test
134135
public void testGlobalToolchains_GoalOverride() throws Exception {
135-
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalToolchainsFile, "-gt", "./resources/settings/empty_settings/toolchains_empty.xml");
136+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setGlobalToolchainsFile, CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS, "./resources/settings/empty_settings/toolchains_empty.xml");
136137
}
137138

138139
/**
@@ -168,7 +169,7 @@ public void testRequiredJavaVersionFromEnforcerRule_NoVersionRange() throws Exce
168169
*/
169170
@Test
170171
public void testUserSettings() throws Exception {
171-
assertMavenLaunchFileSetting(IMavenConfiguration::setUserSettingsFile, "-s", "./resources/settings/empty_settings/settings_empty.xml");
172+
assertMavenLaunchFileSetting(IMavenConfiguration::setUserSettingsFile, String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), "./resources/settings/empty_settings/settings_empty.xml");
172173
}
173174

174175
/**
@@ -178,7 +179,7 @@ public void testUserSettings() throws Exception {
178179
*/
179180
@Test
180181
public void testUserSettings_GoalOverride() throws Exception {
181-
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserSettingsFile, "-s", "./resources/settings/empty_settings/settings_empty.xml");
182+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserSettingsFile, String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), "./resources/settings/empty_settings/settings_empty.xml");
182183
}
183184

184185
/**
@@ -198,7 +199,7 @@ public void testUserSettings_Invalid() throws Exception {
198199
*/
199200
@Test
200201
public void testUserToolchains() throws Exception {
201-
assertMavenLaunchFileSetting(IMavenConfiguration::setUserToolchainsFile, "-t", "./resources/settings/empty_settings/toolchains_empty.xml");
202+
assertMavenLaunchFileSetting(IMavenConfiguration::setUserToolchainsFile, String.valueOf(CLIManager.ALTERNATE_USER_TOOLCHAINS), "./resources/settings/empty_settings/toolchains_empty.xml");
202203
}
203204

204205
/**
@@ -208,7 +209,7 @@ public void testUserToolchains() throws Exception {
208209
*/
209210
@Test
210211
public void testUserToolchains_GoalOverride() throws Exception {
211-
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserToolchainsFile, "-t", "./resources/settings/empty_settings/toolchains_empty.xml");
212+
assertMavenLaunchFileSettingGoalOverride(IMavenConfiguration::setUserToolchainsFile, String.valueOf(CLIManager.ALTERNATE_USER_TOOLCHAINS), "./resources/settings/empty_settings/toolchains_empty.xml");
212213
}
213214

214215
/**
@@ -275,12 +276,13 @@ private void assertMavenLaunchConfig(CoreBiConsumer<IMavenConfiguration, String>
275276
*/
276277
private void assertMavenLaunchFileSetting(CoreBiConsumer<IMavenConfiguration, String> configSetter, String key, String relativePath)
277278
throws Exception {
279+
final String param = "-" + key;
278280
this.assertMavenLaunchConfig(configSetter, null, (launcher, config) -> {
279281
String programArguments = launcher.getProgramArguments(config);
280282

281283
// prepare assert
282284
Matcher<String> allSettings = CoreMatchers.allOf(
283-
CoreMatchers.containsString(key),
285+
CoreMatchers.containsString(param),
284286
CoreMatchers.containsString(new File(relativePath).getAbsolutePath())
285287
);
286288

@@ -299,14 +301,15 @@ private void assertMavenLaunchFileSetting(CoreBiConsumer<IMavenConfiguration, St
299301
private void assertMavenLaunchFileSettingGoalOverride(CoreBiConsumer<IMavenConfiguration, String> configSetter, String key, String relativePath)
300302
throws Exception {
301303
final String userDerivedPath = "./resources/settings/empty_settings/this_do_not_exists.xml";
302-
final String goalConfig = "clean " + key + " " + userDerivedPath;
304+
final String param = "-" + key;
305+
final String goalConfig = "clean " + param + " " + userDerivedPath;
303306

304307
this.assertMavenLaunchConfig(configSetter, goalConfig, (launcher, config) -> {
305308
String programArguments = launcher.getProgramArguments(config);
306309

307310
// prepare assert
308311
Matcher<String> allSettings = CoreMatchers.allOf(
309-
CoreMatchers.containsString(key),
312+
CoreMatchers.containsString(param),
310313
CoreMatchers.containsString(userDerivedPath),
311314
CoreMatchers.not(CoreMatchers.containsString(relativePath))
312315
);

org.eclipse.m2e.launching/src/org/eclipse/m2e/internal/launch/MavenLaunchDelegate.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@
5858
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
5959
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
6060
import org.apache.maven.artifact.versioning.VersionRange;
61+
import org.apache.maven.cli.CLIManager;
6162
import org.apache.maven.plugin.MojoExecution;
6263
import org.apache.maven.project.MavenProject;
6364

6465
import org.eclipse.m2e.actions.MavenLaunchConstants;
6566
import org.eclipse.m2e.core.MavenPlugin;
66-
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
6767
import org.eclipse.m2e.core.embedder.CoreSupplier;
68+
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
6869
import org.eclipse.m2e.core.internal.IMavenConstants;
6970
import org.eclipse.m2e.core.internal.launch.AbstractMavenRuntime;
7071
import org.eclipse.m2e.core.project.IMavenProjectFacade;
@@ -118,7 +119,7 @@ public MavenLaunchDelegate() {
118119
* Appends file based configuration key if not already set.
119120
*
120121
* @param name The name of the setting.
121-
* @param arg The argument. Must not be <code>null</code>.
122+
* @param arg The argument without dash. Must not be <code>null</code>.
122123
* @param sb The target. Must not be <code>null</code>.
123124
* @param goals The configured goals. Used to override settings.
124125
* @param substitute <code>true</code> , if variables in settings should be substituted.
@@ -128,7 +129,8 @@ public MavenLaunchDelegate() {
128129
*/
129130
private void appendFileSetting(String name, String arg, StringBuilder sb, String goals, boolean substitute,
130131
CoreSupplier<String> source) throws CoreException, IllegalArgumentException {
131-
if(!goals.contains(arg) && 0 >= sb.indexOf(arg)) {
132+
final String key = "-" + arg + " ";
133+
if(!goals.contains(key) && 0 >= sb.indexOf(key)) {
132134
String setting = source.get();
133135

134136
if(substitute && null != setting) {
@@ -139,7 +141,7 @@ private void appendFileSetting(String name, String arg, StringBuilder sb, String
139141
final File file = new File(setting.trim());
140142
if(file.exists()) {
141143
sb.append(" "); //$NON-NLS-1$
142-
sb.append(arg).append(quote(file.getAbsolutePath()));
144+
sb.append(key).append(quote(file.getAbsolutePath()));
143145
} else {
144146
throw new IllegalArgumentException("invalid path for " + name + ": " + file.getAbsolutePath());
145147
}
@@ -528,15 +530,19 @@ private void getPreferences(StringBuilder sb, ILaunchConfiguration configuration
528530
"Unexpected value for" + MavenLaunchConstants.ATTR_COLOR + ": " + colors);
529531
};
530532
sb.append(" -Dstyle.color=" + enableColor);
531-
532-
this.appendFileSetting("global settings", "-gs ", sb, goals, false, mavenConfiguration::getGlobalSettingsFile); //$NON-NLS-1$
533-
this.appendFileSetting("global settings", "-gs ", sb, goals, false, launchSupport::getSettings); //$NON-NLS-1$ //$NON-NLS-2$
534-
this.appendFileSetting("global toolchains", "-gt ", sb, goals, false, mavenConfiguration::getGlobalToolchainsFile); //$NON-NLS-1$ //$NON-NLS-2$
535-
536-
this.appendFileSetting("user settings", "-s ", sb, goals, false, //$NON-NLS-1$ //$NON-NLS-2$
533+
this.appendFileSetting("global settings", CLIManager.ALTERNATE_GLOBAL_SETTINGS, sb, goals, false, //$NON-NLS-1$
534+
mavenConfiguration::getGlobalSettingsFile);
535+
this.appendFileSetting("global settings", CLIManager.ALTERNATE_GLOBAL_SETTINGS, sb, goals, false, //$NON-NLS-1$
536+
launchSupport::getSettings); //$NON-NLS-2$
537+
this.appendFileSetting("global toolchains", CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS, sb, goals, false, //$NON-NLS-1$
538+
mavenConfiguration::getGlobalToolchainsFile); //$NON-NLS-2$
539+
540+
this.appendFileSetting("user settings", String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), sb, goals, false, //$NON-NLS-1$ //$NON-NLS-2$
537541
() -> configuration.getAttribute(MavenLaunchConstants.ATTR_USER_SETTINGS, (String) null));
538-
this.appendFileSetting("user settings", "-s ", sb, goals, false, mavenConfiguration::getUserSettingsFile); //$NON-NLS-1$ //$NON-NLS-2$
539-
this.appendFileSetting("user toolchains", "-t ", sb, goals, false, mavenConfiguration::getUserToolchainsFile); //$NON-NLS-1$ //$NON-NLS-2$
542+
this.appendFileSetting("user settings", String.valueOf(CLIManager.ALTERNATE_USER_SETTINGS), sb, goals, false, //$NON-NLS-1$
543+
mavenConfiguration::getUserSettingsFile); //$NON-NLS-2$
544+
this.appendFileSetting("user toolchains", String.valueOf(CLIManager.ALTERNATE_USER_TOOLCHAINS), sb, goals, false, //$NON-NLS-1$
545+
mavenConfiguration::getUserToolchainsFile); //$NON-NLS-2$
540546

541547
// boolean b = preferenceStore.getBoolean(MavenPreferenceConstants.P_CHECK_LATEST_PLUGIN_VERSION);
542548
// sb.append(" -D").append(MavenPreferenceConstants.P_CHECK_LATEST_PLUGIN_VERSION).append("=").append(b);

0 commit comments

Comments
 (0)