-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
DSL extension and documentation for native image plugin #2843
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
jme3-core/src/main/java/com/jme3/util/PreserveReflection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright (c) 2009-2026 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.util; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Marks a class whose constructors, fields, and methods should be preserved for | ||
| * reflective access by ahead-of-time compilation and metadata generation tools. | ||
| */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| public @interface PreserveReflection { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # jMonkeyEngine Native Image Gradle Plugin | ||
|
|
||
| The `org.jmonkeyengine.nativeimage` plugin generates GraalVM Native Image | ||
| reachability metadata for jMonkeyEngine applications and prepares the native | ||
| runtime library layout used by native-image executables. | ||
|
|
||
| Use it together with the official GraalVM Build Tools plugin: | ||
|
|
||
| ```groovy | ||
| plugins { | ||
| id 'application' | ||
| id 'org.graalvm.buildtools.native' version '1.1.0' | ||
| id 'org.jmonkeyengine.nativeimage' | ||
| } | ||
|
|
||
| application { | ||
| mainClass = 'com.example.MyGame' | ||
| } | ||
|
|
||
| graalvmNative { | ||
| binaries { | ||
| named('main') { | ||
| imageName = 'my-game' | ||
| mainClass = 'com.example.MyGame' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| jmeNativeImage { | ||
| useDefaultResourceSettings() | ||
| } | ||
| ``` | ||
|
|
||
| Build the native executable with: | ||
|
|
||
| ```bash | ||
| ./gradlew nativeCompile | ||
| ``` | ||
|
|
||
| ## Reflection Metadata | ||
|
|
||
| The plugin automatically preserves common jME runtime types, such as | ||
| `Savable`, `AssetLoader`, `Control`, `Filter`, networking serializers, and | ||
| other engine extension points. | ||
|
|
||
| For application classes, use the `jmeNativeImage` DSL: | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| targetType 'com.example.MyNiftyController' | ||
| targetType 'com.example.MyCustomAssetLoader' | ||
| } | ||
| ``` | ||
|
|
||
| `targetType` accepts either a concrete class or a type used as a match target. | ||
| If the configured type is an interface or superclass, the generator preserves | ||
| matching classes found on the scan classpath. | ||
|
|
||
| You can also preserve all classes marked with an annotation: | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| targetAnnotation 'com.example.ReflectiveEntryPoint' | ||
| } | ||
| ``` | ||
|
|
||
| jME also provides a built-in annotation for this: | ||
|
|
||
| ```java | ||
| import com.jme3.util.PreserveReflection; | ||
|
|
||
| @PreserveReflection | ||
| public class MyNiftyController { | ||
| public void startGame() { | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Classes annotated with `@PreserveReflection` are included by default when this | ||
| plugin generates metadata. | ||
|
|
||
|
|
||
| ## Default Resource Settings | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| useDefaultResourceSettings() | ||
| } | ||
| ``` | ||
|
|
||
| adds broad default GraalVM resource settings, compatible with typical jME applications. | ||
|
|
||
| You can override those defaults through the DSL: | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| includeResources 'Interface/.*|Textures/.*|Models/.*' | ||
| excludeResources '(?i).*\\.(class|jar|dylib|so|dll|jnilib)$' | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Advanced Usage | ||
|
|
||
| The following additional options are available, but most applications | ||
| should not need them. The plugin already scans the main source set resources, | ||
| preserves common jME extension points, and supports `@PreserveReflection` for | ||
| application classes. | ||
|
|
||
| ### Resource Globs | ||
|
|
||
| A resource glob is an optional path pattern for extra files that should be | ||
| included as resources in the generated native image metadata. | ||
|
|
||
| It is not a Java class name. It is a resource path inside your application or | ||
| dependency jars, using `/` separators. | ||
|
|
||
| You usually do not need to configure resource globs for ordinary files under | ||
| the project's `src/main/resources`: the plugin scans the main source set | ||
| resources and writes metadata for them automatically. | ||
|
|
||
| Use `resourceGlob` when a resource is outside the normal main resources scan, | ||
| comes from a dependency, is generated by a separate task, or needs to be added | ||
| as a broader pattern instead of as a discovered concrete file. | ||
|
|
||
| Examples of optional extra resource globs: | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| resourceGlob 'Interface/**' | ||
| resourceGlob 'Textures/**' | ||
| resourceGlob 'Models/**/*.j3o' | ||
| resourceGlob 'com/example/external-runtime-config.properties' | ||
| } | ||
| ``` | ||
|
|
||
| ### Proxy Interfaces | ||
|
|
||
| If your application uses dynamic proxies, configure the interface set: | ||
|
|
||
| ```groovy | ||
| jmeNativeImage { | ||
| proxyInterfaceSet([ | ||
| 'com.example.Service', | ||
| 'com.example.ServiceEvents' | ||
| ]) | ||
| } | ||
| ``` | ||
|
|
||
| Each `proxyInterfaceSet` entry represents one proxy definition containing the | ||
| interfaces implemented by that proxy. |
60 changes: 60 additions & 0 deletions
60
...lugin/src/main/groovy/org/jmonkeyengine/gradle/nativeimage/JmeNativeImageExtension.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package org.jmonkeyengine.gradle.nativeimage | ||
|
|
||
| import org.gradle.api.model.ObjectFactory | ||
| import org.gradle.api.provider.ListProperty | ||
| import org.gradle.api.provider.Property | ||
|
|
||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * DSL extension for jMonkeyEngine Native Image metadata generation. | ||
| */ | ||
| class JmeNativeImageExtension { | ||
|
|
||
| final ListProperty<String> additionalTargetTypes | ||
| final ListProperty<String> additionalTargetAnnotations | ||
| final ListProperty<List<String>> additionalProxyInterfaceSets | ||
| final ListProperty<String> additionalResourceGlobs | ||
| final Property<String> includeResourcesPattern | ||
| final Property<String> excludeResourcesPattern | ||
| final Property<Boolean> defaultResourceSettings | ||
|
|
||
| @Inject | ||
| JmeNativeImageExtension(ObjectFactory objects) { | ||
| additionalTargetTypes = objects.listProperty(String).convention([]) | ||
| additionalTargetAnnotations = objects.listProperty(String).convention([]) | ||
| additionalProxyInterfaceSets = objects.listProperty(List).convention([]) | ||
| additionalResourceGlobs = objects.listProperty(String).convention([]) | ||
| includeResourcesPattern = objects.property(String) | ||
| excludeResourcesPattern = objects.property(String) | ||
| defaultResourceSettings = objects.property(Boolean).convention(false) | ||
| } | ||
|
|
||
| void targetType(String className) { | ||
| additionalTargetTypes.add(className) | ||
| } | ||
|
|
||
| void targetAnnotation(String annotationClassName) { | ||
| additionalTargetAnnotations.add(annotationClassName) | ||
| } | ||
|
|
||
| void proxyInterfaceSet(Iterable<String> interfaceClassNames) { | ||
| additionalProxyInterfaceSets.add(interfaceClassNames.toList()) | ||
| } | ||
|
|
||
| void resourceGlob(String glob) { | ||
| additionalResourceGlobs.add(glob) | ||
| } | ||
|
|
||
| void includeResources(String pattern) { | ||
| includeResourcesPattern.set(pattern) | ||
| } | ||
|
|
||
| void excludeResources(String pattern) { | ||
| excludeResourcesPattern.set(pattern) | ||
| } | ||
|
|
||
| void useDefaultResourceSettings() { | ||
| defaultResourceSettings.set(true) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method signature accepts
Iterable<String>, butArrayListdoes not have a constructor that accepts a rawIterable(it only acceptsCollection). If a user passes anIterablethat does not implementCollection(such as certain Gradle lazy providers or custom iterables), this will throw aMissingConstructorExceptionat runtime.Using Groovy's
toList()extension method onIterableis safer and more idiomatic.