From f40c1e811f6b4525eb35f3923cba3a625e7481f4 Mon Sep 17 00:00:00 2001 From: Ivan Jensen Date: Fri, 28 Jan 2022 21:53:14 -0800 Subject: [PATCH 1/4] Initial attempt to make -rf work with RUN 0 Main reactor : mymvn -f pom.xml --projects com.elasticpath.tools:mvnmin Failed to execute 'mymvn', either it couldn't be found, or it isn't executable. --- .../tools/mavenminimal/MvnMinCli.java | 81 +++++++++++++------ .../mavenminimal/reactor/MavenDriver.java | 15 +++- 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java index a5333d0..7e25cae 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java @@ -57,6 +57,7 @@ public class MvnMinCli { private static boolean printMode; private static boolean versionMode; private static boolean buildIfEnabled = true; + private static String resumeFromModule; /** * The default command line entry point for mvnmin. @@ -148,32 +149,41 @@ private static ModuleRequests findChangedProjectsIds(final ProjectRepository rep * @param out the PrintStream to report any issues to. */ private static void parseArgs(final String[] args, final PrintStream out) { + boolean parsingResumeFrom = false; for (String arg : args) { - if (arg.matches("--all")) { - allPomMode = true; - } else if (arg.equals("-d") || arg.equals("--dry-run")) { - dryRunMode = true; - } else if (arg.matches("--diff.*")) { - int equalsIndex = arg.indexOf('='); - if (equalsIndex > -1 && equalsIndex + 1 < arg.length()) { - commitDiffArg = arg.substring(equalsIndex + 1); - } - if (!commitDiffArg.contains("..")) { - commitDiffArg += ".."; + if (parsingResumeFrom) { + parsingResumeFrom = false; + resumeFromModule = arg; + } else { + if (arg.matches("--all")) { + allPomMode = true; + } else if (arg.equals("-d") || arg.equals("--dry-run")) { + dryRunMode = true; + } else if (arg.matches("--diff.*")) { + int equalsIndex = arg.indexOf('='); + if (equalsIndex > -1 && equalsIndex + 1 < arg.length()) { + commitDiffArg = arg.substring(equalsIndex + 1); + } + if (!commitDiffArg.contains("..")) { + commitDiffArg += ".."; + } + diffCommitMode = true; + } else if (arg.matches("--help")) { + printUsage(out); + exit(0); + } else if (arg.equals("-p")) { + printMode = true; + } else if (arg.equals("--nbi")) { + buildIfEnabled = false; + } else if (arg.equals("--version")) { + versionMode = true; + } else if (arg.equals("-f") || arg.equals("--file")) { + out.println("The options '-f' and '--file' are not supported by mvnmin, exiting."); + exit(1); + } else if (arg.equals("-rf") || arg.equals("--resume-from")) { + parsingResumeFrom = true; + // the next param is out project } - diffCommitMode = true; - } else if (arg.matches("--help")) { - printUsage(out); - exit(0); - } else if (arg.equals("-p")) { - printMode = true; - } else if (arg.equals("--nbi")) { - buildIfEnabled = false; - } else if (arg.equals("--version")) { - versionMode = true; - } else if (arg.equals("-f") || arg.equals("--file")) { - out.println("The options '-f' and '--file' are not supported by mvnmin, exiting."); - exit(1); } } } @@ -195,6 +205,10 @@ private static void printUsage(final PrintStream out) { out.println(" --nbi No build-if dependencies are considered, just "); out.println(" changed modules."); out.println(); + out.println(" Intercepted Maven Options;"); + out.println(" -rf,--resume-from Resume reactor from specified project (and sub-reactor)."); + out.println(" -f,--file Not supported, mvnmin will exit."); + out.println(); out.println(" Scripting"); out.println(" -p Don't invoke maven, print out activated projects,"); out.println(" sorted, newline separated."); @@ -280,12 +294,27 @@ private static int executeMavenOnReactors(final ExtendedReactor xreactor, final ReactorPrinter printer = new ReactorPrinter(maxReactorNameLength.getAsInt(), out); printer.newline(); // add some spacing + // Skip the reactors before the one containing the resume-from module, let the rest run. + boolean resumeFromActivated = false; + for (Reactor subReactor : xreactor.getSubReactors()) { + if (!resumeFromActivated) { + for (String activeModule : subReactor.getActiveModules()) { // can't simply do `contains()` + Logger.debug(activeModule); + if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name + resumeFromActivated = true; + } else { + Logger.debug("Skipping " + subReactor.getReactorName() + " looking for resume-from module: " + resumeFromModule); + subReactor.setSkipReactor(true); + } + } + } + } int mavenExitValue = 0; for (Reactor subReactor : xreactor.getSubReactors()) { if (mavenExitValue == 0) { mavenExitValue = - MavenDriver.runMvnForReactor(subReactor, filteredArgs, mvnMinConfig.getMvnCommand(), dryRun, printer); + MavenDriver.runMvnForReactor(subReactor, filteredArgs, mvnMinConfig.getMvnCommand(), dryRun, printer, resumeFromModule); printer.newline(); } else { out.println("mvnmin: Maven failed to run successfully."); @@ -299,6 +328,8 @@ private static List removeNonMavenArgs(final String[] args) { List mavenArguments = new ArrayList<>(Arrays.asList(args)); removeArgPair(mavenArguments, "-pl"); removeArgPair(mavenArguments, "--projects"); + removeArgPair(mavenArguments, "-rf"); + removeArgPair(mavenArguments, "--resume-from"); mavenArguments.removeIf(s -> s.equals("--all")); mavenArguments.removeIf(s -> s.equals("--d")); diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java index a398ea0..e92b16e 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java @@ -64,8 +64,8 @@ public class MavenDriver { */ public static int runMvnForReactor( final Reactor reactor, final List args, final String overrideMvnCommand, - final boolean dryRun, final ReactorPrinter printer) { - CommandLine command = determineMavenCommand(reactor, args, overrideMvnCommand); + final boolean dryRun, final ReactorPrinter printer, final String resumeFromModule) { + CommandLine command = determineMavenCommand(reactor, args, overrideMvnCommand, resumeFromModule); printer.commandSummary(reactor, command); if (dryRun || !reactor.shouldBuild()) { @@ -98,7 +98,7 @@ private static int executeMaven(final CommandLine command, final ReactorPrinter * @param overrideMvnCommand the mvn command to used (can be null) * @return the Maven command to execute */ - private static CommandLine determineMavenCommand(final Reactor reactor, final List inputArgs, final String overrideMvnCommand) { + private static CommandLine determineMavenCommand(final Reactor reactor, final List inputArgs, final String overrideMvnCommand, final String resumeFromModule) { AtomicReference goal = new AtomicReference<>(""); List mavenMinimalArguments = new ArrayList<>(inputArgs); @@ -135,6 +135,15 @@ private static CommandLine determineMavenCommand(final Reactor reactor, final Li mavenArguments.add(String.join(",", reactor.getActiveModules())); } + // Skip the reactors before the one containing the resume-from module, let the rest run. + for (String activeModule : reactor.getActiveModules()) { // can't simply do `contains()` + if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name + mavenArguments.add("-rf"); + mavenArguments.add(resumeFromModule); + break; + } + } + String mvnCommand = determineMvnExecutable(overrideMvnCommand); CommandLine cmdLine = new CommandLine(mvnCommand); From ee0fc5dc6ff88e45fb8be981a072fb18f597eacc Mon Sep 17 00:00:00 2001 From: Ivan Jensen Date: Fri, 28 Jan 2022 22:11:57 -0800 Subject: [PATCH 2/4] Update README.md and fix checkstyle issues --- README.md | 4 ++++ .../java/com/elasticpath/tools/mavenminimal/MvnMinCli.java | 7 +++++-- .../tools/mavenminimal/reactor/MavenDriver.java | 5 ++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d57f4c4..2d311ee 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ usage: mvmin [options] [] [] [] --nbi No build-if dependencies are considered, just changed modules. + Intercepted Maven Options; + -rf,--resume-from Resume reactor from specified project (and sub-reactor). + -f,--file Not supported, mvnmin will exit. + Scripting -p Don't invoke maven, print out activated projects, sorted, newline separated. diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java index 7e25cae..7895588 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java @@ -303,7 +303,8 @@ private static int executeMavenOnReactors(final ExtendedReactor xreactor, final if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name resumeFromActivated = true; } else { - Logger.debug("Skipping " + subReactor.getReactorName() + " looking for resume-from module: " + resumeFromModule); + Logger.debug("Skipping " + subReactor.getReactorName() + + " looking for resume-from module: " + resumeFromModule); subReactor.setSkipReactor(true); } } @@ -314,7 +315,9 @@ private static int executeMavenOnReactors(final ExtendedReactor xreactor, final for (Reactor subReactor : xreactor.getSubReactors()) { if (mavenExitValue == 0) { mavenExitValue = - MavenDriver.runMvnForReactor(subReactor, filteredArgs, mvnMinConfig.getMvnCommand(), dryRun, printer, resumeFromModule); + MavenDriver.runMvnForReactor( + subReactor, filteredArgs, mvnMinConfig.getMvnCommand(), + dryRun, printer, resumeFromModule); printer.newline(); } else { out.println("mvnmin: Maven failed to run successfully."); diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java index e92b16e..b004d20 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java @@ -60,6 +60,7 @@ public class MavenDriver { * @param overrideMvnCommand an override to use for a maven command * @param dryRun false to invoke maven, true to skip * @param printer the output printer + * @param resumeFromModule the module to resume from for this reactor (null if not applicable) * @return the exit status of the maven invocation (or zero if dryRun is true) */ public static int runMvnForReactor( @@ -96,9 +97,11 @@ private static int executeMaven(final CommandLine command, final ReactorPrinter * @param reactor the list of project IDs to be passed to maven to build * @param inputArgs the command line arguments that were passed to the tool * @param overrideMvnCommand the mvn command to used (can be null) + * @param resumeFromModule the module to rsult from, or null if not applicable. * @return the Maven command to execute */ - private static CommandLine determineMavenCommand(final Reactor reactor, final List inputArgs, final String overrideMvnCommand, final String resumeFromModule) { + private static CommandLine determineMavenCommand( + final Reactor reactor, final List inputArgs, final String overrideMvnCommand, final String resumeFromModule) { AtomicReference goal = new AtomicReference<>(""); List mavenMinimalArguments = new ArrayList<>(inputArgs); From 86689e2ff21938e60e3ae394c09df2e48b83290b Mon Sep 17 00:00:00 2001 From: Ivan Jensen Date: Mon, 14 Feb 2022 11:39:19 -0800 Subject: [PATCH 3/4] Avoid NPE's. Project list at the end of the cmd --- .../tools/mavenminimal/MvnMinCli.java | 24 ++++++++++--------- .../mavenminimal/reactor/MavenDriver.java | 20 +++++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java index 7895588..37edad8 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java @@ -295,17 +295,19 @@ private static int executeMavenOnReactors(final ExtendedReactor xreactor, final printer.newline(); // add some spacing // Skip the reactors before the one containing the resume-from module, let the rest run. - boolean resumeFromActivated = false; - for (Reactor subReactor : xreactor.getSubReactors()) { - if (!resumeFromActivated) { - for (String activeModule : subReactor.getActiveModules()) { // can't simply do `contains()` - Logger.debug(activeModule); - if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name - resumeFromActivated = true; - } else { - Logger.debug("Skipping " + subReactor.getReactorName() - + " looking for resume-from module: " + resumeFromModule); - subReactor.setSkipReactor(true); + if (resumeFromModule != null) { + boolean resumeFromActivated = false; + for (Reactor subReactor : xreactor.getSubReactors()) { + if (!resumeFromActivated) { + for (String activeModule : subReactor.getActiveModules()) { // can't simply do `contains()` + Logger.debug(activeModule); + if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name + resumeFromActivated = true; + } else { + Logger.debug("Skipping " + subReactor.getReactorName() + + " looking for resume-from module: " + resumeFromModule); + subReactor.setSkipReactor(true); + } } } } diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java index b004d20..35a40b6 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/reactor/MavenDriver.java @@ -133,20 +133,22 @@ private static CommandLine determineMavenCommand( mavenArguments.add("-T1"); } + // Skip the reactors before the one containing the resume-from module, let the rest run. + if (resumeFromModule != null) { + for (String activeModule : reactor.getActiveModules()) { // can't simply do `contains()` + if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name + mavenArguments.add("-rf"); + mavenArguments.add(resumeFromModule); + break; + } + } + } + if (reactor.hasActiveModules()) { mavenArguments.add("--projects"); mavenArguments.add(String.join(",", reactor.getActiveModules())); } - // Skip the reactors before the one containing the resume-from module, let the rest run. - for (String activeModule : reactor.getActiveModules()) { // can't simply do `contains()` - if (activeModule.contains(resumeFromModule)) { // look for the abbreviated module name - mavenArguments.add("-rf"); - mavenArguments.add(resumeFromModule); - break; - } - } - String mvnCommand = determineMvnExecutable(overrideMvnCommand); CommandLine cmdLine = new CommandLine(mvnCommand); From 87b5b856d2a409f30c7adb4dc5fd606f340946e1 Mon Sep 17 00:00:00 2001 From: Ivan Jensen Date: Mon, 14 Feb 2022 11:47:56 -0800 Subject: [PATCH 4/4] Fix typo. --- README.md | 2 +- src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d311ee..0e6d6be 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ usage: mvmin [options] [] [] [] --nbi No build-if dependencies are considered, just changed modules. - Intercepted Maven Options; + Intercepted Maven Options: -rf,--resume-from Resume reactor from specified project (and sub-reactor). -f,--file Not supported, mvnmin will exit. diff --git a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java index 37edad8..2ddb18c 100644 --- a/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java +++ b/src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java @@ -205,7 +205,7 @@ private static void printUsage(final PrintStream out) { out.println(" --nbi No build-if dependencies are considered, just "); out.println(" changed modules."); out.println(); - out.println(" Intercepted Maven Options;"); + out.println(" Intercepted Maven Options:"); out.println(" -rf,--resume-from Resume reactor from specified project (and sub-reactor)."); out.println(" -f,--file Not supported, mvnmin will exit."); out.println();