Add workingDirectory option for Maven plugin run/start

Add configuration option that specifies the working directory to use to
run/start the application via the Maven plugin.

Closes gh-6243
This commit is contained in:
Plamen Totev
2016-08-13 21:45:00 +03:00
committed by Stephane Nicoll
parent f0b235cbc5
commit 3cccc732df
9 changed files with 115 additions and 12 deletions

View File

@@ -87,6 +87,14 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
@Parameter(property = "run.noverify")
private Boolean noverify;
/**
* The working directory of the application. If specified by default the process
* will be started by forking a new JVM.
* @since 1.5
*/
@Parameter(property = "run.workingDirectory")
private File workingDirectory;
/**
* JVM arguments that should be associated with the forked process used to run the
* application. On command line, make sure to wrap multiple values between quotes.
@@ -140,8 +148,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
/**
* Flag to indicate if the run processes should be forked. {@code fork} is
* automatically enabled if an agent or jvmArguments are specified, or if devtools is
* present.
* automatically enabled if an agent, jvmArguments or working directory are
* specified, or if devtools is present.
* @since 1.2
*/
@Parameter(property = "fork")
@@ -185,7 +193,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @see #logDisabledFork()
*/
protected boolean enableForkByDefault() {
return hasAgent() || hasJvmArgs();
return hasAgent() || hasJvmArgs() || hasWorkingDirectorySet();
}
private boolean hasAgent() {
@@ -196,6 +204,10 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
return (this.jvmArguments != null && this.jvmArguments.length() > 0);
}
private boolean hasWorkingDirectorySet() {
return (this.workingDirectory != null);
}
private void findAgent() {
try {
if (this.agent == null || this.agent.length == 0) {
@@ -248,6 +260,9 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ this.jvmArguments + "]");
}
if (hasWorkingDirectorySet()) {
getLog().warn("Fork mode disabled, ignoring working directory configuration");
}
}
private void doRunWithForkedJvm(String startClassName)
@@ -258,16 +273,17 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
addClasspath(args);
args.add(startClassName);
addArgs(args);
runWithForkedJvm(args);
runWithForkedJvm(this.workingDirectory, args);
}
/**
* Run with a forked VM, using the specified command line arguments.
* @param workingDirectory the working directory of the forked JVM
* @param args the arguments (JVM arguments and application arguments)
* @throws MojoExecutionException in case of MOJO execution errors
* @throws MojoFailureException in case of MOJO failures
*/
protected abstract void runWithForkedJvm(List<String> args)
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args)
throws MojoExecutionException, MojoFailureException;
/**

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.maven;
import java.net.URL;
import java.io.File;
import java.net.URLClassLoader;
import java.util.List;
@@ -63,9 +64,9 @@ public class RunMojo extends AbstractRunMojo {
}
@Override
protected void runWithForkedJvm(List<String> args) throws MojoExecutionException {
protected void runWithForkedJvm(File workingDirectory, List<String> args) throws MojoExecutionException {
try {
RunProcess runProcess = new RunProcess(new JavaExecutable().toString());
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
Runtime.getRuntime()
.addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
int exitCode = runProcess.run(true, args.toArray(new String[args.size()]));

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.maven;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.ConnectException;
@@ -87,9 +88,9 @@ public class StartMojo extends AbstractRunMojo {
private final Object lock = new Object();
@Override
protected void runWithForkedJvm(List<String> args)
protected void runWithForkedJvm(File workingDirectory, List<String> args)
throws MojoExecutionException, MojoFailureException {
RunProcess runProcess = runProcess(args);
RunProcess runProcess = runProcess(args, workingDirectory);
try {
waitForSpringApplication();
}
@@ -103,9 +104,9 @@ public class StartMojo extends AbstractRunMojo {
}
}
private RunProcess runProcess(List<String> args) throws MojoExecutionException {
private RunProcess runProcess(List<String> args, File workingDirectory) throws MojoExecutionException {
try {
RunProcess runProcess = new RunProcess(new JavaExecutable().toString());
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
runProcess.run(false, args.toArray(new String[args.size()]));
return runProcess;
}