Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt to make -rf work with #18

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ usage: mvmin [options] [<maven goal(s)>] [<maven phase(s)>] [<maven arg(s)>]
--nbi No build-if dependencies are considered, just
changed modules.

Intercepted Maven Options:
-rf,--resume-from <arg> Resume reactor from specified project (and sub-reactor).
-f,--file <arg> Not supported, mvnmin will exit.

Scripting
-p Don't invoke maven, print out activated projects,
sorted, newline separated.
Expand Down
86 changes: 61 additions & 25 deletions src/main/java/com/elasticpath/tools/mavenminimal/MvnMinCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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 <arg> Resume reactor from specified project (and sub-reactor).");
out.println(" -f,--file <arg> 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.");
Expand Down Expand Up @@ -280,12 +294,32 @@ 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.
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);
}
}
}
}
}

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.");
Expand All @@ -299,6 +333,8 @@ private static List<String> removeNonMavenArgs(final String[] args) {
List<String> 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ 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(
final Reactor reactor, final List<String> 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()) {
Expand Down Expand Up @@ -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<String> inputArgs, final String overrideMvnCommand) {
private static CommandLine determineMavenCommand(
final Reactor reactor, final List<String> inputArgs, final String overrideMvnCommand, final String resumeFromModule) {
AtomicReference<String> goal = new AtomicReference<>("");

List<String> mavenMinimalArguments = new ArrayList<>(inputArgs);
Expand Down Expand Up @@ -130,6 +133,17 @@ private static CommandLine determineMavenCommand(final Reactor reactor, final Li
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()));
Expand Down