|
1 | | -= Getting Started with Native Gradle Plugin |
| 1 | += End-to-end Native Gradle Plugin guide |
2 | 2 | The GraalVM team |
3 | 3 | :highlighjsdir: {gradle-relative-srcdir}/highlight |
4 | 4 |
|
5 | | -This guide shows how to get started with the <<gradle-plugin.adoc#,Gradle plugin for GraalVM Native Image>> and build a native executable for a Java application. |
| 5 | + |
| 6 | +This guide leads you through the process of integrating Native Gradle Plugin with your project. |
| 7 | +It starts from adding the plugin to your project, goes through some of the main functionalities that you may use (like collecting the metadata), |
| 8 | +all the way to some diagnostics tools that you can use to analyse native executables you have made. |
| 9 | + |
| 10 | +In case you only want to see how a simple application works in practise, you can check <<quickstart-gradle-plugin.adoc#,our demo>>. |
6 | 11 |
|
7 | 12 | == Prerequisites |
8 | 13 | [[prerequisites]] |
@@ -173,7 +178,7 @@ You can also pass **build-time** and **run-time** options to the Native Image us |
173 | 178 | - `buildArgs.add('<buildArg>')` - You can find more about possible build arguments https://www.graalvm.org/latest/reference-manual/native-image/overview/BuildConfiguration/[here] |
174 | 179 | - `runtimeArgs.add('<runtimeArg>')` - You can find more about possible runtime arguments https://www.graalvm.org/latest/reference-manual/native-image/overview/Options/[here] |
175 | 180 |
|
176 | | -Here is the example of additional options usage: |
| 181 | +Here is an example of additional options usage: |
177 | 182 |
|
178 | 183 | [source,groovy,subs="verbatim,attributes", role="multi-language-sample"] |
179 | 184 | ---- |
@@ -417,7 +422,7 @@ For example: |
417 | 422 | - `direct` mode is for **experienced users** that knows how to configure the agent manually |
418 | 423 |
|
419 | 424 | You can configure each mode (and declare the one that will be used for generating metadata) inside the `agent` block in `build.gradle` file. |
420 | | -Here is the example of the `agent` block with configured conditional and direct modes, where the conditional mode is set as default and will be used to generate the metadata: |
| 425 | +Here is an example of the `agent` block with configured conditional and direct modes, where the conditional mode is set as default and will be used to generate the metadata: |
421 | 426 |
|
422 | 427 | [source,groovy,subs="verbatim,attributes", role="multi-language-sample"] |
423 | 428 | ---- |
@@ -761,162 +766,3 @@ You can read more about build report features https://www.graalvm.org/latest/ref |
761 | 766 |
|
762 | 767 | [NOTE] |
763 | 768 | Note that Build Report features vary depending on a GraalVM version you use. |
764 | | -
|
765 | | -
|
766 | | -[[build-a-native-executable-with-resources-autodetection]] |
767 | | -== Build a Native Executable with Resources Autodetection |
768 | | -
|
769 | | -You can already build a native executable by running `./gradlew nativeCompile` or run it directly by invoking `./gradlew nativeRun`. |
770 | | -However, at this stage, running the native executable will fail because this application requires additional metadata: you need to provide it with a list of resources to load. |
771 | | -
|
772 | | -. Instruct the plugin to automatically detect resources to be included in the native executable. |
773 | | -Add this to your _build.gradle_ file: |
774 | | -+ |
775 | | -[source,groovy,subs="verbatim,attributes", role="multi-language-sample"] |
776 | | ----- |
777 | | -graalvmNative { |
778 | | - binaries.all { |
779 | | - resources.autodetect() |
780 | | - } |
781 | | -} |
782 | | ----- |
783 | | -+ |
784 | | -[source,kotlin,subs="verbatim,attributes", role="multi-language-sample"] |
785 | | ----- |
786 | | -graalvmNative { |
787 | | - binaries.all { |
788 | | - resources.autodetect() |
789 | | - } |
790 | | -} |
791 | | ----- |
792 | | -[start=2] |
793 | | -. Compile the project and build a native executable at one step: |
794 | | -+ |
795 | | -[source,shell] |
796 | | ----- |
797 | | -./gradlew nativeRun |
798 | | ----- |
799 | | -+ |
800 | | -The native executable, named _fortune_, is created in the _/fortune/build/native/nativeCompile_ directory. |
801 | | -[start=3] |
802 | | -. Run the native executable: |
803 | | -+ |
804 | | -[source,shell] |
805 | | ----- |
806 | | -./fortune/build/native/nativeCompile/fortune |
807 | | ----- |
808 | | -+ |
809 | | -The application starts and prints a random quote. |
810 | | -
|
811 | | -Configuring the `graalvmNative` plugin to automatically detect resources (`resources.autodetect()`) to be included in a binary is one way to make this example work. |
812 | | -Using `resources.autodetect()` works because the application uses resources (_fortunes.json_) which are directly available in the `src/main/resources` location. |
813 | | -
|
814 | | -In the next section, the guide shows that you can use the tracing agent to do the same. |
815 | | -
|
816 | | -[[build-a-native-executable-detecting-resources-with-the-agent]] |
817 | | -=== Build a Native Executable by Detecting Resources with the Agent |
818 | | -
|
819 | | -The Native Image Gradle plugin simplifies generation of the required metadata by injecting the |
820 | | -https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html#agent-support[ |
821 | | -tracing agent] automatically for you at compile time. |
822 | | -To enable the agent, just pass the `-Pagent` option to any Gradle tasks that extends `JavaForkOptions` (for example, `test` or `run`). |
823 | | -
|
824 | | -The following steps illustrate how to collect metadata using the agent, and then build a native executable using that metadata. |
825 | | -
|
826 | | -. To demonstrate this approach, remove the `resources.autodetect()` block from your _build.gradle_ file: |
827 | | -+ |
828 | | -[source,shell] |
829 | | ----- |
830 | | -binaries.all { |
831 | | - resources.autodetect() |
832 | | -} |
833 | | ----- |
834 | | -. Run your application with the agent enabled: |
835 | | -+ |
836 | | -[source,shell] |
837 | | ----- |
838 | | -./gradlew -Pagent run |
839 | | ----- |
840 | | -It runs your application on the JVM with the agent, collects the metadata, and generates configuration files in the _$\{buildDir}/native/agent-output/$\{taskName}_ directory. |
841 | | -. Copy the configuration files into the project's _/META-INF/native-image_ directory using the `metadataCopy` task: |
842 | | -+ |
843 | | -[source,shell] |
844 | | ----- |
845 | | -./gradlew metadataCopy --task run --dir src/main/resources/META-INF/native-image |
846 | | ----- |
847 | | -. Build a native executable using metadata acquired by the agent: |
848 | | -+ |
849 | | -[source,shell] |
850 | | ----- |
851 | | -./gradlew nativeCompile |
852 | | ----- |
853 | | -+ |
854 | | -The native executable, named _fortune_, is created in the _build/native/nativeCompile_ directory. |
855 | | -. Run the native executable: |
856 | | -+ |
857 | | -[source,shell] |
858 | | ----- |
859 | | -./fortune/build/native/nativeCompile/fortune |
860 | | ----- |
861 | | -+ |
862 | | -The application starts and prints a random quote. |
863 | | -
|
864 | | -== Add JUnit Testing |
865 | | -
|
866 | | -The Gradle plugin for GraalVM Native Image can run |
867 | | -https://junit.org/junit5/docs/current/user-guide/[JUnit Platform] tests on your native executable. |
868 | | -This means that the tests will be compiled and run as native code. |
869 | | -
|
870 | | -. Create the following test in the |
871 | | -_fortune/src/test/java/demo/FortuneTest.java_ file: |
872 | | -+ |
873 | | -.fortune/src/test/java/demo/FortuneTest.java |
874 | | -[source,java] |
875 | | ----- |
876 | | -package demo; |
877 | | - |
878 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
879 | | -import org.junit.jupiter.api.DisplayName; |
880 | | -import org.junit.jupiter.api.Test; |
881 | | - |
882 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
883 | | - |
884 | | -class FortuneTest { |
885 | | - @Test |
886 | | - @DisplayName("Returns a fortune") |
887 | | - void testItWorks() throws JsonProcessingException { |
888 | | - Fortune fortune = new Fortune(); |
889 | | - assertTrue(fortune.randomFortune().length()>0); |
890 | | - } |
891 | | -} |
892 | | ----- |
893 | | -
|
894 | | -. Run JUnit tests: |
895 | | -[source,shell] |
896 | | ----- |
897 | | -./gradlew nativeTest |
898 | | ----- |
899 | | -
|
900 | | -The plugin runs tests on the JVM prior to running tests from the native executable. |
901 | | -To disable testing support (which comes by default), add the following configuration to the _build.gradle_ file: |
902 | | -
|
903 | | -[source,groovy,subs="verbatim,attributes", role="multi-language-sample"] |
904 | | ----- |
905 | | -graalvmNative { |
906 | | - testSupport = false |
907 | | -} |
908 | | ----- |
909 | | -
|
910 | | -[source,kotlin,subs="verbatim,attributes", role="multi-language-sample"] |
911 | | ----- |
912 | | -graalvmNative { |
913 | | - testSupport.set(false) |
914 | | -} |
915 | | ----- |
916 | | -
|
917 | | -=== Summary |
918 | | -
|
919 | | -The Gradle plugin for GraalVM Native Image adds support for building and testing native executables using the https://gradle.org[Gradle]. |
920 | | -The plugin has many features, described in the |
921 | | -https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html[plugin |
922 | | -reference documentation]. |
0 commit comments