Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven plugin fixes #274

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use relative paths for argument files
Workaround for a problem with absolute paths on Windows.

Signed-off-by: Lazar Mitrović <lazar.mitrovic@oracle.com>
  • Loading branch information
lazar-mitrovic committed Aug 4, 2022
commit 150b0aaf3d00adab8519d5a8e69f97d79d267500
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -72,15 +73,24 @@ public static String nativeImageConfigureFileName() {
return "native-image-configure" + GRAALVM_EXE_EXTENSION;
}

public static List<String> convertToArgsFile(List<String> cliArgs) {
public static List<String> convertToArgsFile(List<String> cliArgs, Path outputDir) {
return convertToArgsFile(cliArgs, outputDir, Paths.get(""));
}

public static List<String> convertToArgsFile(List<String> cliArgs, Path outputDir, Path projectDir) {
try {
File tmpFile = Files.createTempFile("native-image", "args").toFile();
tmpFile.deleteOnExit();
boolean ignored = outputDir.toFile().mkdirs();
File tmpFile = Files.createTempFile(outputDir, "native-image-", ".args").toFile();
// tmpFile.deleteOnExit();
cliArgs = cliArgs.stream().map(NativeImageUtils::escapeArg).collect(Collectors.toList());
Files.write(tmpFile.toPath(), cliArgs, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
return Collections.singletonList("@" + tmpFile.getAbsolutePath());
} catch (IOException e) {

Path resultingPath = tmpFile.toPath().toAbsolutePath();
if (projectDir != null) { // We know where the project dir is, so want to use relative paths
resultingPath = projectDir.toAbsolutePath().relativize(resultingPath);
}
return Collections.singletonList("@" + resultingPath);
} catch (IOException e) {
return Collections.unmodifiableList(cliArgs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.gradle.process.CommandLineArgumentProvider;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -64,16 +66,19 @@ public class NativeImageCommandLineProvider implements CommandLineArgumentProvid
private final Provider<NativeImageOptions> options;
private final Provider<String> executableName;
private final Provider<String> outputDirectory;
private final Provider<String> workingDirectory;
private final Provider<RegularFile> classpathJar;
private final Provider<Boolean> useArgFile;

public NativeImageCommandLineProvider(Provider<NativeImageOptions> options,
Provider<String> executableName,
Provider<String> workingDirectory,
Provider<String> outputDirectory,
Provider<RegularFile> classpathJar,
Provider<Boolean> useArgFile) {
this.options = options;
this.executableName = executableName;
this.workingDirectory = workingDirectory;
this.outputDirectory = outputDirectory;
this.classpathJar = classpathJar;
this.useArgFile = useArgFile;
Expand Down Expand Up @@ -142,7 +147,8 @@ public List<String> asArguments() {
}
cliArgs.addAll(options.getBuildArgs().get());
if (useArgFile.getOrElse(true)) {
return NativeImageUtils.convertToArgsFile(cliArgs);
Path argFileDir = Paths.get(workingDirectory.get());
return NativeImageUtils.convertToArgsFile(cliArgs, argFileDir, argFileDir);
}
return Collections.unmodifiableList(cliArgs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private List<String> buildActualCommandLineArgs() {
return new NativeImageCommandLineProvider(
getOptions(),
getExecutableShortName(),
getProviders().provider(() -> getWorkingDirectory().get().getAsFile().getAbsolutePath()),
// Can't use getOutputDirectory().map(...) because Gradle would complain that we use
// a mapped value before the task was called, when we are actually calling it...
getProviders().provider(() -> getOutputDirectory().getAsFile().get().getAbsolutePath()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
}

if (useArgFile) {
return NativeImageUtils.convertToArgsFile(cliArgs);
Path tmpDir = Paths.get("target", "tmp");
return NativeImageUtils.convertToArgsFile(cliArgs, tmpDir);
}
return Collections.unmodifiableList(cliArgs);
}
Expand Down Expand Up @@ -405,7 +406,6 @@ protected void buildImage() throws MojoExecutionException {
if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
throw new MojoExecutionException("Failed creating output directory");
}
processBuilder.directory(outputDirectory);
processBuilder.inheritIO();

String commandString = String.join(" ", processBuilder.command());
Expand Down