Skip to content

Commit

Permalink
Missing argument for option -D, fixes apache#662
Browse files Browse the repository at this point in the history
  • Loading branch information
核桃 authored and gnodet committed Aug 30, 2022
1 parent 5e59c40 commit de518a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.internal.CLibrary;
import org.jline.utils.AttributedString;
Expand Down Expand Up @@ -139,9 +142,23 @@ public static void main(String[] argv) throws Exception {
}

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)));
/* 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) {
/* empty -D or --define, next arg is value */
if (val.isEmpty() && iterator.hasNext()) {
defineIsEmpty = true;
continue;
}
if (val.isEmpty()) {
throw new IllegalArgumentException("Missing argument for option " + arg);
}
Expand All @@ -156,6 +173,14 @@ public static void setSystemPropertiesFromCommandLine(List<String> args) {
}
}

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) {
// Those options are needed in order to be able to set the environment correctly
this.parameters = parameters.withJdkJavaOpts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public class MavenConfNativeIT {
@Test
void version() throws IOException, InterruptedException {
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",
"-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();
assertTrue(o.getMessages().stream()
.anyMatch(m -> m.toString().contains(conf)), "Output should contain " + conf);
Expand Down

0 comments on commit de518a0

Please sign in to comment.