Merge branch '1.5.x'

pull/6666/merge
Stephane Nicoll 8 years ago
commit c50a352999

@ -17,6 +17,7 @@
package org.springframework.boot.loader.tools; package org.springframework.boot.loader.tools;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -41,16 +42,34 @@ public class RunProcess {
private static final long JUST_ENDED_LIMIT = 500; private static final long JUST_ENDED_LIMIT = 500;
private File workingDirectory;
private final String[] command; private final String[] command;
private volatile Process process; private volatile Process process;
private volatile long endTime; private volatile long endTime;
public RunProcess(String... command) { /**
* Creates new {@link RunProcess} instance for the specified command
* and working directory.
* @param workingDirectory the working directory of the child process or {@code null}
* to run in the working directory of the current Java process
* @param command the program to execute and it's arguments
*/
public RunProcess(File workingDirectory, String... command) {
this.workingDirectory = workingDirectory;
this.command = command; this.command = command;
} }
/**
* Creates new {@link RunProcess} instance for the specified command.
* @param command the program to execute and it's arguments
*/
public RunProcess(String... command) {
this(null, command);
}
public int run(boolean waitForProcess, String... args) throws IOException { public int run(boolean waitForProcess, String... args) throws IOException {
return run(waitForProcess, Arrays.asList(args)); return run(waitForProcess, Arrays.asList(args));
} }
@ -58,6 +77,7 @@ public class RunProcess {
protected int run(boolean waitForProcess, Collection<String> args) protected int run(boolean waitForProcess, Collection<String> args)
throws IOException { throws IOException {
ProcessBuilder builder = new ProcessBuilder(this.command); ProcessBuilder builder = new ProcessBuilder(this.command);
builder.directory(this.workingDirectory);
builder.command().addAll(args); builder.command().addAll(args);
builder.redirectErrorStream(true); builder.redirectErrorStream(true);
boolean inheritedIO = inheritIO(builder); boolean inheritedIO = inheritIO(builder);

@ -23,6 +23,7 @@
<configuration> <configuration>
<fork>false</fork> <fork>false</fork>
<jvmArguments>-Dfoo=bar</jvmArguments> <jvmArguments>-Dfoo=bar</jvmArguments>
<workingDirectory>${project.build.sourceDirectory}</workingDirectory>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

@ -3,4 +3,5 @@ import static org.junit.Assert.assertTrue
def file = new File(basedir, "build.log") def file = new File(basedir, "build.log")
assertTrue file.text.contains("I haz been run") assertTrue file.text.contains("I haz been run")
assertTrue file.text.contains("Fork mode disabled, ignoring JVM argument(s) [-Dfoo=bar]") assertTrue file.text.contains("Fork mode disabled, ignoring JVM argument(s) [-Dfoo=bar]")
assertTrue file.text.contains("Fork mode disabled, ignoring working directory configuration")

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-working-directory</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<workingDirectory>${project.build.sourceDirectory}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,26 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
public class SampleApplication {
public static void main(String[] args) {
String workingDirectory = System.getProperty("user.dir");
System.out.println(String.format("I haz been run from %s", workingDirectory));
}
}

@ -0,0 +1,6 @@
import static org.junit.Assert.assertTrue
def file = new File(basedir, "build.log")
def workDir = new File(basedir, "src/main/java").getAbsolutePath()
assertTrue file.text.contains("I haz been run from ${workDir}")

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

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.boot.maven; package org.springframework.boot.maven;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.net.ConnectException; import java.net.ConnectException;
@ -87,9 +88,9 @@ public class StartMojo extends AbstractRunMojo {
private final Object lock = new Object(); private final Object lock = new Object();
@Override @Override
protected void runWithForkedJvm(List<String> args) protected void runWithForkedJvm(File workingDirectory, List<String> args)
throws MojoExecutionException, MojoFailureException { throws MojoExecutionException, MojoFailureException {
RunProcess runProcess = runProcess(args); RunProcess runProcess = runProcess(workingDirectory, args);
try { try {
waitForSpringApplication(); waitForSpringApplication();
} }
@ -103,9 +104,11 @@ public class StartMojo extends AbstractRunMojo {
} }
} }
private RunProcess runProcess(List<String> args) throws MojoExecutionException { private RunProcess runProcess(File workingDirectory, List<String> args)
throws MojoExecutionException {
try { 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()])); runProcess.run(false, args.toArray(new String[args.size()]));
return runProcess; return runProcess;
} }

Loading…
Cancel
Save