Skip to content

Commit

Permalink
Merge pull request #5895 from jaikiran/qk-5880
Browse files Browse the repository at this point in the history
java.library.path should be configurable when launching the application's native image
  • Loading branch information
gsmet authored Dec 2, 2019
2 parents 88ad7f1 + 633d976 commit d5eff14
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ void build(SslContextConfigurationRecorder sslContextConfigurationRecorder,
} else {
// On MacOS, the SunEC library is directly in jre/lib/
// This is useful for testing or if you have a similar environment in production
systemProperty.produce(new SystemPropertyBuildItem("java.library.path", graalVmLibDirectory.toString()));
javaLibraryPathAdditionalPath
.produce(new JavaLibraryPathAdditionalPathBuildItem(graalVmLibDirectory.toString()));
}

// This is useful for testing but the user will have to override it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.quarkus.maven.it;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;

import io.quarkus.maven.it.verifier.MavenProcessInvocationResult;
import io.quarkus.maven.it.verifier.RunningInvoker;

/**
*
*/
public class NativeImageIT extends MojoTestBase {

/**
* Tests that the {@code java.library.path} can be overridden/configurable by passing the system property
* when launching the generated application's native image.
*
* @throws Exception
*/
@Test
public void testJavaLibraryPathAtRuntime() throws Exception {
final File testDir = initProject("projects/native-image-app", "projects/native-image-app-output");
final RunningInvoker running = new RunningInvoker(testDir, false);

// trigger mvn package -Pnative -Dquarkus.ssl.native=true
final String[] mvnArgs = new String[] { "package", "-DskipTests", "-Pnative", "-Dquarkus.ssl.native=true" };
final MavenProcessInvocationResult result = running.execute(Arrays.asList(mvnArgs), Collections.emptyMap());
await().atMost(5, TimeUnit.MINUTES).until(() -> result.getProcess() != null && !result.getProcess().isAlive());
final String processLog = running.log();
try {
assertThat(processLog).containsIgnoringCase("BUILD SUCCESS");
} catch (AssertionError ae) {
// skip this test (instead of failing), if the native-image command wasn't available.
// Bit brittle to rely on the log message, but it's OK in the context of this test
Assumptions.assumeFalse(processLog.contains("Cannot find the `native-image"),
"Skipping test since native-image tool isn't available");
// native-image command was available but the build failed for some reason, throw the original error
throw ae;
} finally {
running.stop();
}

// now that the native image is built, run it
final Path nativeImageRunner = testDir.toPath().toAbsolutePath().resolve(Paths.get("target/acme-1.0-SNAPSHOT-runner"));
final Path tmpDir = Files.createTempDirectory("native-image-test");
tmpDir.toFile().deleteOnExit();
final Process nativeImageRunWithAdditionalLibPath = runNativeImage(nativeImageRunner,
new String[] { "-Djava.library.path=" + tmpDir.toString() });
try {
final String response = getHttpResponse("/hello/javaLibraryPath");
Assertions.assertTrue(response.contains(tmpDir.toString()),
"Response " + response + " for java.library.path was expected to contain the " + tmpDir + ", but didn't");
} finally {
nativeImageRunWithAdditionalLibPath.destroy();
}

}

private static Process runNativeImage(final Path nativeImageRunnerFile, final String[] params) throws Exception {
final List<String> commands = new ArrayList<>();
commands.add(nativeImageRunnerFile.toString());
if (params != null) {
commands.addAll(Arrays.asList(params));
}
final ProcessBuilder processBuilder = new ProcessBuilder(commands.toArray(new String[0]));
processBuilder.inheritIO();
return processBuilder.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>acme</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- insert test dependencies here -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<configuration>
<enableHttpUrlHandler>true</enableHttpUrlHandler>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.acme;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}

@GET
@Path("/javaLibraryPath")
@Produces(MediaType.TEXT_PLAIN)
public String javaLibraryPath() {
return System.getProperty("java.library.path");
}

}

0 comments on commit d5eff14

Please sign in to comment.