Merge branch '2.0.x' into 2.1.x

Closes gh-17078
This commit is contained in:
Andy Wilkinson
2019-06-07 10:50:34 +01:00
2691 changed files with 27746 additions and 46049 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -75,8 +75,8 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
this.excludeGroupIds = excludeGroupIds;
}
protected Set<Artifact> filterDependencies(Set<Artifact> dependencies,
FilterArtifacts filters) throws MojoExecutionException {
protected Set<Artifact> filterDependencies(Set<Artifact> dependencies, FilterArtifacts filters)
throws MojoExecutionException {
try {
Set<Artifact> filtered = new LinkedHashSet<>(dependencies);
filtered.retainAll(filters.filter(dependencies));
@@ -97,8 +97,7 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
for (ArtifactsFilter additionalFilter : additionalFilters) {
filters.addFilter(additionalFilter);
}
filters.addFilter(
new MatchingGroupIdFilter(cleanFilterConfig(this.excludeGroupIds)));
filters.addFilter(new MatchingGroupIdFilter(cleanFilterConfig(this.excludeGroupIds)));
if (this.includes != null && !this.includes.isEmpty()) {
filters.addFilter(new IncludeFilter(this.includes));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -203,8 +203,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @return {@code true} if the application process should be forked
*/
protected boolean isFork() {
return (Boolean.TRUE.equals(this.fork)
|| (this.fork == null && enableForkByDefault()));
return (Boolean.TRUE.equals(this.fork) || (this.fork == null && enableForkByDefault()));
}
/**
@@ -213,8 +212,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @see #logDisabledFork()
*/
protected boolean enableForkByDefault() {
return hasAgent() || hasJvmArgs() || hasEnvVariables()
|| hasWorkingDirectorySet();
return hasAgent() || hasJvmArgs() || hasEnvVariables() || hasWorkingDirectorySet();
}
private boolean hasAgent() {
@@ -223,24 +221,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private boolean hasJvmArgs() {
return (this.jvmArguments != null && !this.jvmArguments.isEmpty())
|| (this.systemPropertyVariables != null
&& !this.systemPropertyVariables.isEmpty());
|| (this.systemPropertyVariables != null && !this.systemPropertyVariables.isEmpty());
}
private boolean hasEnvVariables() {
return (this.environmentVariables != null
&& !this.environmentVariables.isEmpty());
return (this.environmentVariables != null && !this.environmentVariables.isEmpty());
}
private boolean hasWorkingDirectorySet() {
return this.workingDirectory != null;
}
private void run(String startClassName)
throws MojoExecutionException, MojoFailureException {
private void run(String startClassName) throws MojoExecutionException, MojoFailureException {
boolean fork = isFork();
this.project.getProperties().setProperty("_spring.boot.fork.enabled",
Boolean.toString(fork));
this.project.getProperties().setProperty("_spring.boot.fork.enabled", Boolean.toString(fork));
if (fork) {
doRunWithForkedJvm(startClassName);
}
@@ -262,29 +256,24 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
if (hasJvmArgs()) {
RunArguments runArguments = resolveJvmArguments();
getLog().warn("Fork mode disabled, ignoring JVM argument(s) [" + Arrays
.stream(runArguments.asArray()).collect(Collectors.joining(" "))
+ "]");
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ Arrays.stream(runArguments.asArray()).collect(Collectors.joining(" ")) + "]");
}
if (hasWorkingDirectorySet()) {
getLog().warn(
"Fork mode disabled, ignoring working directory configuration");
getLog().warn("Fork mode disabled, ignoring working directory configuration");
}
}
}
private void doRunWithForkedJvm(String startClassName)
throws MojoExecutionException, MojoFailureException {
private void doRunWithForkedJvm(String startClassName) throws MojoExecutionException, MojoFailureException {
List<String> args = new ArrayList<>();
addAgents(args);
addJvmArgs(args);
addClasspath(args);
args.add(startClassName);
addArgs(args);
runWithForkedJvm(
(this.workingDirectory != null) ? this.workingDirectory
: this.project.getBasedir(),
args, determineEnvironmentVariables());
runWithForkedJvm((this.workingDirectory != null) ? this.workingDirectory : this.project.getBasedir(), args,
determineEnvironmentVariables());
}
/**
@@ -296,8 +285,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @throws MojoFailureException in case of MOJO failures
*/
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args,
Map<String, String> environmentVariables)
throws MojoExecutionException, MojoFailureException;
Map<String, String> environmentVariables) throws MojoExecutionException, MojoFailureException;
/**
* Run with the current VM, using the specified arguments.
@@ -422,8 +410,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
}
if (mainClass == null) {
throw new MojoExecutionException("Unable to find a suitable main class, "
+ "please add a 'mainClass' property");
throw new MojoExecutionException(
"Unable to find a suitable main class, " + "please add a 'mainClass' property");
}
return mainClass;
}
@@ -455,8 +443,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
for (Resource resource : this.project.getResources()) {
File directory = new File(resource.getDirectory());
urls.add(directory.toURI().toURL());
FileUtils.removeDuplicatesFromOutputDirectory(this.classesDirectory,
directory);
FileUtils.removeDuplicatesFromOutputDirectory(this.classesDirectory, directory);
}
}
}
@@ -465,12 +452,9 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
urls.add(this.classesDirectory.toURI().toURL());
}
private void addDependencies(List<URL> urls)
throws MalformedURLException, MojoExecutionException {
FilterArtifacts filters = (this.useTestClasspath ? getFilters()
: getFilters(new TestArtifactFilter()));
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
filters);
private void addDependencies(List<URL> urls) throws MalformedURLException, MojoExecutionException {
FilterArtifacts filters = (this.useTestClasspath ? getFilters() : getFilters(new TestArtifactFilter()));
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), filters);
for (Artifact artifact : artifacts) {
if (artifact.getFile() != null) {
urls.add(artifact.getFile().toURI().toURL());
@@ -480,8 +464,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private void logArguments(String message, String[] args) {
if (getLog().isDebugEnabled()) {
getLog().debug(
Arrays.stream(args).collect(Collectors.joining(" ", message, "")));
getLog().debug(Arrays.stream(args).collect(Collectors.joining(" ", message, "")));
}
}
@@ -525,9 +508,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
synchronized (this.monitor) {
if (this.exception != null) {
throw new MojoExecutionException(
"An exception occurred while running. "
+ this.exception.getMessage(),
this.exception);
"An exception occurred while running. " + this.exception.getMessage(), this.exception);
}
}
}
@@ -562,9 +543,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
catch (NoSuchMethodException ex) {
Exception wrappedEx = new Exception(
"The specified mainClass doesn't contain a "
+ "main method with appropriate signature.",
ex);
"The specified mainClass doesn't contain a " + "main method with appropriate signature.", ex);
thread.getThreadGroup().uncaughtException(thread, wrappedEx);
}
catch (Exception ex) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -59,8 +59,7 @@ public class ArtifactsLibraries implements Libraries {
private final Log log;
public ArtifactsLibraries(Set<Artifact> artifacts, Collection<Dependency> unpacks,
Log log) {
public ArtifactsLibraries(Set<Artifact> artifacts, Collection<Dependency> unpacks, Log log) {
this.artifacts = artifacts;
this.unpacks = unpacks;
this.log = log;
@@ -78,8 +77,7 @@ public class ArtifactsLibraries implements Libraries {
name = artifact.getGroupId() + "-" + name;
this.log.debug("Renamed to: " + name);
}
callback.library(new Library(name, artifact.getFile(), scope,
isUnpackRequired(artifact)));
callback.library(new Library(name, artifact.getFile(), scope, isUnpackRequired(artifact)));
}
}
}

View File

@@ -41,8 +41,7 @@ import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetail
* @author Stephane Nicoll
* @since 1.4.0
*/
@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
threadSafe = true)
@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class BuildInfoMojo extends AbstractMojo {
@Component
@@ -57,8 +56,7 @@ public class BuildInfoMojo extends AbstractMojo {
/**
* The location of the generated build-info.properties.
*/
@Parameter(
defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
private File outputFile;
/**
@@ -71,16 +69,13 @@ public class BuildInfoMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
new BuildPropertiesWriter(this.outputFile)
.writeBuildProperties(new ProjectDetails(this.project.getGroupId(),
this.project.getArtifactId(), this.project.getVersion(),
this.project.getName(), Instant.now(),
this.additionalProperties));
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(
this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(),
this.project.getName(), Instant.now(), this.additionalProperties));
this.buildContext.refresh(this.outputFile);
}
catch (NullAdditionalPropertyValueException ex) {
throw new MojoFailureException(
"Failed to generate build-info.properties. " + ex.getMessage(), ex);
throw new MojoFailureException("Failed to generate build-info.properties. " + ex.getMessage(), ex);
}
catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -74,8 +74,8 @@ public abstract class DependencyFilter extends AbstractArtifactsFilter {
if (!dependency.getArtifactId().equals(artifact.getArtifactId())) {
return false;
}
return (dependency.getClassifier() == null || artifact.getClassifier() != null
&& dependency.getClassifier().equals(artifact.getClassifier()));
return (dependency.getClassifier() == null
|| artifact.getClassifier() != null && dependency.getClassifier().equals(artifact.getClassifier()));
}
protected final List<? extends FilterableDependency> getFilters() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -58,8 +58,8 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer
}
@Override
public void processResource(String resource, InputStream inputStream,
List<Relocator> relocators) throws IOException {
public void processResource(String resource, InputStream inputStream, List<Relocator> relocators)
throws IOException {
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();

View File

@@ -60,8 +60,7 @@ import org.springframework.boot.loader.tools.Repackager.MainClassTimeoutWarningL
* @author Stephane Nicoll
* @author Björn Lindström
*/
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true,
threadSafe = true,
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class RepackageMojo extends AbstractDependencyFilterMojo {
@@ -220,10 +219,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
Artifact source = getSourceArtifact();
File target = getTargetFile();
Repackager repackager = getRepackager(source.getFile());
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
getFilters(getAdditionalFilters()));
Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack,
getLog());
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), getFilters(getAdditionalFilters()));
Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack, getLog());
try {
LaunchScript launchScript = getLaunchScript();
repackager.repackage(target, libraries, launchScript);
@@ -248,8 +245,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
private Artifact getArtifact(String classifier) {
if (classifier != null) {
for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) {
if (classifier.equals(attachedArtifact.getClassifier())
&& attachedArtifact.getFile() != null
if (classifier.equals(attachedArtifact.getClassifier()) && attachedArtifact.getFile() != null
&& attachedArtifact.getFile().isFile()) {
return attachedArtifact;
}
@@ -266,14 +262,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
if (!this.outputDirectory.exists()) {
this.outputDirectory.mkdirs();
}
return new File(this.outputDirectory, this.finalName + classifier + "."
+ this.project.getArtifact().getArtifactHandler().getExtension());
return new File(this.outputDirectory,
this.finalName + classifier + "." + this.project.getArtifact().getArtifactHandler().getExtension());
}
private Repackager getRepackager(File source) {
Repackager repackager = new Repackager(source, this.layoutFactory);
repackager.addMainClassTimeoutWarningListener(
new LoggingMainClassTimeoutWarningListener());
repackager.addMainClassTimeoutWarningListener(new LoggingMainClassTimeoutWarningListener());
repackager.setMainClass(this.mainClass);
if (this.layout != null) {
getLog().info("Layout: " + this.layout);
@@ -299,8 +294,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
private LaunchScript getLaunchScript() throws IOException {
if (this.executable || this.embeddedLaunchScript != null) {
return new DefaultLaunchScript(this.embeddedLaunchScript,
buildLaunchScriptProperties());
return new DefaultLaunchScript(this.embeddedLaunchScript, buildLaunchScriptProperties());
}
return null;
}
@@ -311,21 +305,17 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
properties.putAll(this.embeddedLaunchScriptProperties);
}
putIfMissing(properties, "initInfoProvides", this.project.getArtifactId());
putIfMissing(properties, "initInfoShortDescription", this.project.getName(),
this.project.getArtifactId());
putIfMissing(properties, "initInfoDescription",
removeLineBreaks(this.project.getDescription()), this.project.getName(),
this.project.getArtifactId());
putIfMissing(properties, "initInfoShortDescription", this.project.getName(), this.project.getArtifactId());
putIfMissing(properties, "initInfoDescription", removeLineBreaks(this.project.getDescription()),
this.project.getName(), this.project.getArtifactId());
return properties;
}
private String removeLineBreaks(String description) {
return (description != null)
? WHITE_SPACE_PATTERN.matcher(description).replaceAll(" ") : null;
return (description != null) ? WHITE_SPACE_PATTERN.matcher(description).replaceAll(" ") : null;
}
private void putIfMissing(Properties properties, String key,
String... valueCandidates) {
private void putIfMissing(Properties properties, String key, String... valueCandidates) {
if (!properties.containsKey(key)) {
for (String candidate : valueCandidates) {
if (candidate != null && !candidate.isEmpty()) {
@@ -341,35 +331,30 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
attachArtifact(source, target);
}
else if (source.getFile().equals(target) && original.exists()) {
String artifactId = (this.classifier != null)
? "artifact with classifier " + this.classifier : "main artifact";
getLog().info(String.format("Updating %s %s to %s", artifactId,
source.getFile(), original));
String artifactId = (this.classifier != null) ? "artifact with classifier " + this.classifier
: "main artifact";
getLog().info(String.format("Updating %s %s to %s", artifactId, source.getFile(), original));
source.setFile(original);
}
else if (this.classifier != null) {
getLog().info("Creating repackaged archive " + target + " with classifier "
+ this.classifier);
getLog().info("Creating repackaged archive " + target + " with classifier " + this.classifier);
}
}
private void attachArtifact(Artifact source, File target) {
if (this.classifier != null && !source.getFile().equals(target)) {
getLog().info("Attaching repackaged archive " + target + " with classifier "
+ this.classifier);
this.projectHelper.attachArtifact(this.project, this.project.getPackaging(),
this.classifier, target);
getLog().info("Attaching repackaged archive " + target + " with classifier " + this.classifier);
this.projectHelper.attachArtifact(this.project, this.project.getPackaging(), this.classifier, target);
}
else {
String artifactId = (this.classifier != null)
? "artifact with classifier " + this.classifier : "main artifact";
String artifactId = (this.classifier != null) ? "artifact with classifier " + this.classifier
: "main artifact";
getLog().info("Replacing " + artifactId + " with repackaged archive");
source.setFile(target);
}
}
private class LoggingMainClassTimeoutWarningListener
implements MainClassTimeoutWarningListener {
private class LoggingMainClassTimeoutWarningListener implements MainClassTimeoutWarningListener {
@Override
public void handleTimeoutWarning(long duration, String mainMethod) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -62,8 +62,7 @@ class RunArguments {
return CommandLineUtils.translateCommandline(arguments);
}
catch (Exception ex) {
throw new IllegalArgumentException(
"Failed to parse arguments [" + arguments + "]", ex);
throw new IllegalArgumentException("Failed to parse arguments [" + arguments + "]", ex);
}
}

View File

@@ -67,19 +67,16 @@ public class RunMojo extends AbstractRunMojo {
}
@Override
protected void runWithForkedJvm(File workingDirectory, List<String> args,
Map<String, String> environmentVariables) throws MojoExecutionException {
protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
throws MojoExecutionException {
try {
RunProcess runProcess = new RunProcess(workingDirectory,
new JavaExecutable().toString());
Runtime.getRuntime()
.addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
Runtime.getRuntime().addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
int exitCode = runProcess.run(true, args, environmentVariables);
if (exitCode == 0 || exitCode == EXIT_CODE_SIGINT) {
return;
}
throw new MojoExecutionException(
"Application finished with exit code: " + exitCode);
throw new MojoExecutionException("Application finished with exit code: " + exitCode);
}
catch (Exception ex) {
throw new MojoExecutionException("Could not exec java", ex);
@@ -87,11 +84,9 @@ public class RunMojo extends AbstractRunMojo {
}
@Override
protected void runWithMavenJvm(String startClassName, String... arguments)
throws MojoExecutionException {
protected void runWithMavenJvm(String startClassName, String... arguments) throws MojoExecutionException {
IsolatedThreadGroup threadGroup = new IsolatedThreadGroup(startClassName);
Thread launchThread = new Thread(threadGroup,
new LaunchRunner(startClassName, arguments), "main");
Thread launchThread = new Thread(threadGroup, new LaunchRunner(startClassName, arguments), "main");
launchThread.setContextClassLoader(new URLClassLoader(getClassPathUrls()));
launchThread.start();
join(threadGroup);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -66,12 +66,10 @@ class SpringApplicationAdminClient {
return false; // Instance not available yet
}
catch (AttributeNotFoundException ex) {
throw new IllegalStateException("Unexpected: attribute 'Ready' not available",
ex);
throw new IllegalStateException("Unexpected: attribute 'Ready' not available", ex);
}
catch (ReflectionException ex) {
throw new MojoExecutionException("Failed to retrieve Ready attribute",
ex.getCause());
throw new MojoExecutionException("Failed to retrieve Ready attribute", ex.getCause());
}
catch (MBeanException | IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
@@ -84,8 +82,7 @@ class SpringApplicationAdminClient {
* @throws IOException if an I/O error occurs
* @throws InstanceNotFoundException if the lifecycle mbean cannot be found
*/
public void stop()
throws MojoExecutionException, IOException, InstanceNotFoundException {
public void stop() throws MojoExecutionException, IOException, InstanceNotFoundException {
try {
this.connection.invoke(this.objectName, "shutdown", null, null);
}

View File

@@ -50,8 +50,7 @@ import org.springframework.boot.loader.tools.RunProcess;
* @since 1.3.0
* @see StopMojo
*/
@Mojo(name = "start", requiresProject = true,
defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST,
@Mojo(name = "start", requiresProject = true, defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST,
requiresDependencyResolution = ResolutionScope.TEST)
public class StartMojo extends AbstractRunMojo {
@@ -91,8 +90,7 @@ public class StartMojo extends AbstractRunMojo {
private final Object lock = new Object();
@Override
protected void runWithForkedJvm(File workingDirectory, List<String> args,
Map<String, String> environmentVariables)
protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
throws MojoExecutionException, MojoFailureException {
RunProcess runProcess = runProcess(workingDirectory, args, environmentVariables);
try {
@@ -104,11 +102,10 @@ public class StartMojo extends AbstractRunMojo {
}
}
private RunProcess runProcess(File workingDirectory, List<String> args,
Map<String, String> environmentVariables) throws MojoExecutionException {
private RunProcess runProcess(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
throws MojoExecutionException {
try {
RunProcess runProcess = new RunProcess(workingDirectory,
new JavaExecutable().toString());
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
runProcess.run(false, args, environmentVariables);
return runProcess;
}
@@ -122,8 +119,7 @@ public class StartMojo extends AbstractRunMojo {
RunArguments applicationArguments = super.resolveApplicationArguments();
applicationArguments.getArgs().addLast(ENABLE_MBEAN_PROPERTY);
if (isFork()) {
applicationArguments.getArgs()
.addLast(JMX_NAME_PROPERTY_PREFIX + this.jmxName);
applicationArguments.getArgs().addLast(JMX_NAME_PROPERTY_PREFIX + this.jmxName);
}
return applicationArguments;
}
@@ -144,18 +140,16 @@ public class StartMojo extends AbstractRunMojo {
}
@Override
protected void runWithMavenJvm(String startClassName, String... arguments)
throws MojoExecutionException {
protected void runWithMavenJvm(String startClassName, String... arguments) throws MojoExecutionException {
IsolatedThreadGroup threadGroup = new IsolatedThreadGroup(startClassName);
Thread launchThread = new Thread(threadGroup,
new LaunchRunner(startClassName, arguments), startClassName + ".main()");
Thread launchThread = new Thread(threadGroup, new LaunchRunner(startClassName, arguments),
startClassName + ".main()");
launchThread.setContextClassLoader(new URLClassLoader(getClassPathUrls()));
launchThread.start();
waitForSpringApplication(this.wait, this.maxAttempts);
}
private void waitForSpringApplication(long wait, int maxAttempts)
throws MojoExecutionException {
private void waitForSpringApplication(long wait, int maxAttempts) throws MojoExecutionException {
SpringApplicationAdminClient client = new SpringApplicationAdminClient(
ManagementFactory.getPlatformMBeanServer(), this.jmxName);
getLog().debug("Waiting for spring application to start...");
@@ -163,8 +157,7 @@ public class StartMojo extends AbstractRunMojo {
if (client.isReady()) {
return;
}
String message = "Spring application is not ready yet, waiting " + wait
+ "ms (attempt " + (i + 1) + ")";
String message = "Spring application is not ready yet, waiting " + wait + "ms (attempt " + (i + 1) + ")";
getLog().debug(message);
synchronized (this.lock) {
try {
@@ -172,18 +165,15 @@ public class StartMojo extends AbstractRunMojo {
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(
"Interrupted while waiting for Spring Boot app to start.");
throw new IllegalStateException("Interrupted while waiting for Spring Boot app to start.");
}
}
}
throw new MojoExecutionException(
"Spring application did not start before the configured timeout ("
+ (wait * maxAttempts) + "ms");
"Spring application did not start before the configured timeout (" + (wait * maxAttempts) + "ms");
}
private void waitForSpringApplication()
throws MojoFailureException, MojoExecutionException {
private void waitForSpringApplication() throws MojoFailureException, MojoExecutionException {
try {
if (isFork()) {
waitForForkedSpringApplication();
@@ -193,26 +183,20 @@ public class StartMojo extends AbstractRunMojo {
}
}
catch (IOException ex) {
throw new MojoFailureException("Could not contact Spring Boot application",
ex);
throw new MojoFailureException("Could not contact Spring Boot application", ex);
}
catch (Exception ex) {
throw new MojoExecutionException(
"Could not figure out if the application has started", ex);
throw new MojoExecutionException("Could not figure out if the application has started", ex);
}
}
private void waitForForkedSpringApplication()
throws IOException, MojoFailureException, MojoExecutionException {
private void waitForForkedSpringApplication() throws IOException, MojoFailureException, MojoExecutionException {
try {
getLog().debug("Connecting to local MBeanServer at port " + this.jmxPort);
try (JMXConnector connector = execute(this.wait, this.maxAttempts,
new CreateJmxConnector(this.jmxPort))) {
try (JMXConnector connector = execute(this.wait, this.maxAttempts, new CreateJmxConnector(this.jmxPort))) {
if (connector == null) {
throw new MojoExecutionException(
"JMX MBean server was not reachable before the configured "
+ "timeout (" + (this.wait * this.maxAttempts)
+ "ms");
throw new MojoExecutionException("JMX MBean server was not reachable before the configured "
+ "timeout (" + (this.wait * this.maxAttempts) + "ms");
}
getLog().debug("Connected to local MBeanServer at port " + this.jmxPort);
MBeanServerConnection connection = connector.getMBeanServerConnection();
@@ -223,21 +207,18 @@ public class StartMojo extends AbstractRunMojo {
throw ex;
}
catch (Exception ex) {
throw new MojoExecutionException(
"Failed to connect to MBean server at port " + this.jmxPort, ex);
throw new MojoExecutionException("Failed to connect to MBean server at port " + this.jmxPort, ex);
}
}
private void doWaitForSpringApplication(MBeanServerConnection connection)
throws IOException, MojoExecutionException, MojoFailureException {
final SpringApplicationAdminClient client = new SpringApplicationAdminClient(
connection, this.jmxName);
final SpringApplicationAdminClient client = new SpringApplicationAdminClient(connection, this.jmxName);
try {
execute(this.wait, this.maxAttempts, () -> (client.isReady() ? true : null));
}
catch (ReflectionException ex) {
throw new MojoExecutionException("Unable to retrieve 'ready' attribute",
ex.getCause());
throw new MojoExecutionException("Unable to retrieve 'ready' attribute", ex.getCause());
}
catch (Exception ex) {
throw new MojoFailureException("Could not invoke shutdown operation", ex);
@@ -254,16 +235,14 @@ public class StartMojo extends AbstractRunMojo {
* @return the result
* @throws Exception in case of execution errors
*/
public <T> T execute(long wait, int maxAttempts, Callable<T> callback)
throws Exception {
public <T> T execute(long wait, int maxAttempts, Callable<T> callback) throws Exception {
getLog().debug("Waiting for spring application to start...");
for (int i = 0; i < maxAttempts; i++) {
T result = callback.call();
if (result != null) {
return result;
}
String message = "Spring application is not ready yet, waiting " + wait
+ "ms (attempt " + (i + 1) + ")";
String message = "Spring application is not ready yet, waiting " + wait + "ms (attempt " + (i + 1) + ")";
getLog().debug(message);
synchronized (this.lock) {
try {
@@ -271,14 +250,12 @@ public class StartMojo extends AbstractRunMojo {
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(
"Interrupted while waiting for Spring Boot app to start.");
throw new IllegalStateException("Interrupted while waiting for Spring Boot app to start.");
}
}
}
throw new MojoExecutionException(
"Spring application did not start before the configured " + "timeout ("
+ (wait * maxAttempts) + "ms");
"Spring application did not start before the configured " + "timeout (" + (wait * maxAttempts) + "ms");
}
private class CreateJmxConnector implements Callable<JMXConnector> {
@@ -296,8 +273,7 @@ public class StartMojo extends AbstractRunMojo {
}
catch (IOException ex) {
if (hasCauseWithType(ex, ConnectException.class)) {
String message = "MBean server at port " + this.port
+ " is not up yet...";
String message = "MBean server at port " + this.port + " is not up yet...";
getLog().debug(message);
return null;
}
@@ -306,8 +282,7 @@ public class StartMojo extends AbstractRunMojo {
}
private boolean hasCauseWithType(Throwable t, Class<? extends Exception> type) {
return type.isAssignableFrom(t.getClass())
|| t.getCause() != null && hasCauseWithType(t.getCause(), type);
return type.isAssignableFrom(t.getClass()) || t.getCause() != null && hasCauseWithType(t.getCause(), type);
}
}

View File

@@ -38,8 +38,7 @@ import org.apache.maven.project.MavenProject;
* @author Stephane Nicoll
* @since 1.3.0
*/
@Mojo(name = "stop", requiresProject = true,
defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
@Mojo(name = "stop", requiresProject = true, defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
public class StopMojo extends AbstractMojo {
/**
@@ -104,15 +103,12 @@ public class StopMojo extends AbstractMojo {
if (this.fork != null) {
return this.fork;
}
String property = this.project.getProperties()
.getProperty("_spring.boot.fork.enabled");
String property = this.project.getProperties().getProperty("_spring.boot.fork.enabled");
return Boolean.valueOf(property);
}
private void stopForkedProcess()
throws IOException, MojoFailureException, MojoExecutionException {
try (JMXConnector connector = SpringApplicationAdminClient
.connect(this.jmxPort)) {
private void stopForkedProcess() throws IOException, MojoFailureException, MojoExecutionException {
try (JMXConnector connector = SpringApplicationAdminClient.connect(this.jmxPort)) {
MBeanServerConnection connection = connector.getMBeanServerConnection();
doStop(connection);
}
@@ -122,16 +118,13 @@ public class StopMojo extends AbstractMojo {
doStop(ManagementFactory.getPlatformMBeanServer());
}
private void doStop(MBeanServerConnection connection)
throws IOException, MojoExecutionException {
private void doStop(MBeanServerConnection connection) throws IOException, MojoExecutionException {
try {
new SpringApplicationAdminClient(connection, this.jmxName).stop();
}
catch (InstanceNotFoundException ex) {
throw new MojoExecutionException(
"Spring application lifecycle JMX bean not found (fork is "
+ this.fork + "). Could not stop application gracefully",
ex);
throw new MojoExecutionException("Spring application lifecycle JMX bean not found (fork is " + this.fork
+ "). Could not stop application gracefully", ex);
}
}