Skip to content

Commit

Permalink
Fix review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lazar-mitrovic committed Jun 9, 2022
1 parent ac68bce commit 1f2d92f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion docs/src/docs/asciidoc/index.adoc
Expand Up @@ -28,8 +28,9 @@ If you are interested in contributing, please refer to our https://github.com/gr
* Added `quickBuild` configuration option.

==== Maven plugin
* Added support for GraalVM Reachability Metadata Repository.
* Completely reworked Maven plugin (should fix many of previous issues and inconsistencies between main and test builds).
* Added `classesDirectory`, `debug`, `fallback`, `verbose`, `sharedLibrary`, `configurationFileDirectories`, `excludeConfig`, `quickBuild`, and `jvmArgs` properties in order to match those present in the Gradle plugin. +
* Added `classesDirectory`, `debug`, `fallback`, `verbose`, `sharedLibrary`, `configurationFileDirectories`, `excludeConfig`, `quickBuild`, and `jvmArgs` properties in order to match those present in the Gradle plugin.
+
See <<maven-plugin.adoc#,docs>> for more information.
* `useArgFile` is now set to true by default only on Windows.
Expand Down
Expand Up @@ -42,6 +42,7 @@
package org.graalvm.buildtools;

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.logging.Logger;
import org.graalvm.buildtools.utils.SharedConstants;

import java.io.File;
Expand All @@ -59,10 +60,9 @@
public abstract class Utils implements SharedConstants {
public static final String NATIVE_TESTS_EXE = "native-tests" + EXECUTABLE_EXTENSION;
public static final String MAVEN_GROUP_ID = "org.graalvm.buildtools";

public static Path nativeImageCache;

public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failFast) throws MojoExecutionException {
public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failFast, Logger logger) throws MojoExecutionException {
String graalHome = System.getenv(javaHomeVariable);
if (graalHome == null) {
return null;
Expand Down Expand Up @@ -98,7 +98,7 @@ public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failF
return null;
}
}
System.out.println("Found GraalVM installation from " + javaHomeVariable + " variable.");
logger.info("Found GraalVM installation from " + javaHomeVariable + " variable.");
return graalExe;
}

Expand All @@ -110,21 +110,21 @@ public static Path getNativeImageFromPath() {
return exePath.map(path -> path.resolve(NATIVE_IMAGE_EXE)).orElse(null);
}

public static Path getNativeImage() throws MojoExecutionException {
public static Path getNativeImage(Logger logger) throws MojoExecutionException {
if (nativeImageCache != null) {
return nativeImageCache;
}

Path nativeImage = getJavaHomeNativeImage("GRAALVM_HOME", false);
Path nativeImage = getJavaHomeNativeImage("GRAALVM_HOME", false, logger);

if (nativeImage == null) {
nativeImage = getJavaHomeNativeImage("JAVA_HOME", true);
nativeImage = getJavaHomeNativeImage("JAVA_HOME", true, logger);
}

if (nativeImage == null) {
nativeImage = getNativeImageFromPath();
if (nativeImage != null) {
System.out.println("Found GraalVM installation from PATH variable.");
logger.info("Found GraalVM installation from PATH variable.");
}
}

Expand Down
Expand Up @@ -88,7 +88,6 @@
*/

public abstract class AbstractNativeMojo extends AbstractMojo {

protected static final String NATIVE_IMAGE_META_INF = "META-INF/native-image";
protected static final String NATIVE_IMAGE_PROPERTIES_FILENAME = "native-image.properties";
protected static final String NATIVE_IMAGE_DRY_RUN = "nativeDryRun";
Expand Down Expand Up @@ -219,6 +218,14 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
if (sharedLibrary) {
cliArgs.add("--shared");
}

// Let's allow user to specify environment option to toggle quick build.
String quickBuildEnv = System.getenv("GRAALVM_QUICK_BUILD");
if (quickBuildEnv != null) {
logger.info("Quick build environment variable is set.");
quickBuild = quickBuildEnv.isEmpty() || Boolean.parseBoolean(quickBuildEnv);
}

if (quickBuild) {
cliArgs.add("-Ob");
}
Expand Down Expand Up @@ -375,7 +382,7 @@ protected String getClasspath() throws MojoExecutionException {
}

protected void buildImage() throws MojoExecutionException {
Path nativeImageExecutable = Utils.getNativeImage();
Path nativeImageExecutable = Utils.getNativeImage(logger);

try {
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageExecutable.toString());
Expand Down
Expand Up @@ -42,10 +42,12 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.FileUtils;
import org.graalvm.buildtools.Utils;
import org.graalvm.buildtools.utils.NativeImageUtils;
Expand Down Expand Up @@ -73,12 +75,16 @@ public class MergeAgentFilesMojo extends AbstractMojo {
@Parameter(property = "native.agent.merge.context", required = true)
protected String context;

@Component
protected Logger logger;


@Override
public void execute() throws MojoExecutionException {
String agentOutputDirectory = agentOutputDirectoryFor(target, NativeExtension.Context.valueOf(context));
File baseDir = new File(agentOutputDirectory);
if (baseDir.exists()) {
Path nativeImageExecutable = Utils.getNativeImage();
Path nativeImageExecutable = Utils.getNativeImage(logger);
File mergerExecutable = tryInstall(nativeImageExecutable);
List<File> sessionDirectories = sessionDirectoriesFrom(baseDir.listFiles()).collect(Collectors.toList());
invokeMerge(mergerExecutable, sessionDirectories, baseDir);
Expand Down
Expand Up @@ -42,14 +42,15 @@
package org.graalvm.buildtools.maven;

import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.graalvm.buildtools.Utils;
import org.graalvm.buildtools.utils.SharedConstants;
Expand All @@ -66,12 +67,19 @@
* the JUnit Platform test listener and registering the native dependency transparently.
*/
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "native-build-tools")
public class NativeExtension extends AbstractMavenLifecycleParticipant {
public class NativeExtension extends AbstractMavenLifecycleParticipant implements LogEnabled {

private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED = "junit.platform.listeners.uid.tracking.enabled";
private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR = "junit.platform.listeners.uid.tracking.output.dir";
private static final String NATIVEIMAGE_IMAGECODE = "org.graalvm.nativeimage.imagecode";

private Logger logger;

@Override
public void enableLogging(Logger logger) {
this.logger = logger;
}

/**
* Enumeration of execution contexts.
* <p>Enum constants are intentionally lowercase for use as directory names
Expand Down Expand Up @@ -106,7 +114,7 @@ static String agentOutputDirectoryFor(String baseDir, Context context) {
}

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
public void afterProjectsRead(MavenSession session) {
for (MavenProject project : session.getProjects()) {
Build build = project.getBuild();
withPlugin(build, "native-maven-plugin", nativePlugin -> {
Expand Down Expand Up @@ -153,7 +161,7 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
}
Xpp3Dom executable = findOrAppend(config, "executable");
try {
executable.setValue(Utils.getNativeImage().getParent().resolve("java").toString());
executable.setValue(Utils.getNativeImage(logger).getParent().resolve("java").toString());
} catch (MojoExecutionException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 1f2d92f

Please sign in to comment.