Skip to content

Commit

Permalink
Add Graal native image support
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Sep 3, 2018
1 parent 8027bd4 commit 779454b
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build/
*.iml
*.ipr
out/
samples/graal-webapp/org.springframework.fu.sample.graal.applicationkt
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ You can have a look to the sample applications:

* /~https://github.com/spring-projects/spring-fu/tree/master/samples/reactive-webapp[Reactive webapp]
* /~https://github.com/spring-projects/spring-fu/tree/master/samples/coroutine-webapp[Coroutine webapp]
* /~https://github.com/spring-projects/spring-fu/tree/master/samples/minimal-webapp[Minimal webapp]
* /~https://github.com/spring-projects/spring-fu/tree/master/samples/graal-webapp[Graal native image webapp]

=== Credits

Expand Down
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ subprojects {
dependency("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:$coroutinesVersion")
}
}
configurations.all {
exclude(module = "spring-boot-autoconfigure")
exclude(module = "javax.annotation-api")
}
}
7 changes: 1 addition & 6 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
dependencies {
api("org.springframework.boot:spring-boot-starter") {
exclude("spring-boot-autoconfigure")
exclude("javax.annotation-api")
exclude("snakeyaml")

}
api("org.springframework.boot:spring-boot-starter")
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
api("org.jetbrains.kotlin:kotlin-reflect")

Expand Down
14 changes: 12 additions & 2 deletions core/src/main/kotlin/org/springframework/fu/ApplicationDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import org.springframework.boot.SpringApplication
import org.springframework.boot.WebApplicationType
import org.springframework.boot.context.properties.bind.Bindable
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationEvent
import org.springframework.context.ApplicationListener
import org.springframework.context.support.BeanDefinitionDsl
import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.support.ReloadableResourceBundleMessageSource
import org.springframework.context.support.registerBean
import org.springframework.fu.properties.ConfigurationPropertiesBinder
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor



/**
* @author Sebastien Deleuze
Expand All @@ -37,14 +41,15 @@ open class ApplicationDsl(private val startServer: Boolean, val init: Applicatio

override fun initialize(context: GenericApplicationContext) {
this.context = context
context.registerBean(AutowiredAnnotationBeanPostProcessor::class.java)

init()
context.registerBean("messageSource") {
ReloadableResourceBundleMessageSource().apply {
setBasename("messages")
setDefaultEncoding("UTF-8")
}
}
init()
super.initialize(context)
}

Expand Down Expand Up @@ -86,7 +91,11 @@ open class ApplicationDsl(private val startServer: Boolean, val init: Applicatio
* @param profiles [ApplicationContext] profiles separated by commas.
*/
fun run(args: Array<String> = emptyArray(), profiles: String = "") {
val application = SpringApplication(Application::class.java)
val application = object: SpringApplication(Application::class.java) {
override fun load(context: ApplicationContext?, sources: Array<out Any>?) {
// We don't want the annotation bean definition reader
}
}
application.webApplicationType = if(startServer) WebApplicationType.REACTIVE else WebApplicationType.NONE
application.setApplicationContextClass(
if (startServer)
Expand All @@ -98,6 +107,7 @@ open class ApplicationDsl(private val startServer: Boolean, val init: Applicatio
application.setAdditionalProfiles(*profiles.split(",").map { it.trim() }.toTypedArray())
}
application.addInitializers(this)
application.setRegisterShutdownHook(false)
application.run(*args)
}

Expand Down
1 change: 0 additions & 1 deletion samples/coroutine-webapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ kotlin {
}

dependencies {
api("org.springframework.boot:spring-boot-starter")
api(project(":modules:webflux-jackson"))
api(project(":modules:mongodb-coroutine"))
api(project(":modules:mongodb-embedded"))
Expand Down
4 changes: 4 additions & 0 deletions samples/graal-webapp/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Graal webapp

This is a Spring Fu example of a minimal webapp compiled as a /~https://github.com/oracle/graal/tree/master/substratevm[Graal native image]. You can build it by running `build.sh` with Graal 1.0 RC5 or above.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ plugins {
}

dependencies {
api("org.springframework.boot:spring-boot-starter")
api(project(":modules:webflux"))

implementation("io.projectreactor.netty:reactor-netty")
// Remove when Graal RC6 will be released
implementation("org.aspectj:aspectjweaver")
implementation("com.jcraft:jzlib:1.1.3")

testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.springframework:spring-test")

testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

configurations.all {
exclude(module = "netty-transport-native-epoll")
exclude(module = "netty-transport-native-unix-common")
}
7 changes: 7 additions & 0 deletions samples/graal-webapp/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh

cd ../../
./gradlew clean build -x test
cd samples/graal-webapp
unzip build/libs/graal-webapp-0.0.1.BUILD-SNAPSHOT.jar -d build/libs/graal-webapp-0.0.1.BUILD-SNAPSHOT
native-image -H:ReflectionConfigurationFiles=graal.json -Dio.netty.noUnsafe=true -H:+ReportUnsupportedElementsAtRuntime -Dfile.encoding=UTF-8 -cp ".:$(echo build/libs/graal-webapp-0.0.1.BUILD-SNAPSHOT/BOOT-INF/lib/*.jar | tr ' ' ':')":build/libs/graal-webapp-0.0.1.BUILD-SNAPSHOT/BOOT-INF/classes org.springframework.fu.sample.graal.ApplicationKt
50 changes: 50 additions & 0 deletions samples/graal-webapp/graal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"name": "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.apache.logging.log4j.message.ReusableMessageFactory",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.apache.logging.log4j.message.DefaultFlowMessageFactory",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.springframework.context.support.GenericApplicationContext",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.springframework.context.annotation.ConfigurationClassPostProcessor",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "org.springframework.http.codec.support.DefaultServerCodecConfigurer",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "io.netty.channel.socket.nio.NioServerSocketChannel",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.fu.sample.minimal
package org.springframework.fu.sample.graal

import org.springframework.core.io.ClassPathResource
import org.springframework.fu.application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.fu.sample.minimal
package org.springframework.fu.sample.graal

import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
Expand Down
78 changes: 0 additions & 78 deletions samples/minimal-webapp/README.adoc

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include(
"modules:webflux-mustache",
"modules:webflux-thymeleaf",
"samples:coroutine-webapp",
"samples:minimal-webapp",
"samples:graal-webapp",
"samples:reactive-webapp"
)

Expand Down

0 comments on commit 779454b

Please sign in to comment.