mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-11 13:39:32 +00:00
Use Environment to deal with 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.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import org.fusesource.jansi.Ansi;
|
import org.fusesource.jansi.Ansi;
|
||||||
@@ -58,41 +59,50 @@ public class DefaultClient implements Client {
|
|||||||
private final DaemonParameters parameters;
|
private final DaemonParameters parameters;
|
||||||
|
|
||||||
public static void main(String[] argv) throws Exception {
|
public static void main(String[] argv) throws Exception {
|
||||||
final List<String> args = new ArrayList<>(argv.length);
|
final List<String> args = new ArrayList<>(Arrays.asList(argv));
|
||||||
|
|
||||||
|
// Log file
|
||||||
Path logFile = null;
|
Path logFile = null;
|
||||||
int i = 0;
|
String sLogFile = Environment.MAVEN_LOG_FILE.removeCommandLineOption(args);
|
||||||
boolean batchMode = false;
|
if (sLogFile != null) {
|
||||||
while (i < argv.length) {
|
if (sLogFile.isEmpty()) {
|
||||||
final String arg = argv[i++];
|
throw new IllegalArgumentException("-l and --log-file need to be followed by a path");
|
||||||
if ("-l".equals(arg) || "--log-file".equals(arg)) {
|
|
||||||
if (i < argv.length) {
|
|
||||||
logFile = Paths.get(argv[i++]);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("-l and --log-file need to be followed by a path");
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (arg.startsWith("-D")) {
|
|
||||||
/* This needs to be done very early, otherwise various DeamonParameters do not work properly */
|
|
||||||
final int eqPos = arg.indexOf('=');
|
|
||||||
if (eqPos >= 0) {
|
|
||||||
System.setProperty(arg.substring(2, eqPos), arg.substring(eqPos + 1));
|
|
||||||
} else {
|
|
||||||
System.setProperty(arg.substring(2), "");
|
|
||||||
}
|
|
||||||
args.add(arg);
|
|
||||||
} else {
|
} else {
|
||||||
if (!batchMode && ("-B".equals(arg) || "--batch-mode".equals(arg))) {
|
logFile = Paths.get(sLogFile);
|
||||||
batchMode = true;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch mode
|
||||||
|
boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandOption(args);
|
||||||
|
|
||||||
|
// System properties
|
||||||
|
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
||||||
|
String arg = it.next();
|
||||||
|
String val;
|
||||||
|
if (arg.startsWith("-D")) {
|
||||||
|
val = arg.substring(2);
|
||||||
|
} else if (arg.equals("--define")) {
|
||||||
|
if (it.hasNext()) {
|
||||||
|
val = it.next();
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Missing argument for option --define");
|
||||||
}
|
}
|
||||||
args.add(arg);
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
|
||||||
|
final int eqPos = val.indexOf('=');
|
||||||
|
if (eqPos >= 0) {
|
||||||
|
System.setProperty(val.substring(2, eqPos), val.substring(eqPos + 1));
|
||||||
|
} else {
|
||||||
|
System.setProperty(val.substring(2), "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DaemonParameters parameters = new DaemonParameters();
|
DaemonParameters parameters = new DaemonParameters();
|
||||||
int exitCode = 0;
|
int exitCode = 0;
|
||||||
try (TerminalOutput output = new TerminalOutput(batchMode || parameters.noBuffering(), parameters.rollingWindowSize(),
|
boolean noBuffering = batchMode || parameters.noBuffering();
|
||||||
logFile)) {
|
try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile)) {
|
||||||
try {
|
try {
|
||||||
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
|
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
|
||||||
exitCode = result.getExitCode();
|
exitCode = result.getExitCode();
|
||||||
@@ -180,8 +190,7 @@ public class DefaultClient implements Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try (DaemonRegistry registry = new DaemonRegistry(parameters.registry())) {
|
try (DaemonRegistry registry = new DaemonRegistry(parameters.registry())) {
|
||||||
boolean status = args.remove("--status");
|
if (Environment.STATUS.removeCommandLineOption(args) != null) {
|
||||||
if (status) {
|
|
||||||
final String template = " %36s %7s %5s %7s %5s %23s %s";
|
final String template = " %36s %7s %5s %7s %5s %23s %s";
|
||||||
output.accept(Message.log(String.format(template,
|
output.accept(Message.log(String.format(template,
|
||||||
"UUID", "PID", "Port", "Status", "RSS", "Last activity", "Java home")));
|
"UUID", "PID", "Port", "Status", "RSS", "Last activity", "Java home")));
|
||||||
@@ -201,8 +210,7 @@ public class DefaultClient implements Client {
|
|||||||
}
|
}
|
||||||
return DefaultResult.success(argv);
|
return DefaultResult.success(argv);
|
||||||
}
|
}
|
||||||
boolean stop = args.remove("--stop");
|
if (Environment.STOP.removeCommandLineOption(args) != null) {
|
||||||
if (stop) {
|
|
||||||
DaemonInfo[] dis = registry.getAll().toArray(new DaemonInfo[0]);
|
DaemonInfo[] dis = registry.getAll().toArray(new DaemonInfo[0]);
|
||||||
if (dis.length > 0) {
|
if (dis.length > 0) {
|
||||||
output.accept(Message.display("Stopping " + dis.length + " running daemons"));
|
output.accept(Message.display("Stopping " + dis.length + " running daemons"));
|
||||||
@@ -218,29 +226,25 @@ public class DefaultClient implements Client {
|
|||||||
}
|
}
|
||||||
return DefaultResult.success(argv);
|
return DefaultResult.success(argv);
|
||||||
}
|
}
|
||||||
boolean purge = args.remove("--purge");
|
if (Environment.PURGE.removeCommandLineOption(args) != null) {
|
||||||
if (purge) {
|
|
||||||
String result = purgeLogs();
|
String result = purgeLogs();
|
||||||
output.accept(Message.display(result != null ? result : "Nothing to purge"));
|
output.accept(Message.display(result != null ? result : "Nothing to purge"));
|
||||||
return DefaultResult.success(argv);
|
return DefaultResult.success(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.stream().noneMatch(arg -> arg.startsWith("-T") || arg.equals("--threads"))) {
|
if (!Environment.MVND_THREADS.hasCommandOption(args)) {
|
||||||
args.add("--threads");
|
Environment.MVND_THREADS.appendAsCommandLineOption(args, parameters.threads());
|
||||||
args.add(parameters.threads());
|
|
||||||
}
|
}
|
||||||
if (args.stream().noneMatch(arg -> arg.startsWith("-b") || arg.equals("--builder"))) {
|
if (!Environment.MVND_BUILDER.hasCommandOption(args)) {
|
||||||
args.add("--builder");
|
Environment.MVND_BUILDER.appendAsCommandLineOption(args, parameters.builder());
|
||||||
args.add(parameters.builder());
|
|
||||||
}
|
}
|
||||||
final Path settings = parameters.settings();
|
final Path settings = parameters.settings();
|
||||||
if (settings != null && args.stream().noneMatch(arg -> arg.equals("-s") || arg.equals("--settings"))) {
|
if (settings != null && !Environment.MAVEN_SETTINGS.hasCommandOption(args)) {
|
||||||
args.add("--settings");
|
Environment.MAVEN_SETTINGS.appendAsCommandLineOption(args, settings.toString());
|
||||||
args.add(settings.toString());
|
|
||||||
}
|
}
|
||||||
final Path localMavenRepository = parameters.mavenRepoLocal();
|
final Path localMavenRepository = parameters.mavenRepoLocal();
|
||||||
if (localMavenRepository != null && args.stream().noneMatch(arg -> arg.startsWith("-Dmaven.repo.local="))) {
|
if (localMavenRepository != null && !Environment.MAVEN_REPO_LOCAL.hasCommandOption(args)) {
|
||||||
args.add("-Dmaven.repo.local=" + localMavenRepository.toString());
|
Environment.MAVEN_REPO_LOCAL.appendAsCommandLineOption(args, localMavenRepository.toString());
|
||||||
}
|
}
|
||||||
Environment.MVND_TERMINAL_WIDTH.appendAsCommandLineOption(args, Integer.toString(output.getTerminalWidth()));
|
Environment.MVND_TERMINAL_WIDTH.appendAsCommandLineOption(args, Integer.toString(output.getTerminalWidth()));
|
||||||
|
|
||||||
|
@@ -24,6 +24,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -83,6 +84,10 @@ public enum Environment {
|
|||||||
MAVEN_SETTINGS("maven.settings", null, null, OptionType.PATH, Flags.NONE, "-s", "--settings"),
|
MAVEN_SETTINGS("maven.settings", null, null, OptionType.PATH, Flags.NONE, "-s", "--settings"),
|
||||||
/** The root directory of the current multi module Maven project */
|
/** The root directory of the current multi module Maven project */
|
||||||
MAVEN_MULTIMODULE_PROJECT_DIRECTORY("maven.multiModuleProjectDirectory", null, null, OptionType.PATH, Flags.NONE),
|
MAVEN_MULTIMODULE_PROJECT_DIRECTORY("maven.multiModuleProjectDirectory", null, null, OptionType.PATH, Flags.NONE),
|
||||||
|
/** Log file */
|
||||||
|
MAVEN_LOG_FILE(null, null, null, OptionType.PATH, Flags.INTERNAL, "-l", "--log-file"),
|
||||||
|
/** Batch mode */
|
||||||
|
MAVEN_BATCH_MODE(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-B", "--batch-mode"),
|
||||||
|
|
||||||
//
|
//
|
||||||
// mvnd properties
|
// mvnd properties
|
||||||
@@ -323,6 +328,11 @@ public enum Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasCommandOption(Collection<String> args) {
|
public boolean hasCommandOption(Collection<String> args) {
|
||||||
|
final String[] prefixes = getPrefixes();
|
||||||
|
return args.stream().anyMatch(arg -> Stream.of(prefixes).anyMatch(arg::startsWith));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] getPrefixes() {
|
||||||
final String[] prefixes;
|
final String[] prefixes;
|
||||||
if (options.isEmpty()) {
|
if (options.isEmpty()) {
|
||||||
prefixes = new String[] { "-D" + property + "=" };
|
prefixes = new String[] { "-D" + property + "=" };
|
||||||
@@ -333,7 +343,35 @@ public enum Environment {
|
|||||||
} else {
|
} else {
|
||||||
prefixes = options.toArray(new String[0]);
|
prefixes = options.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
return args.stream().anyMatch(arg -> Stream.of(prefixes).anyMatch(arg::startsWith));
|
return prefixes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String removeCommandLineOption(List<String> args) {
|
||||||
|
final String[] prefixes = getPrefixes();
|
||||||
|
String value = null;
|
||||||
|
for (Iterator<String> it = args.iterator(); it.hasNext();) {
|
||||||
|
String arg = it.next();
|
||||||
|
if (Stream.of(prefixes).anyMatch(arg::startsWith)) {
|
||||||
|
if (type == OptionType.VOID) {
|
||||||
|
value = "";
|
||||||
|
it.remove();
|
||||||
|
} else {
|
||||||
|
int idx = arg.indexOf('=');
|
||||||
|
if (idx >= 0) {
|
||||||
|
value = arg.substring(idx + 1);
|
||||||
|
it.remove();
|
||||||
|
} else {
|
||||||
|
value = "";
|
||||||
|
it.remove();
|
||||||
|
if (it.hasNext()) {
|
||||||
|
value = it.next();
|
||||||
|
it.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String cygpath(String result) {
|
public static String cygpath(String result) {
|
||||||
|
Reference in New Issue
Block a user