Missing argument for option -D, fixes #662 (#679)

Co-authored-by: 核桃 <hetao@2dfire.com>
This commit is contained in:
Guillaume Nodet
2022-08-30 09:14:12 +02:00
committed by GitHub
parent 5e59c40453
commit 449e815973
2 changed files with 28 additions and 2 deletions

View File

@@ -30,11 +30,14 @@ 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.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.internal.CLibrary; import org.fusesource.jansi.internal.CLibrary;
import org.jline.utils.AttributedString; import org.jline.utils.AttributedString;
@@ -139,9 +142,23 @@ public class DefaultClient implements Client {
} }
public static void setSystemPropertiesFromCommandLine(List<String> args) { public static void setSystemPropertiesFromCommandLine(List<String> args) {
for (String arg : args) { final Iterator<String> iterator = args.iterator();
boolean defineIsEmpty = false;
while (iterator.hasNext()) {
final String arg = iterator.next();
String val = Environment.MAVEN_DEFINE.removeCommandLineOption(new ArrayList<>(Collections.singletonList(arg))); String val = Environment.MAVEN_DEFINE.removeCommandLineOption(new ArrayList<>(Collections.singletonList(arg)));
/* not -D or --define and pre define is empty */
if (val == null && defineIsEmpty) {
defineIsEmpty = false;
/* not all of Environment, use arg as pre define value */
val = maybeDefineCommandLineOption(arg) ? arg : "";
}
if (val != null) { if (val != null) {
/* empty -D or --define, next arg is value */
if (val.isEmpty() && iterator.hasNext()) {
defineIsEmpty = true;
continue;
}
if (val.isEmpty()) { if (val.isEmpty()) {
throw new IllegalArgumentException("Missing argument for option " + arg); throw new IllegalArgumentException("Missing argument for option " + arg);
} }
@@ -156,6 +173,14 @@ public class DefaultClient implements Client {
} }
} }
private static boolean maybeDefineCommandLineOption(String arg) {
// if arg maybe MAVEN_DEFINE value
return EnumSet.allOf(Environment.class)
.stream()
.filter(e -> e != Environment.MAVEN_DEFINE)
.noneMatch(e -> e.hasCommandLineOption(Collections.singletonList(arg)));
}
public DefaultClient(DaemonParameters parameters) { public DefaultClient(DaemonParameters parameters) {
// Those options are needed in order to be able to set the environment correctly // Those options are needed in order to be able to set the environment correctly
this.parameters = parameters.withJdkJavaOpts( this.parameters = parameters.withJdkJavaOpts(

View File

@@ -37,8 +37,9 @@ public class MavenConfNativeIT {
@Test @Test
void version() throws IOException, InterruptedException { void version() throws IOException, InterruptedException {
final TestClientOutput o = new TestClientOutput(); final TestClientOutput o = new TestClientOutput();
// this test also exercise the "-D foo=bar" syntax for defining properties
client.execute(o, "org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate", client.execute(o, "org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate",
"-Dexpression=maven.conf", "-q", "-DforceStdout", "--raw-streams").assertSuccess(); "-D", "expression=maven.conf", "-q", "-DforceStdout", "--raw-streams").assertSuccess();
String conf = parameters.mvndHome().resolve("mvn/conf").toString(); String conf = parameters.mvndHome().resolve("mvn/conf").toString();
assertTrue(o.getMessages().stream() assertTrue(o.getMessages().stream()
.anyMatch(m -> m.toString().contains(conf)), "Output should contain " + conf); .anyMatch(m -> m.toString().contains(conf)), "Output should contain " + conf);