This commit is contained in:
Phillip Webb
2014-05-15 09:15:35 +01:00
parent 158b6a5c07
commit 01fcf61140
15 changed files with 170 additions and 131 deletions

View File

@@ -110,7 +110,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (project.getPackaging().equals("pom")) {
if (this.project.getPackaging().equals("pom")) {
getLog().debug("repackage goal could not be applied to pom project.");
return;
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2012-2014 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.springframework.boot.maven;
import org.codehaus.plexus.util.cli.CommandLineUtils;
/**
* Parse and expose arguments specified as {@link RunMojo} parameters.
*
* @author Stephane Nicoll
* @since 1.1.0
*/
class RunArguments {
private static final String[] NO_ARGS = {};
private final String[] args;
public RunArguments(String arguments) {
this.args = parseArgs(arguments);
}
private String[] parseArgs(String arguments) {
if (arguments == null || arguments.trim().isEmpty()) {
return NO_ARGS;
}
try {
arguments = arguments.replace('\n', ' ').replace('\t', ' ');
return CommandLineUtils.translateCommandline(arguments);
}
catch (Exception ex) {
throw new IllegalArgumentException("Failed to parse arguments [" + arguments
+ "]", ex);
}
}
public String[] asArray() {
return this.args;
}
}

View File

@@ -39,8 +39,6 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.springframework.boot.loader.tools.FileUtils;
import org.springframework.boot.loader.tools.JavaExecutable;
import org.springframework.boot.loader.tools.MainClassFinder;
@@ -91,9 +89,8 @@ public class RunMojo extends AbstractDependencyFilterMojo {
private Boolean noverify;
/**
* 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.
* 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.
* @since 1.1
*/
@Parameter(property = "run.jvmArguments")
@@ -176,15 +173,23 @@ public class RunMojo extends AbstractDependencyFilterMojo {
}
}
private void addJvmArgs(List<String> args) {
String[] jvmArgs = parseArgs(this.jvmArguments);
Collections.addAll(args, jvmArgs);
logArguments("JVM argument(s): ", jvmArgs);
private void addAgents(List<String> args) {
findAgent();
if (this.agent != null) {
getLog().info("Attaching agents: " + Arrays.asList(this.agent));
for (File agent : this.agent) {
args.add("-javaagent:" + agent);
}
}
if (this.noverify) {
args.add("-noverify");
}
}
private void addArgs(List<String> args) {
Collections.addAll(args, this.arguments);
logArguments("Application argument(s): ", this.arguments);
private void addJvmArgs(List<String> args) {
RunArguments jvmArguments = new RunArguments(this.jvmArguments);
Collections.addAll(args, jvmArguments.asArray());
logArguments("JVM argument(s): ", jvmArguments.asArray());
}
private void addClasspath(List<String> args) throws MojoExecutionException {
@@ -203,17 +208,9 @@ public class RunMojo extends AbstractDependencyFilterMojo {
}
}
private void addAgents(List<String> args) {
findAgent();
if (this.agent != null) {
getLog().info("Attaching agents: " + Arrays.asList(this.agent));
for (File agent : this.agent) {
args.add("-javaagent:" + agent);
}
}
if (this.noverify) {
args.add("-noverify");
}
private void addArgs(List<String> args) {
Collections.addAll(args, this.arguments);
logArguments("Application argument(s): ", this.arguments);
}
private final String getStartClass() throws MojoExecutionException {
@@ -292,28 +289,6 @@ public class RunMojo extends AbstractDependencyFilterMojo {
getLog().debug(sb.toString().trim());
}
/**
* Parse the arguments parameters and return individual arguments.
*
* @param arguments the arguments line to parse
* @return the individual arguments
*/
static String[] parseArgs(String arguments) {
if (arguments == null || arguments.trim().isEmpty()) {
return new String[]{};
}
String args = arguments.replace('\n', ' ');
args = args.replace('\t', ' ');
try {
return CommandLineUtils.translateCommandline(args);
}
catch (Exception e) {
throw new IllegalArgumentException("Failed to parse arguments [" + arguments + "]", e);
}
}
private static class TestArtifactFilter extends AbstractArtifactFeatureFilter {
public TestArtifactFilter() {
super("", Artifact.SCOPE_TEST);

View File

@@ -16,41 +16,44 @@
package org.springframework.boot.maven;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
*
* Tests for {@link RunArguments}.
*
* @author Stephane Nicoll
*/
public class RunMojoTests {
public class RunArgumentsTests {
@Test
public void parseNull() {
String[] args = RunMojo.parseArgs(null);
String[] args = parseArgs(null);
assertNotNull(args);
assertEquals(0, args.length);
}
@Test
public void parseEmpty() {
String[] args = RunMojo.parseArgs(" ");
String[] args = parseArgs(" ");
assertNotNull(args);
assertEquals(0, args.length);
}
@Test
public void parseDebugFlags() {
String[] args = RunMojo.parseArgs("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005");
String[] args = parseArgs("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005");
assertEquals(2, args.length);
assertEquals("-Xdebug", args[0]);
assertEquals("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005", args[1]);
assertEquals("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005",
args[1]);
}
@Test
public void parseWithExtraSpaces() {
String[] args = RunMojo.parseArgs(" -Dfoo=bar -Dfoo2=bar2 ");
String[] args = parseArgs(" -Dfoo=bar -Dfoo2=bar2 ");
assertEquals(2, args.length);
assertEquals("-Dfoo=bar", args[0]);
assertEquals("-Dfoo2=bar2", args[1]);
@@ -58,8 +61,7 @@ public class RunMojoTests {
@Test
public void parseWithNewLinesAndTabs() {
String[] args = RunMojo.parseArgs(" -Dfoo=bar \n" +
"\t\t -Dfoo2=bar2 ");
String[] args = parseArgs(" -Dfoo=bar \n" + "\t\t -Dfoo2=bar2 ");
assertEquals(2, args.length);
assertEquals("-Dfoo=bar", args[0]);
assertEquals("-Dfoo2=bar2", args[1]);
@@ -67,9 +69,13 @@ public class RunMojoTests {
@Test
public void quoteHandledProperly() {
String[] args = RunMojo.parseArgs("-Dvalue=\"My Value\" ");
String[] args = parseArgs("-Dvalue=\"My Value\" ");
assertEquals(1, args.length);
assertEquals("-Dvalue=My Value", args[0]);
}
private String[] parseArgs(String args) {
return new RunArguments(args).asArray();
}
}