Skip to content

Commit

Permalink
Ensure thin jar launcher is copied to thin.root in Maven plugin
Browse files Browse the repository at this point in the history
Fixes gh-146
  • Loading branch information
Dave Syer committed Jun 23, 2020
1 parent 1525fb8 commit 50233a4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
Expand Up @@ -51,18 +51,19 @@
* @author Dave Syer
*
*/
@Mojo(name = "resolve", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.NONE, requiresDependencyCollection = ResolutionScope.NONE)
@Mojo(name = "resolve", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
requiresDependencyResolution = ResolutionScope.NONE, requiresDependencyCollection = ResolutionScope.NONE)
public class ResolveMojo extends ThinJarMojo {

/**
* Directory containing the downloaded archives.
*/
@Parameter(defaultValue = "${project.build.directory}/thin/root", required = true, property = "thin.outputDirectory")
@Parameter(defaultValue = "${project.build.directory}/thin/root", required = true,
property = "thin.outputDirectory")
private File outputDirectory;

/**
* A list of the deployable thin libraries that must be downloaded and
* assembled.
* A list of the deployable thin libraries that must be downloaded and assembled.
*/
@Parameter
private List<Dependency> deployables;
Expand Down Expand Up @@ -129,22 +130,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"No deployables found. If your only deployable is the current project jar, you need to run 'mvn package' at the same time.");
}

File thinJar = downloadThinJar();
File thinJar = downloadThinJar(outputDirectory);

for (File deployable : deployables) {
getLog().info("Deploying: " + deployable);
try {
getLog().info(
"Copying: " + deployable.getName() + " to " + outputDirectory);
getLog().info("Copying: " + deployable.getName() + " to " + outputDirectory);

FileUtils.copyFile(deployable,
new File(outputDirectory, deployable.getName()));
runWithForkedJvm(thinJar, outputDirectory,
"--thin.archive=" + deployable.getAbsolutePath());
FileUtils.copyFile(deployable, new File(outputDirectory, deployable.getName()));
runWithForkedJvm(thinJar, outputDirectory, "--thin.archive=" + deployable.getAbsolutePath());
}
catch (Exception e) {
throw new MojoExecutionException("Cannot locate deployable " + deployable,
e);
throw new MojoExecutionException("Cannot locate deployable " + deployable, e);
}
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.experimental.maven;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -34,6 +35,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

import org.springframework.boot.loader.tools.JavaExecutable;
Expand Down Expand Up @@ -165,9 +167,22 @@ private ArtifactResolutionRequest getRequest(Artifact artifact) {
return request;
}

protected File downloadThinJar() throws MojoFailureException {
protected File downloadThinJar(File outputDirectory) throws MojoFailureException {
Dependency deployable = decode(thinLauncherArtifact);
return resolveFile(deployable);
File result = resolveFile(deployable);
try {
if (!result.getCanonicalPath().startsWith(outputDirectory.getCanonicalPath())) {
String root = new File(this.settings.getLocalRepository()).getCanonicalPath();
String path = result.getCanonicalPath().substring(root.length());
File target = new File(new File(outputDirectory, "repository"), path);
FileUtils.copyFile(result, target);
result = target;
}
}
catch (IOException e) {
throw new IllegalStateException("Could not copy thin jar launcher", e);
}
return result;
}

private Dependency decode(String artifact) throws MojoFailureException {
Expand Down
41 changes: 22 additions & 19 deletions samples/tests/src/test/java/com/example/AppMavenIT.java
Expand Up @@ -55,11 +55,19 @@ public boolean matches(File value) {
});
}

@Test
public void launcherDownloaded() {
File downloaded = new File("../app/target/thin/root/repository/org/springframework/boot/experimental");
assertThat(downloaded).exists();
downloaded = new File(downloaded,
"spring-boot-thin-launcher/1.0.26.BUILD-SNAPSHOT/spring-boot-thin-launcher-1.0.26.BUILD-SNAPSHOT-exec.jar");
assertThat(downloaded).exists();
}

@Test
public void runJar() throws Exception {
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m",
"-noverify", "-XX:TieredStopAtLevel=1",
"-Djava.security.egd=file:/dev/./urandom", "-jar",
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m", "-noverify",
"-XX:TieredStopAtLevel=1", "-Djava.security.egd=file:/dev/./urandom", "-jar",
"../app/target/app-0.0.1-SNAPSHOT.jar", "--server.port=0");
builder.redirectErrorStream(true);
started = builder.start();
Expand All @@ -72,9 +80,8 @@ public void runJar() throws Exception {

@Test
public void runJarCustomProperties() throws Exception {
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m",
"-noverify", "-XX:TieredStopAtLevel=1",
"-Djava.security.egd=file:/dev/./urandom", "-jar",
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m", "-noverify",
"-XX:TieredStopAtLevel=1", "-Djava.security.egd=file:/dev/./urandom", "-jar",
"../../../../../app/target/app-0.0.1-SNAPSHOT.jar", "--server.port=0");
builder.redirectErrorStream(true);
builder.directory(new File("src/test/resources/app"));
Expand All @@ -86,13 +93,11 @@ public void runJarCustomProperties() throws Exception {

@Test
public void runJarNamedProperties() throws Exception {
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m",
"-noverify", "-XX:TieredStopAtLevel=1",
"-Djava.security.egd=file:/dev/./urandom", "-Dthin.debug",
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m", "-noverify",
"-XX:TieredStopAtLevel=1", "-Djava.security.egd=file:/dev/./urandom", "-Dthin.debug",
// Switches to Spring Boot 2.0.1
"-Dthin.name=app", //
"-jar", "../../../../../app/target/app-0.0.1-SNAPSHOT.jar",
"--server.port=0");
"-jar", "../../../../../app/target/app-0.0.1-SNAPSHOT.jar", "--server.port=0");
builder.redirectErrorStream(true);
builder.directory(new File("src/test/resources/app"));
started = builder.start();
Expand All @@ -103,9 +108,8 @@ public void runJarNamedProperties() throws Exception {

@Test
public void runJarNamedPropertiesEnvVar() throws Exception {
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m",
"-noverify", "-XX:TieredStopAtLevel=1",
"-Djava.security.egd=file:/dev/./urandom", "-jar",
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m", "-noverify",
"-XX:TieredStopAtLevel=1", "-Djava.security.egd=file:/dev/./urandom", "-jar",
"../../../../../app/target/app-0.0.1-SNAPSHOT.jar", "--server.port=0");
// Switches to Spring Boot 2.0.1
builder.environment().put("THIN_NAME", "app");
Expand All @@ -119,9 +123,8 @@ public void runJarNamedPropertiesEnvVar() throws Exception {

@Test
public void runJarExternalArchive() throws Exception {
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m",
"-noverify", "-XX:TieredStopAtLevel=1",
"-Djava.security.egd=file:/dev/./urandom",
ProcessBuilder builder = new ProcessBuilder(Utils.javaCommand(), "-Xmx128m", "-noverify",
"-XX:TieredStopAtLevel=1", "-Djava.security.egd=file:/dev/./urandom",
"-Dthin.archive=maven://com.example:simple:0.0.1-SNAPSHOT", "-jar",
"../app/target/app-0.0.1-SNAPSHOT.jar", "--server.port=0");
builder.redirectErrorStream(true);
Expand All @@ -131,8 +134,7 @@ public void runJarExternalArchive() throws Exception {
assertThat(output).contains("2.2.4.RELEASE");
}

private static String output(InputStream inputStream, String marker)
throws IOException {
private static String output(InputStream inputStream, String marker) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
Expand All @@ -150,4 +152,5 @@ private static String output(InputStream inputStream, String marker)
}
return sb.toString();
}

}

0 comments on commit 50233a4

Please sign in to comment.