Skip to content

Commit 07965b0

Browse files
authored
Merge pull request #586 from graalvm/dnestoro/RefactorInitializationOfConfigurtaionFileMerger
Refactor Initialization of Configuration File Merger
2 parents 096ea1f + 8be17a9 commit 07965b0

File tree

5 files changed

+26
-41
lines changed

5 files changed

+26
-41
lines changed

docs/src/docs/asciidoc/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ If you are using alternative build systems, see <<alternative-build-systems.adoc
2121

2222
=== Release 0.10.3
2323

24+
- Remove usage of macro from merger tool initialization and throw better error if executable does not exist
25+
2426
==== Gradle plugin
2527

2628
- Add retries when downloading the metadata repository when using a URL directly

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/MergeAgentFilesMojo.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@
4848
import org.codehaus.plexus.util.FileUtils;
4949
import org.graalvm.buildtools.maven.config.AbstractMergeAgentFilesMojo;
5050
import org.graalvm.buildtools.maven.config.agent.AgentConfiguration;
51-
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
5251

5352
import java.io.File;
5453
import java.io.IOException;
55-
import java.nio.file.Path;
5654
import java.util.Arrays;
5755
import java.util.HashSet;
5856
import java.util.List;
@@ -116,14 +114,12 @@ public void execute() throws MojoExecutionException {
116114
private void mergeForGivenDir(String agentOutputDirectory) throws MojoExecutionException {
117115
File baseDir = new File(agentOutputDirectory);
118116
if (baseDir.exists()) {
119-
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
120-
tryInstallMergeExecutable(nativeImageExecutable);
121117
List<File> sessionDirectories = sessionDirectoriesFrom(baseDir.listFiles()).collect(Collectors.toList());
122118
if (sessionDirectories.size() == 0) {
123119
sessionDirectories = Collections.singletonList(baseDir);
124120
}
125121

126-
invokeMerge(mergerExecutable, sessionDirectories, baseDir);
122+
invokeMerge(sessionDirectories, baseDir);
127123
} else {
128124
getLog().debug("Agent output directory " + baseDir + " doesn't exist. Skipping merge.");
129125
}
@@ -135,11 +131,8 @@ private static Stream<File> sessionDirectoriesFrom(File[] files) {
135131
.filter(f -> f.getName().startsWith("session-"));
136132
}
137133

138-
private void invokeMerge(File mergerExecutable, List<File> inputDirectories, File outputDirectory) throws MojoExecutionException {
139-
if (!mergerExecutable.exists()) {
140-
getLog().warn("Cannot merge agent files because native-image-configure is not installed. Please upgrade to a newer version of GraalVM.");
141-
return;
142-
}
134+
private void invokeMerge(List<File> inputDirectories, File outputDirectory) throws MojoExecutionException {
135+
File mergerExecutable = getMergerExecutable();
143136
try {
144137
if (inputDirectories.isEmpty()) {
145138
getLog().warn("Skipping merging of agent files since there are no input directories.");

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/MetadataCopyMojo.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@
5050
import org.graalvm.buildtools.maven.config.AbstractMergeAgentFilesMojo;
5151
import org.graalvm.buildtools.maven.config.agent.AgentConfiguration;
5252
import org.graalvm.buildtools.maven.config.agent.MetadataCopyConfiguration;
53-
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
5453

5554
import java.io.File;
5655
import java.io.IOException;
5756
import java.nio.file.Files;
58-
import java.nio.file.Path;
5957
import java.nio.file.Paths;
6058
import java.util.Arrays;
6159
import java.util.Collections;
@@ -113,8 +111,6 @@ public void execute() throws MojoExecutionException {
113111
}
114112
}
115113

116-
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
117-
tryInstallMergeExecutable(nativeImageExecutable);
118114
executeCopy(buildDirectory, destinationDir);
119115
getLog().info("Metadata copy process finished.");
120116
}
@@ -155,7 +151,7 @@ private void executeCopy(String buildDirectory, String destinationDir) throws Mo
155151
logger.info("Copying files from: " + sourceDirsInfo);
156152

157153
List<String> nativeImageConfigureOptions = new StandardAgentMode().getNativeImageConfigureOptions(sourceDirectories, Collections.singletonList(destinationDir));
158-
nativeImageConfigureOptions.add(0, mergerExecutable.getAbsolutePath());
154+
nativeImageConfigureOptions.add(0, getMergerExecutable().getAbsolutePath());
159155
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageConfigureOptions);
160156

161157
try {

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/config/AbstractMergeAgentFilesMojo.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
package org.graalvm.buildtools.maven.config;
4343

4444
import org.apache.maven.plugin.AbstractMojo;
45+
import org.apache.maven.plugin.MojoExecutionException;
4546
import org.apache.maven.plugins.annotations.Component;
4647
import org.codehaus.plexus.logging.Logger;
47-
import org.graalvm.buildtools.utils.NativeImageUtils;
48+
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
4849

4950
import java.io.File;
50-
import java.io.IOException;
5151
import java.nio.file.Path;
5252

5353
import static org.graalvm.buildtools.utils.NativeImageUtils.nativeImageConfigureFileName;
@@ -58,32 +58,26 @@ public abstract class AbstractMergeAgentFilesMojo extends AbstractMojo {
5858
@Component
5959
protected Logger logger;
6060

61-
protected File mergerExecutable;
61+
private File mergerExecutable;
6262

63-
protected void tryInstallMergeExecutable(Path nativeImageExecutablePath) {
64-
if (mergerExecutable != null && mergerExecutable.exists()) {
65-
return;
63+
public File getMergerExecutable() throws MojoExecutionException {
64+
if (mergerExecutable == null) {
65+
initializeMergerExecutable();
6666
}
6767

68-
File nativeImageExecutable = nativeImageExecutablePath.toAbsolutePath().toFile();
69-
File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName());
70-
if (!mergerExecutable.exists()) {
71-
getLog().info("Installing native image merger to " + mergerExecutable);
72-
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageExecutable.toString());
73-
processBuilder.command().add("--macro:native-image-configure-launcher");
74-
processBuilder.directory(mergerExecutable.getParentFile());
75-
processBuilder.inheritIO();
76-
77-
try {
78-
Process installProcess = processBuilder.start();
79-
if (installProcess.waitFor() != 0) {
80-
getLog().warn("Installation of native image merging tool failed");
81-
}
82-
NativeImageUtils.maybeCreateConfigureUtilSymlink(mergerExecutable, nativeImageExecutablePath);
83-
} catch (IOException | InterruptedException e) {
84-
// ignore since we will handle that if the installer doesn't exist later
85-
}
68+
return mergerExecutable;
69+
}
8670

71+
private void initializeMergerExecutable() throws MojoExecutionException {
72+
Path nativeImage = NativeImageConfigurationUtils.getNativeImage(logger);
73+
File nativeImageExecutable = nativeImage.toAbsolutePath().toFile();
74+
String nativeImageConfigureFileName = nativeImageConfigureFileName();
75+
File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName);
76+
if (!mergerExecutable.exists()) {
77+
throw new MojoExecutionException("The '" + nativeImageConfigureFileName + "' tool was not found in the GraalVM JDK at '" + nativeImageExecutable.getParentFile().getParentFile() + "'." +
78+
"This probably means that you are using a GraalVM distribution that is not fully supported by the Native Build Tools. " +
79+
"Please try again, for example, with Oracle GraalVM or GraalVM Community Edition."
80+
);
8781
}
8882

8983
this.mergerExecutable = mergerExecutable;

native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public static Path getNativeImage(Logger logger) throws MojoExecutionException {
128128
}
129129

130130
if (nativeImage == null) {
131-
throw new RuntimeException("GraalVM native-image is missing on your system. " + System.lineSeparator() +
132-
"Make sure that GRAALVM_HOME environment variable is present.");
131+
throw new RuntimeException("The 'native-image' tool was not found on your system. " +
132+
"Make sure that the JAVA_HOME or GRAALVM_HOME environment variables point to a GraalVM JDK, or that 'native-image' is on the system path.");
133133
}
134134

135135
nativeImageExeCache = nativeImage;

0 commit comments

Comments
 (0)