Integrating SpringBoot with Gradle
Springboot support is provided by most of the build tools e.g. Gradle and Maven. Though grails 3.x comes with Springboot and gradle integration, there might come scenarios where you just want to create a very small and simple build that does specific task e.g. a build that will just run some threads and trigger some rabbitmq messages. Now here we just need service layer and no view or domain layer. To achieve this using spring and springboot together with Gradle, we can follow steps below:
1. Create a gradle app which could be created using gradle init
withing the root of your project directory after you install gradle on your machine. Typical build.gradle created by default would be as below:
[code lang=”groovy”]
/*
* This build file was auto generated by running the Gradle ‘init’ task
* by ‘vinayprajapati’ at ’27/7/15 5:07 PM’ with Gradle 2.3
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at http://gradle.org/docs/2.3/userguide/tutorial_java_projects.html
*/
/*
// Apply the java plugin to add support for Java
apply plugin: ‘java’
// In this section you declare where to find the dependencies of your project
repositories {
// Use ‘jcenter’ for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile ‘org.slf4j:slf4j-api:1.7.7’
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile ‘org.testng:testng:6.8.1’ and add
// ‘test.useTestNG()’ to your build script.
testCompile "junit:junit:4.11"
}
*/
[/code]
if you read the above build.gradle, it has come with basic java, logging and testing libs under comment /**/.
2. Now decide whether your project would be a java or groovy project as by default you can’t use both together(grails has added this feature though). Say you are building a groovy project.
[code lang=”groovy”]
apply plugin: ‘groovy’
apply plugin: ‘idea’//optional, used to provide idea support for gradle project
apply plugin: ‘spring-boot’
buildscript {
repositories { mavenCentral()}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE")
classpath ‘org.springframework:springloaded:1.2.0.RELEASE’
}
}
repositories { mavenCentral() }
dependencies {
compile ‘org.codehaus.groovy:groovy-all’
compile ‘org.springframework.boot:spring-boot-starter-web’
//all the other dependencies go here e.g. uncomment below
// compile("org.springframework.boot:spring-boot-starter-jetty")
}
[/code]
3. Now, you will get a task bootRun
from spring-boot plugin. Use this to run your project. The only thing you are left with is to create the project structure and a main file.Below is the example project structure.
[code lang=”bash”]
├── build.gradle
├── src
│ └── main
│ ├── groovy
│ │ └── intellimeet
│ │ ├── ExampleController.groovy
│ ├── java
│ └── resources
[/code]
4. Now create an entry point for your application(ExampleController in last step). Below is example file:
[code lang=”groovy”]
package intellimeet
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.ApplicationListener
import org.springframework.context.annotation.Configuration
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@EnableAutoConfiguration
@Configuration
public class ExampleController {
@RequestMapping("/")
String home() {
return "Hello World!SpringBoot runs spring app so quickly";
}
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(ExampleController.class)
app.run(args)
}
}
[/code]
5. We are done, Now go to root of your application and run command below:
[code lang=”groovy”]
gradle bootRun
[/code]
6. Last but not least it will create a buld dir in root which contains compiled code and if you want to create a executable spring application jar file add code below to your build.gradle
[code lang=”groovy”]
jar {
baseName = ‘gs-spring-boot’
version = ‘0.1.0’
}
[/code]
which specifies your jar base name and version(basename+version.jar would be typical file)
or
if you want to create a war add code below to your build.gradle
[code lang=”groovy”]
apply plugin: ‘war’
war {
baseName = ‘myapp’
version = ‘0.5.0’
}
[/code]
Now, to create jar or war or both run task below:
[code]
gradle build
[/code]
Or specifically for creating jar
[code]
gradle jar
[/code]
or for creating war
[code]
gradle war
[/code]
Note : It will create jar and war under build/libs and tar ball compression and zip compression under build/distributions. build directory is accessible from root.
Pingback: Diario Grails (Settimana 31 del 2015) | BME