40
40
*/
41
41
package org .graalvm .python .maven .plugin ;
42
42
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
+
43
54
import org .apache .maven .artifact .Artifact ;
44
55
import org .apache .maven .artifact .DefaultArtifact ;
45
56
import org .apache .maven .artifact .handler .DefaultArtifactHandler ;
54
65
import org .graalvm .python .embedding .tools .exec .GraalPyRunner ;
55
66
import org .graalvm .python .embedding .tools .vfs .VFSUtils ;
56
67
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 ;
67
68
68
69
import static org .graalvm .python .embedding .tools .vfs .VFSUtils .VFS_HOME ;
69
70
import static org .graalvm .python .embedding .tools .vfs .VFSUtils .VFS_ROOT ;
@@ -93,6 +94,18 @@ public class ManageResourcesMojo extends AbstractMojo {
93
94
94
95
private static final String EXCLUDE_PREFIX = "exclude:" ;
95
96
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
+
96
109
@ Parameter (defaultValue = "${project}" , required = true , readonly = true )
97
110
MavenProject project ;
98
111
@@ -114,10 +127,38 @@ static Path getHomeDirectory(MavenProject project) {
114
127
return Path .of (project .getBuild ().getOutputDirectory (), VFS_ROOT , VFS_HOME );
115
128
}
116
129
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
+
117
138
public void execute () throws MojoExecutionException {
118
139
manageHome ();
119
140
manageVenv ();
120
141
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
+ }
121
162
}
122
163
123
164
public static class PythonHome {
@@ -269,10 +310,6 @@ private void manageVenv() throws MojoExecutionException {
269
310
}
270
311
}
271
312
272
- static Path getVenvDirectory (MavenProject project ) {
273
- return Path .of (project .getBuild ().getOutputDirectory (), VFS_ROOT , VFS_VENV );
274
- }
275
-
276
313
private void installWantedPackages (Path venvDirectory , List <String > installedPackages ) throws MojoExecutionException {
277
314
var pkgsToInstall = new HashSet <String >(packages );
278
315
pkgsToInstall .removeAll (installedPackages );
0 commit comments