Skip to content

Commit

Permalink
Fix native-image-maven-plugin for Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
olpaw committed Oct 25, 2019
1 parent bbfd632 commit 84eed7e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion substratevm/ci_includes/gate.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ builds += [
${labsjdk-ee-11} ${svm-common-linux-gate} {
name: "gate-svm-build-ee-11"
run: [
${svm-cmd-gate} ["build,helloworld,test,relocations"]
${svm-cmd-gate} ["build,helloworld,test,relocations,maven"]
]
}
${oraclejdk8} ${svm-common-linux-gate} ${eclipse} ${jdt} ${linux-deploy} {
Expand Down
5 changes: 4 additions & 1 deletion substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,10 @@ def maven_plugin_test(args):
svm_version = suite.release_version(snapshotSuffix='SNAPSHOT')
pom_from_template(proj_dir, svm_version)
# Build native image with native-image-maven-plugin
mx.run_maven(['package'], cwd=proj_dir)
env = os.environ.copy()
if not svm_java8():
env['MAVEN_OPTS'] = '--add-exports=java.base/jdk.internal.module=ALL-UNNAMED'
mx.run_maven(['package'], cwd=proj_dir, env=env)
mx.run([join(proj_dir, 'target', 'com.oracle.substratevm.nativeimagemojotest')])


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ public interface BuildConfiguration {
* @return the name of the image generator main class.
*/
default String getGeneratorMainClass() {
return DEFAULT_GENERATOR_CLASS_NAME;
String generatorClassName = DEFAULT_GENERATOR_CLASS_NAME;
if (useJavaModules()) {
generatorClassName += DEFAULT_GENERATOR_9PLUS_SUFFIX;
}
return generatorClassName;
}

/**
Expand Down Expand Up @@ -396,15 +400,13 @@ private static class DefaultBuildConfiguration implements BuildConfiguration {
private final Path workDir;
private final Path rootDir;
private final List<String> args;
private final String generatorClassName;

DefaultBuildConfiguration(String generatorClassName, List<String> args) {
this(generatorClassName, null, null, args);
DefaultBuildConfiguration(List<String> args) {
this(null, null, args);
}

@SuppressWarnings("deprecation")
DefaultBuildConfiguration(String generatorClassName, Path rootDir, Path workDir, List<String> args) {
this.generatorClassName = generatorClassName;
DefaultBuildConfiguration(Path rootDir, Path workDir, List<String> args) {
this.args = args;
this.workDir = workDir != null ? workDir : Paths.get(".").toAbsolutePath().normalize();
if (rootDir != null) {
Expand Down Expand Up @@ -433,11 +435,6 @@ private static class DefaultBuildConfiguration implements BuildConfiguration {
}
}

@Override
public String getGeneratorMainClass() {
return generatorClassName;
}

@Override
public Path getWorkingDirectory() {
return workDir;
Expand Down Expand Up @@ -1177,24 +1174,16 @@ protected int buildImage(List<String> javaArgs, LinkedHashSet<Path> bcp, LinkedH

private static final Function<BuildConfiguration, NativeImage> defaultNativeImageProvider = config -> IS_AOT ? NativeImageServer.create(config) : new NativeImage(config);

private static void main(String[] args, String generatorClassName) {
performBuild(new DefaultBuildConfiguration(generatorClassName, Arrays.asList(args)), defaultNativeImageProvider);
}

public static void main(String[] args) {
main(args, DEFAULT_GENERATOR_CLASS_NAME);
performBuild(new DefaultBuildConfiguration(Arrays.asList(args)), defaultNativeImageProvider);
}

public static void build(BuildConfiguration config) {
build(config, defaultNativeImageProvider);
}

public static void agentBuild(Path javaHome, Path workDir, List<String> buildArgs) {
String generatorClassName = DEFAULT_GENERATOR_CLASS_NAME;
if (JavaVersionUtil.JAVA_SPEC > 8) {
generatorClassName += DEFAULT_GENERATOR_9PLUS_SUFFIX;
}
performBuild(new DefaultBuildConfiguration(generatorClassName, javaHome, workDir, buildArgs), NativeImage::new);
performBuild(new DefaultBuildConfiguration(javaHome, workDir, buildArgs), NativeImage::new);
}

public static Map<Path, List<String>> extractEmbeddedImageArgs(Path workDir, String[] imageClasspath) {
Expand Down Expand Up @@ -1670,7 +1659,7 @@ public static void main(String[] args) {
ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.internal.vm.compiler", false);
ModuleSupport.exportAndOpenAllPackagesToUnnamed("com.oracle.graal.graal_enterprise", true);
}
NativeImage.main(args, DEFAULT_GENERATOR_CLASS_NAME + DEFAULT_GENERATOR_9PLUS_SUFFIX);
NativeImage.main(args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ questions.
<url>https://github.com/oracle/graal/tree/master/substratevm</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

import com.oracle.svm.core.OS;
import com.oracle.svm.driver.NativeImage;
import com.oracle.svm.util.ModuleSupport;

@Mojo(name = "native-image", defaultPhase = LifecyclePhase.PACKAGE)
public class NativeImageMojo extends AbstractMojo {
Expand Down Expand Up @@ -230,6 +231,7 @@ public void execute() throws MojoExecutionException {
}

try {
ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.internal.vm.ci", false);
MojoBuildConfiguration config = new MojoBuildConfiguration();
getLog().info("WorkingDirectory: " + config.getWorkingDirectory());
getLog().info("ImageClasspath: " + classpathStr);
Expand All @@ -249,6 +251,8 @@ public void execute() throws MojoExecutionException {
NativeImage.build(config);
} catch (NativeImage.NativeImageError e) {
throw new MojoExecutionException("Error creating native image:", e);
} catch (IllegalAccessError e) {
throw new MojoExecutionException("Image building on Java 11+ without native-image requires MAVEN_OPTS='--add-exports=java.base/jdk.internal.module=ALL-UNNAMED'");
}
}
}
Expand Down

0 comments on commit 84eed7e

Please sign in to comment.