mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-11 21:50:38 +00:00
Completely remove the usage of hardcoded command line options
This commit is contained in:
@@ -29,6 +29,7 @@ import java.time.ZoneId;
|
|||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
@@ -83,17 +84,10 @@ public class DefaultClient implements Client {
|
|||||||
// System properties
|
// System properties
|
||||||
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
||||||
String arg = it.next();
|
String arg = it.next();
|
||||||
String val;
|
String val = Environment.MAVEN_DEFINE.removeCommandLineOption(new ArrayList<>(Collections.singletonList(arg)));
|
||||||
if (arg.startsWith("-D")) {
|
if (val != null) {
|
||||||
val = arg.substring(2);
|
if (val.isEmpty()) {
|
||||||
} else if (arg.equals("--define")) {
|
throw new IllegalArgumentException("Missing argument for option " + arg);
|
||||||
if (it.hasNext()) {
|
|
||||||
val = it.next();
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Missing argument for option --define");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
|
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
|
||||||
final int eqPos = val.indexOf('=');
|
final int eqPos = val.indexOf('=');
|
||||||
@@ -103,6 +97,7 @@ public class DefaultClient implements Client {
|
|||||||
System.setProperty(val.substring(2), "");
|
System.setProperty(val.substring(2), "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DaemonParameters parameters = new DaemonParameters();
|
DaemonParameters parameters = new DaemonParameters();
|
||||||
if (parameters.serial()) {
|
if (parameters.serial()) {
|
||||||
@@ -136,31 +131,10 @@ public class DefaultClient implements Client {
|
|||||||
LOGGER.debug("Starting client");
|
LOGGER.debug("Starting client");
|
||||||
|
|
||||||
final List<String> args = new ArrayList<>(argv);
|
final List<String> args = new ArrayList<>(argv);
|
||||||
boolean version = false;
|
boolean version = Environment.MAVEN_VERSION.hasCommandLineOption(args);
|
||||||
boolean showVersion = false;
|
boolean showVersion = Environment.MAVEN_SHOW_VERSION.hasCommandLineOption(args);
|
||||||
boolean debug = false;
|
boolean debug = Environment.MAVEN_DEBUG.hasCommandLineOption(args);
|
||||||
boolean batchMode = false;
|
boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandLineOption(args);
|
||||||
for (String arg : args) {
|
|
||||||
switch (arg) {
|
|
||||||
case "-v":
|
|
||||||
case "-version":
|
|
||||||
case "--version":
|
|
||||||
version = true;
|
|
||||||
break;
|
|
||||||
case "-V":
|
|
||||||
case "--show-version":
|
|
||||||
showVersion = true;
|
|
||||||
break;
|
|
||||||
case "-X":
|
|
||||||
case "--debug":
|
|
||||||
debug = true;
|
|
||||||
break;
|
|
||||||
case "-B":
|
|
||||||
case "--batch-mode":
|
|
||||||
batchMode = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print version if needed
|
// Print version if needed
|
||||||
if (version || showVersion || debug) {
|
if (version || showVersion || debug) {
|
||||||
|
@@ -16,20 +16,46 @@
|
|||||||
package org.mvndaemon.mvnd.client;
|
package org.mvndaemon.mvnd.client;
|
||||||
|
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mvndaemon.mvnd.common.Environment;
|
import org.mvndaemon.mvnd.common.Environment;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class EnvironmentTest {
|
public class EnvironmentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void arguments() {
|
||||||
|
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo=bar")));
|
||||||
|
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D", "foo=bar")));
|
||||||
|
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define", "foo=bar")));
|
||||||
|
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define=foo=bar")));
|
||||||
|
|
||||||
|
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D=foo")));
|
||||||
|
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo")));
|
||||||
|
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D", "foo")));
|
||||||
|
|
||||||
|
assertEquals("foo=", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo=")));
|
||||||
|
|
||||||
|
assertEquals("", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D")));
|
||||||
|
assertEquals("", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> list(String... items) {
|
||||||
|
return new ArrayList<>(Arrays.asList(items));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void prop() {
|
void prop() {
|
||||||
try (EnvironmentResource env = new EnvironmentResource()) {
|
try (EnvironmentResource env = new EnvironmentResource()) {
|
||||||
env.props("mvnd.home", "/maven/home/prop");
|
env.props("mvnd.home", "/maven/home/prop");
|
||||||
Assertions.assertEquals("/maven/home/prop", DaemonParameters.systemProperty(Environment.MVND_HOME).asString());
|
assertEquals("/maven/home/prop", DaemonParameters.systemProperty(Environment.MVND_HOME).asString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +63,7 @@ public class EnvironmentTest {
|
|||||||
void env() {
|
void env() {
|
||||||
try (EnvironmentResource env = new EnvironmentResource()) {
|
try (EnvironmentResource env = new EnvironmentResource()) {
|
||||||
env.env("MVND_HOME", "/maven/home/env");
|
env.env("MVND_HOME", "/maven/home/env");
|
||||||
Assertions.assertEquals("/maven/home/env", DaemonParameters.environmentVariable(Environment.MVND_HOME).asString());
|
assertEquals("/maven/home/env", DaemonParameters.environmentVariable(Environment.MVND_HOME).asString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +72,7 @@ public class EnvironmentTest {
|
|||||||
try (EnvironmentResource env = new EnvironmentResource()) {
|
try (EnvironmentResource env = new EnvironmentResource()) {
|
||||||
final Properties localProps = new Properties();
|
final Properties localProps = new Properties();
|
||||||
localProps.put("mvnd.home", "/maven/home/local");
|
localProps.put("mvnd.home", "/maven/home/local");
|
||||||
Assertions.assertEquals(Paths.get("/maven/home/local"),
|
assertEquals(Paths.get("/maven/home/local"),
|
||||||
DaemonParameters
|
DaemonParameters
|
||||||
.environmentVariable(Environment.MVND_HOME)
|
.environmentVariable(Environment.MVND_HOME)
|
||||||
.orSystemProperty()
|
.orSystemProperty()
|
||||||
@@ -61,7 +87,7 @@ public class EnvironmentTest {
|
|||||||
try (EnvironmentResource env = new EnvironmentResource()) {
|
try (EnvironmentResource env = new EnvironmentResource()) {
|
||||||
env.props("mvnd.home", "/maven/home/prop");
|
env.props("mvnd.home", "/maven/home/prop");
|
||||||
env.env("MVND_HOME", "/maven/home/env");
|
env.env("MVND_HOME", "/maven/home/env");
|
||||||
Assertions.assertEquals("/maven/home/env",
|
assertEquals("/maven/home/env",
|
||||||
DaemonParameters
|
DaemonParameters
|
||||||
.environmentVariable(Environment.MVND_HOME)
|
.environmentVariable(Environment.MVND_HOME)
|
||||||
.orSystemProperty()
|
.orSystemProperty()
|
||||||
@@ -73,7 +99,7 @@ public class EnvironmentTest {
|
|||||||
void fail() {
|
void fail() {
|
||||||
try (EnvironmentResource env = new EnvironmentResource()) {
|
try (EnvironmentResource env = new EnvironmentResource()) {
|
||||||
try {
|
try {
|
||||||
Assertions.assertEquals("/maven/home/env",
|
assertEquals("/maven/home/env",
|
||||||
DaemonParameters
|
DaemonParameters
|
||||||
.environmentVariable(Environment.MVND_HOME)
|
.environmentVariable(Environment.MVND_HOME)
|
||||||
.orSystemProperty()
|
.orSystemProperty()
|
||||||
@@ -81,7 +107,7 @@ public class EnvironmentTest {
|
|||||||
.asString());
|
.asString());
|
||||||
Assertions.fail("IllegalStateException expected");
|
Assertions.fail("IllegalStateException expected");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
Assertions.assertEquals(
|
assertEquals(
|
||||||
"Could not get value for Environment.MVND_HOME from any of the following sources: environment variable MVND_HOME, system property mvnd.home",
|
"Could not get value for Environment.MVND_HOME from any of the following sources: environment variable MVND_HOME, system property mvnd.home",
|
||||||
e.getMessage());
|
e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -90,7 +116,7 @@ public class EnvironmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cygwin() {
|
void cygwin() {
|
||||||
Assertions.assertEquals("C:\\jdk-11.0.2\\", Environment.cygpath("/cygdrive/c/jdk-11.0.2/"));
|
assertEquals("C:\\jdk-11.0.2\\", Environment.cygpath("/cygdrive/c/jdk-11.0.2/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static class EnvironmentResource implements AutoCloseable {
|
static class EnvironmentResource implements AutoCloseable {
|
||||||
|
@@ -92,6 +92,14 @@ public enum Environment {
|
|||||||
MAVEN_LOG_FILE(null, null, null, OptionType.PATH, Flags.INTERNAL, "-l", "--log-file"),
|
MAVEN_LOG_FILE(null, null, null, OptionType.PATH, Flags.INTERNAL, "-l", "--log-file"),
|
||||||
/** Batch mode */
|
/** Batch mode */
|
||||||
MAVEN_BATCH_MODE(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-B", "--batch-mode"),
|
MAVEN_BATCH_MODE(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-B", "--batch-mode"),
|
||||||
|
/** Debug */
|
||||||
|
MAVEN_DEBUG(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-X", "--debug"),
|
||||||
|
/** Version */
|
||||||
|
MAVEN_VERSION(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-v", "-version", "--version"),
|
||||||
|
/** Show version */
|
||||||
|
MAVEN_SHOW_VERSION(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-V", "--show-version"),
|
||||||
|
/** Define */
|
||||||
|
MAVEN_DEFINE(null, null, null, OptionType.STRING, Flags.INTERNAL, "-D", "--define"),
|
||||||
|
|
||||||
//
|
//
|
||||||
// mvnd properties
|
// mvnd properties
|
||||||
@@ -342,21 +350,22 @@ public enum Environment {
|
|||||||
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
||||||
String arg = it.next();
|
String arg = it.next();
|
||||||
if (Stream.of(prefixes).anyMatch(arg::startsWith)) {
|
if (Stream.of(prefixes).anyMatch(arg::startsWith)) {
|
||||||
|
it.remove();
|
||||||
if (type == OptionType.VOID) {
|
if (type == OptionType.VOID) {
|
||||||
value = "";
|
value = "";
|
||||||
it.remove();
|
|
||||||
} else {
|
} else {
|
||||||
int idx = arg.indexOf('=');
|
String opt = Stream.of(prefixes).filter(arg::startsWith)
|
||||||
if (idx >= 0) {
|
.max(Comparator.comparing(String::length)).get();
|
||||||
value = arg.substring(idx + 1);
|
value = arg.substring(opt.length());
|
||||||
it.remove();
|
if (value.isEmpty()) {
|
||||||
} else {
|
|
||||||
value = "";
|
|
||||||
it.remove();
|
|
||||||
if (it.hasNext()) {
|
if (it.hasNext()) {
|
||||||
value = it.next();
|
value = it.next();
|
||||||
it.remove();
|
it.remove();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (value.charAt(0) == '=') {
|
||||||
|
value = value.substring(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user