|
| 1 | +# jMonkeyEngine Native Image Gradle Plugin |
| 2 | + |
| 3 | +The `org.jmonkeyengine.nativeimage` plugin generates GraalVM Native Image |
| 4 | +reachability metadata for jMonkeyEngine applications and prepares the native |
| 5 | +runtime library layout used by native-image executables. |
| 6 | + |
| 7 | +Use it together with the official GraalVM Build Tools plugin: |
| 8 | + |
| 9 | +```groovy |
| 10 | +plugins { |
| 11 | + id 'application' |
| 12 | + id 'org.graalvm.buildtools.native' version '1.1.0' |
| 13 | + id 'org.jmonkeyengine.nativeimage' |
| 14 | +} |
| 15 | +
|
| 16 | +application { |
| 17 | + mainClass = 'com.example.MyGame' |
| 18 | +} |
| 19 | +
|
| 20 | +graalvmNative { |
| 21 | + binaries { |
| 22 | + named('main') { |
| 23 | + imageName = 'my-game' |
| 24 | + mainClass = 'com.example.MyGame' |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +
|
| 29 | +jmeNativeImage { |
| 30 | + useDefaultResourceSettings() |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +Build the native executable with: |
| 35 | + |
| 36 | +```bash |
| 37 | +./gradlew nativeCompile |
| 38 | +``` |
| 39 | + |
| 40 | +## Reflection Metadata |
| 41 | + |
| 42 | +The plugin automatically preserves common jME runtime types, such as |
| 43 | +`Savable`, `AssetLoader`, `Control`, `Filter`, networking serializers, and |
| 44 | +other engine extension points. |
| 45 | + |
| 46 | +For application classes, use the `jmeNativeImage` DSL: |
| 47 | + |
| 48 | +```groovy |
| 49 | +jmeNativeImage { |
| 50 | + targetType 'com.example.MyNiftyController' |
| 51 | + targetType 'com.example.MyCustomAssetLoader' |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +`targetType` accepts either a concrete class or a type used as a match target. |
| 56 | +If the configured type is an interface or superclass, the generator preserves |
| 57 | +matching classes found on the scan classpath. |
| 58 | + |
| 59 | +You can also preserve all classes marked with an annotation: |
| 60 | + |
| 61 | +```groovy |
| 62 | +jmeNativeImage { |
| 63 | + targetAnnotation 'com.example.ReflectiveEntryPoint' |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +jME also provides a built-in annotation for this: |
| 68 | + |
| 69 | +```java |
| 70 | +import com.jme3.util.PreserveReflection; |
| 71 | + |
| 72 | +@PreserveReflection |
| 73 | +public class MyNiftyController { |
| 74 | + public void startGame() { |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Classes annotated with `@PreserveReflection` are included by default when this |
| 80 | +plugin generates metadata. |
| 81 | + |
| 82 | + |
| 83 | +## Default Resource Settings |
| 84 | + |
| 85 | +```groovy |
| 86 | +jmeNativeImage { |
| 87 | + useDefaultResourceSettings() |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +adds broad default GraalVM resource settings: |
| 92 | + |
| 93 | +```text |
| 94 | +-H:IncludeResources=.* |
| 95 | +-H:ExcludeResources=(?i).*\.(class|jar|dylib|so|dll|jnilib)$ |
| 96 | +``` |
| 97 | + |
| 98 | +This includes ordinary resources while avoiding class files, jars, and native |
| 99 | +libraries as embedded resources. |
| 100 | + |
| 101 | +You can override those defaults through the DSL: |
| 102 | + |
| 103 | +```groovy |
| 104 | +jmeNativeImage { |
| 105 | + includeResources 'Interface/.*|Textures/.*|Models/.*' |
| 106 | + excludeResources '(?i).*\\.(class|jar|dylib|so|dll|jnilib)$' |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The generated metadata is written under: |
| 111 | + |
| 112 | +```text |
| 113 | +build/generated/native-image-metadata/resources/META-INF/native-image/<group>/<project>/ |
| 114 | +``` |
| 115 | + |
| 116 | +## Advanced Usage |
| 117 | + |
| 118 | +The following options are available for unusual cases, but most applications |
| 119 | +should not need them. The plugin already scans the main source set resources, |
| 120 | +preserves common jME extension points, and supports `@PreserveReflection` for |
| 121 | +application classes. |
| 122 | + |
| 123 | +### Resource Globs |
| 124 | + |
| 125 | +A resource glob is an optional path pattern for extra files that should be |
| 126 | +included as resources in the generated native image metadata. |
| 127 | + |
| 128 | +It is not a Java class name. It is a resource path inside your application or |
| 129 | +dependency jars, using `/` separators. |
| 130 | + |
| 131 | +You usually do not need to configure resource globs for ordinary files under |
| 132 | +the project's `src/main/resources`: the plugin scans the main source set |
| 133 | +resources and writes metadata for them automatically. |
| 134 | + |
| 135 | +Use `resourceGlob` when a resource is outside the normal main resources scan, |
| 136 | +comes from a dependency, is generated by a separate task, or needs to be added |
| 137 | +as a broader pattern instead of as a discovered concrete file. |
| 138 | + |
| 139 | +Examples of optional extra resource globs: |
| 140 | + |
| 141 | +```groovy |
| 142 | +jmeNativeImage { |
| 143 | + resourceGlob 'Interface/**' |
| 144 | + resourceGlob 'Textures/**' |
| 145 | + resourceGlob 'Models/**/*.j3o' |
| 146 | + resourceGlob 'com/example/external-runtime-config.properties' |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +Typical jME resources are asset files such as `.j3o`, `.j3m`, `.j3md`, `.png`, |
| 151 | +`.jpg`, `.xml`, `.fnt`, `.frag`, `.vert`, and other files loaded through the |
| 152 | +asset manager. |
| 153 | + |
| 154 | +### Proxy Interfaces |
| 155 | + |
| 156 | +If your application uses dynamic proxies, configure the interface set: |
| 157 | + |
| 158 | +```groovy |
| 159 | +jmeNativeImage { |
| 160 | + proxyInterfaceSet([ |
| 161 | + 'com.example.Service', |
| 162 | + 'com.example.ServiceEvents' |
| 163 | + ]) |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +Each `proxyInterfaceSet` entry represents one proxy definition containing the |
| 168 | +interfaces implemented by that proxy. |
0 commit comments