Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add working directory configuration option for Maven plugin run/start
Add configuration option that specifies the working directory
of the application ran by Maven plugin's run/start goals.

Closes gh-6243
  • Loading branch information
plamentotev committed Aug 15, 2016
commit 0febe2510333592309ce70707144576713efd458
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<configuration>
<fork>false</fork>
<jvmArguments>-Dfoo=bar</jvmArguments>
<workingDirectory>${project.build.sourceDirectory}</workingDirectory>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import static org.junit.Assert.assertTrue
def file = new File(basedir, "build.log")
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 working directory configuration")

Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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}")

Original file line number Diff line number Diff line change
Expand Up @@ -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.4.1
*/
@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.
Expand Down Expand Up @@ -138,7 +146,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {

/**
* Flag to indicate if the run processes should be forked. By default process forking
* is only used if an agent or jvmArguments are specified.
* is only used if an agent, jvmArguments or working directory are specified.
* @since 1.2
*/
@Parameter(property = "fork")
Expand All @@ -164,7 +172,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
*/
protected boolean isFork() {
return (Boolean.TRUE.equals(this.fork)
|| (this.fork == null && (hasAgent() || hasJvmArgs())));
|| (this.fork == null && (hasAgent() || hasJvmArgs() || hasWorkingDirectorySet())));
}

private boolean hasAgent() {
Expand All @@ -175,6 +183,10 @@ private boolean hasJvmArgs() {
return (this.jvmArguments != null && this.jvmArguments.length() > 0);
}

private boolean hasWorkingDirectorySet() {
return (this.workingDirectory != null);
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.skip) {
Expand Down Expand Up @@ -223,6 +235,9 @@ private void run(String startClassName)
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ this.jvmArguments + "]");
}
if (hasWorkingDirectorySet()) {
getLog().warn("Fork mode disabled, ignoring working directory configuration");
}
runWithMavenJvm(startClassName, resolveApplicationArguments().asArray());
}
}
Expand All @@ -235,16 +250,17 @@ private void doRunWithForkedJvm(String startClassName)
addClasspath(args);
args.add(startClassName);
addArgs(args);
runWithForkedJvm(args);
runWithForkedJvm(args, workingDirectory);
}

/**
* Run with a forked VM, using the specified command line arguments.
* @param args the arguments (JVM arguments and application arguments)
* @param workingDirectory the working directory of the forked JVM
* @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(List<String> args, File workingDirectory)
throws MojoExecutionException, MojoFailureException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.maven;

import java.io.File;
import java.net.URLClassLoader;
import java.util.List;

Expand All @@ -42,9 +43,9 @@ public class RunMojo extends AbstractRunMojo {
private static final int EXIT_CODE_SIGINT = 130;

@Override
protected void runWithForkedJvm(List<String> args) throws MojoExecutionException {
protected void runWithForkedJvm(List<String> args, File workingDirectory) 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()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(List<String> args, File workingDirectory)
throws MojoExecutionException, MojoFailureException {
RunProcess runProcess = runProcess(args);
RunProcess runProcess = runProcess(args, workingDirectory);
try {
waitForSpringApplication();
}
Expand All @@ -103,9 +104,9 @@ protected void runWithForkedJvm(List<String> args)
}
}

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;
}
Expand Down