Enable fork more when devtools is present

This commit improves the run goal to automatically fork the process when
devtools is present and log a warning when fork has been disabled via
configuration since devtools will not work on a non-forked process.

We don't want devtools to kick in for integration tests so the logic has
been placed in `RunMojo` requiring a couple of protected methods to
override.

Closes gh-5137
This commit is contained in:
Stephane Nicoll
2016-08-31 12:10:35 +02:00
parent 7765d31edd
commit ff48a88b91
7 changed files with 174 additions and 27 deletions

View File

@@ -140,7 +140,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.
* automatically enabled if an agent or jvmArguments are specified, or if
* devtools is present.
* @since 1.2
*/
@Parameter(property = "fork")
@@ -160,23 +161,6 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
@Parameter(defaultValue = "false")
private boolean skip;
/**
* Specify if the application process should be forked.
* @return {@code true} if the application process should be forked
*/
protected boolean isFork() {
return (Boolean.TRUE.equals(this.fork)
|| (this.fork == null && (hasAgent() || hasJvmArgs())));
}
private boolean hasAgent() {
return (this.agent != null && this.agent.length > 0);
}
private boolean hasJvmArgs() {
return (this.jvmArguments != null && this.jvmArguments.length() > 0);
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.skip) {
@@ -187,6 +171,32 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
run(startClassName);
}
/**
* Specify if the application process should be forked.
* @return {@code true} if the application process should be forked
*/
protected boolean isFork() {
return (Boolean.TRUE.equals(this.fork)
|| (this.fork == null && enableForkByDefault()));
}
/**
* Specify if fork should be enabled by default.
* @return {@code true} if fork should be enabled by default
* @see #logDisabledFork()
*/
protected boolean enableForkByDefault() {
return hasAgent() || hasJvmArgs();
}
private boolean hasAgent() {
return (this.agent != null && this.agent.length > 0);
}
private boolean hasJvmArgs() {
return (this.jvmArguments != null && this.jvmArguments.length() > 0);
}
private void findAgent() {
try {
if (this.agent == null || this.agent.length == 0) {
@@ -221,17 +231,26 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
doRunWithForkedJvm(startClassName);
}
else {
if (hasAgent()) {
getLog().warn("Fork mode disabled, ignoring agent");
}
if (hasJvmArgs()) {
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ this.jvmArguments + "]");
}
logDisabledFork();
runWithMavenJvm(startClassName, resolveApplicationArguments().asArray());
}
}
/**
* Log a warning indicating that fork mode has been explicitly disabled
* while some conditions are present that require to enable it.
* @see #enableForkByDefault()
*/
protected void logDisabledFork() {
if (hasAgent()) {
getLog().warn("Fork mode disabled, ignoring agent");
}
if (hasJvmArgs()) {
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ this.jvmArguments + "]");
}
}
private void doRunWithForkedJvm(String startClassName)
throws MojoExecutionException, MojoFailureException {
List<String> args = new ArrayList<String>();

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.maven;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
@@ -41,6 +42,26 @@ public class RunMojo extends AbstractRunMojo {
private static final int EXIT_CODE_SIGINT = 130;
private static final String RESTARTER_CLASS_LOCATION = "org/springframework/boot/devtools/restart/Restarter.class";
/**
* Devtools presence flag to avoid checking for it several times per execution.
*/
private Boolean hasDevtools;
@Override
protected boolean enableForkByDefault() {
return super.enableForkByDefault() || hasDevtools();
}
@Override
protected void logDisabledFork() {
super.logDisabledFork();
if (hasDevtools()) {
getLog().warn("Fork mode disabled, devtools will be disabled");
}
}
@Override
protected void runWithForkedJvm(List<String> args) throws MojoExecutionException {
try {
@@ -92,6 +113,24 @@ public class RunMojo extends AbstractRunMojo {
while (hasNonDaemonThreads);
}
private boolean hasDevtools() {
if (this.hasDevtools == null) {
this.hasDevtools = checkForDevtools();
}
return this.hasDevtools;
}
private boolean checkForDevtools() {
try {
URL[] urls = getClassPathUrls();
URLClassLoader classLoader = new URLClassLoader(urls);
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
}
catch (Exception ex) {
return false;
}
}
private static final class RunProcessKiller implements Runnable {
private final RunProcess runProcess;