Skip to content

Commit 2be079a

Browse files
committed
manage native-image configuration files in graalpy maven plugin
1 parent fa0e33e commit 2be079a

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

graalpython/graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/ManageResourcesMojo.java

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
*/
4141
package org.graalvm.python.maven.plugin;
4242

43+
import java.io.File;
44+
import java.io.FileWriter;
45+
import java.io.IOException;
46+
import java.nio.file.Files;
47+
import java.nio.file.Path;
48+
import java.nio.file.Paths;
49+
import java.nio.file.StandardOpenOption;
50+
import java.nio.file.attribute.PosixFilePermission;
51+
import java.util.*;
52+
import java.util.stream.Collectors;
53+
4354
import org.apache.maven.artifact.Artifact;
4455
import org.apache.maven.artifact.DefaultArtifact;
4556
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
@@ -54,16 +65,6 @@
5465
import org.graalvm.python.embedding.tools.exec.GraalPyRunner;
5566
import org.graalvm.python.embedding.tools.vfs.VFSUtils;
5667

57-
import java.io.File;
58-
import java.io.FileWriter;
59-
import java.io.IOException;
60-
import java.nio.file.Files;
61-
import java.nio.file.Path;
62-
import java.nio.file.Paths;
63-
import java.nio.file.StandardOpenOption;
64-
import java.nio.file.attribute.PosixFilePermission;
65-
import java.util.*;
66-
import java.util.stream.Collectors;
6768

6869
import static org.graalvm.python.embedding.tools.vfs.VFSUtils.VFS_HOME;
6970
import static org.graalvm.python.embedding.tools.vfs.VFSUtils.VFS_ROOT;
@@ -93,6 +94,18 @@ public class ManageResourcesMojo extends AbstractMojo {
9394

9495
private static final String EXCLUDE_PREFIX = "exclude:";
9596

97+
private static final String NATIVE_IMAGE_RESOURCES_CONFIG = """
98+
{
99+
"resources": {
100+
"includes": [
101+
{"pattern": "$vfs/.*"}
102+
]
103+
}
104+
}
105+
""".replace("$vfs", VFS_ROOT);
106+
107+
private static final String NATIVE_IMAGE_ARGS = "Args = -H:-CopyLanguageResources";
108+
96109
@Parameter(defaultValue = "${project}", required = true, readonly = true)
97110
MavenProject project;
98111

@@ -114,10 +127,38 @@ static Path getHomeDirectory(MavenProject project) {
114127
return Path.of(project.getBuild().getOutputDirectory(), VFS_ROOT, VFS_HOME);
115128
}
116129

130+
static Path getVenvDirectory(MavenProject project) {
131+
return Path.of(project.getBuild().getOutputDirectory(), VFS_ROOT, VFS_VENV);
132+
}
133+
134+
static Path getMetaInfDirectory(MavenProject project) {
135+
return Path.of(project.getBuild().getOutputDirectory(), "META-INF", "native-image", GRAALPY_GROUP_ID, GRAALPY_MAVEN_PLUGIN_ARTIFACT_ID);
136+
}
137+
117138
public void execute() throws MojoExecutionException {
118139
manageHome();
119140
manageVenv();
120141
listGraalPyResources();
142+
manageNativeImageConfig();
143+
}
144+
145+
private void manageNativeImageConfig() throws MojoExecutionException {
146+
Path metaInf = getMetaInfDirectory(project);
147+
// XXX remove resource-config.json and native-image.properties from archetype
148+
Path resourceConfig = metaInf.resolve("resource-config.json");
149+
try {
150+
Files.createDirectories(resourceConfig.getParent());
151+
Files.writeString(resourceConfig, NATIVE_IMAGE_RESOURCES_CONFIG, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
152+
} catch (IOException e) {
153+
throw new MojoExecutionException(String.format("failed to write %s", resourceConfig), e);
154+
}
155+
Path nativeImageProperties = metaInf.resolve("native-image.properties");
156+
try {
157+
Files.createDirectories(nativeImageProperties.getParent());
158+
Files.writeString(nativeImageProperties, NATIVE_IMAGE_ARGS, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
159+
} catch (IOException e) {
160+
throw new MojoExecutionException(String.format("failed to write %s", nativeImageProperties), e);
161+
}
121162
}
122163

123164
public static class PythonHome {
@@ -269,10 +310,6 @@ private void manageVenv() throws MojoExecutionException {
269310
}
270311
}
271312

272-
static Path getVenvDirectory(MavenProject project) {
273-
return Path.of(project.getBuild().getOutputDirectory(), VFS_ROOT, VFS_VENV);
274-
}
275-
276313
private void installWantedPackages(Path venvDirectory, List<String> installedPackages) throws MojoExecutionException {
277314
var pkgsToInstall = new HashSet<String>(packages);
278315
pkgsToInstall.removeAll(installedPackages);

0 commit comments

Comments
 (0)