Add support for Spring Loaded in Maven and Gradle

Requires Loaded 1.1.5 (or better).

For Maven you can just add springloaded to the dependencies of the
spring-boot plugin (and also set MAVEN_OPTS=-noverify).

For Gradle add springloaded to the build dependencies (-noverify
can be added by the plugin).

In both cases there is also support for adding an arbitrary java agent
via configuration. Samples are provided in
spring-boot-sample-[simple,web-ui].

The ApplicationPlugin is only added if there is no JavaExec task
already present, and additionally it computes its own man class if
none is provided. So "gradle run" and "gradle bootRun" look
superficially similar, but "bootRun" has extra options, including
the agent and Loaded support.

Fixes gh-251, gh-183
This commit is contained in:
Dave Syer
2013-12-27 08:35:19 +00:00
parent f888567c1d
commit 77bac876ce
14 changed files with 463 additions and 34 deletions

View File

@@ -22,6 +22,7 @@ import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
@@ -36,6 +37,7 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.springframework.boot.loader.tools.AgentAttacher;
import org.springframework.boot.loader.tools.MainClassFinder;
/**
@@ -63,6 +65,18 @@ public class RunMojo extends AbstractMojo {
@Parameter(property = "run.addResources", defaultValue = "true")
private boolean addResources;
/**
* Path to agent jar.
*/
@Parameter(property = "run.agent")
private File agent;
/**
* Flag to say that the agent requires -noverify.
*/
@Parameter(property = "run.noverify")
private Boolean noverify;
/**
* Arguments that should be passed to the application.
*/
@@ -91,7 +105,39 @@ public class RunMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
findAgent();
if (this.agent != null) {
getLog().info("Attaching agent: " + this.agent);
if (this.noverify != null && this.noverify && !AgentAttacher.hasNoVerify()) {
throw new MojoExecutionException(
"The JVM must be started with -noverify for this agent to work. You can use MAVEN_OPTS to add that flag.");
}
AgentAttacher.attach(this.agent);
}
final String startClassName = getStartClass();
run(startClassName);
}
private void findAgent() {
try {
Class<?> loaded = Class
.forName("org.springsource.loaded.agent.SpringLoadedAgent");
if (this.agent == null && loaded != null) {
if (this.noverify == null) {
this.noverify = true;
}
CodeSource source = loaded.getProtectionDomain().getCodeSource();
if (source != null) {
this.agent = new File(source.getLocation().getFile());
}
}
}
catch (ClassNotFoundException e) {
// ignore;
}
}
private void run(String startClassName) throws MojoExecutionException {
IsolatedThreadGroup threadGroup = new IsolatedThreadGroup(startClassName);
Thread launchThread = new Thread(threadGroup, new LaunchRunner(startClassName,
this.arguments), startClassName + ".main()");