mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-01 23:09:01 +00:00
64 lines
2.8 KiB
Plaintext
64 lines
2.8 KiB
Plaintext
[[deployment.efficient]]
|
|
== Efficient deployments
|
|
|
|
[[deployment.efficient.unpacking]]
|
|
=== Unpacking the Executable JAR
|
|
If you are running your application from a container, you can use an executable jar, but it is also often an advantage to explode it and run it in a different way.
|
|
Certain PaaS implementations may also choose to unpack archives before they run.
|
|
For example, Cloud Foundry operates this way.
|
|
One way to run an unpacked archive is by starting the appropriate launcher, as follows:
|
|
|
|
[source,shell,indent=0,subs="verbatim"]
|
|
----
|
|
$ jar -xf myapp.jar
|
|
$ java org.springframework.boot.loader.JarLauncher
|
|
----
|
|
|
|
This is actually slightly faster on startup (depending on the size of the jar) than running from an unexploded archive.
|
|
After startup, you should not expect any differences.
|
|
|
|
Once you have unpacked the jar file, you can also get an extra boost to startup time by running the app with its "natural" main method instead of the `JarLauncher`. For example:
|
|
|
|
[source,shell,indent=0,subs="verbatim"]
|
|
----
|
|
$ jar -xf myapp.jar
|
|
$ java -cp "BOOT-INF/classes:BOOT-INF/lib/*" com.example.MyApplication
|
|
----
|
|
|
|
NOTE: Using the `JarLauncher` over the application's main method has the added benefit of a predictable classpath order.
|
|
The jar contains a `classpath.idx` file which is used by the `JarLauncher` when constructing the classpath.
|
|
|
|
[[deployment.efficient.aot]]
|
|
=== Using Ahead-of-time Processing With the JVM
|
|
|
|
It's beneficial for the startup time to run your application using the AOT generated initialization code.
|
|
First, you need to ensure that the jar you are building includes AOT generated code.
|
|
|
|
For Maven, this means that you should build with `-Pnative` to activate the `native` profile:
|
|
|
|
[source,shell,indent=0,subs="verbatim"]
|
|
----
|
|
$ mvn -Pnative package
|
|
----
|
|
|
|
For Gradle, you need to ensure that your build includes the `org.springframework.boot.aot` plugin.
|
|
|
|
When the JAR has been built, run it with `spring.aot.enabled` system property set to `true`. For example:
|
|
|
|
[source,shell,indent=0,subs="verbatim"]
|
|
----
|
|
$ java -Dspring.aot.enabled=true -jar myapplication.jar
|
|
|
|
........ Starting AOT-processed MyApplication ...
|
|
----
|
|
|
|
Beware that using the ahead-of-time processing has drawbacks.
|
|
It implies the following restrictions:
|
|
|
|
* The classpath is fixed and fully defined at build time
|
|
* The beans defined in your application cannot change at runtime, meaning:
|
|
- The Spring `@Profile` annotation and profile-specific configuration is not supported
|
|
- Properties that change if a bean is created are not supported (for example, `@ConditionalOnProperty` and `.enable` properties).
|
|
|
|
To learn more about ahead-of-time processing, please see the <<native-image#native-image.introducing-graalvm-native-images.understanding-aot-processing,Understanding Spring Ahead-of-Time Processing section>>.
|