{"id":22505,"date":"2015-07-29T16:13:09","date_gmt":"2015-07-29T10:43:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22505"},"modified":"2022-01-11T13:50:32","modified_gmt":"2022-01-11T08:20:32","slug":"integrating-springboot-with-gradle","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-springboot-with-gradle\/","title":{"rendered":"Integrating SpringBoot with Gradle"},"content":{"rendered":"<p>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 <a href=\"http:\/\/www.tothenew.com\/blog\/sending-scheduleddelayed-messages-with-rabbitmq-through-java-client\/\">trigger some rabbitmq messages<\/a>. 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:<\/p>\n<p>1. Create a gradle app which could be created using <code>gradle init<\/code> withing the root of your project directory after you <a title=\"Gradle installation in Ubuntu\" href=\"http:\/\/www.tothenew.com\/blog\/gradle-installation-in-ubuntu\/\">install gradle on your machine<\/a>. Typical build.gradle created by default would be as below:<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\n\/*<br \/>\n * This build file was auto generated by running the Gradle &#8216;init&#8217; task<br \/>\n * by &#8216;vinayprajapati&#8217; at &#8217;27\/7\/15 5:07 PM&#8217; with Gradle 2.3<br \/>\n *<br \/>\n * This generated file contains a commented-out sample Java project to get you started.<br \/>\n * For more details take a look at the Java Quickstart chapter in the Gradle<br \/>\n * user guide available at http:\/\/gradle.org\/docs\/2.3\/userguide\/tutorial_java_projects.html<br \/>\n *\/<\/p>\n<p>\/*<br \/>\n\/\/ Apply the java plugin to add support for Java<br \/>\napply plugin: &#8216;java&#8217;<\/p>\n<p>\/\/ In this section you declare where to find the dependencies of your project<br \/>\nrepositories {<br \/>\n    \/\/ Use &#8216;jcenter&#8217; for resolving your dependencies.<br \/>\n    \/\/ You can declare any Maven\/Ivy\/file repository here.<br \/>\n    jcenter()<br \/>\n}<\/p>\n<p>\/\/ In this section you declare the dependencies for your production and test code<br \/>\ndependencies {<br \/>\n    \/\/ The production code uses the SLF4J logging API at compile time<br \/>\n    compile &#8216;org.slf4j:slf4j-api:1.7.7&#8217;<\/p>\n<p>    \/\/ Declare the dependency for your favourite test framework you want to use in your tests.<br \/>\n    \/\/ TestNG is also supported by the Gradle Test task. Just change the<br \/>\n    \/\/ testCompile dependency to testCompile &#8216;org.testng:testng:6.8.1&#8217; and add<br \/>\n    \/\/ &#8216;test.useTestNG()&#8217; to your build script.<br \/>\n    testCompile &quot;junit:junit:4.11&quot;<br \/>\n}<br \/>\n*\/<br \/>\n[\/code]<\/p>\n<p>if you read the above build.gradle, it has come with basic java, logging and testing libs under comment \/**\/.<\/p>\n<p>2. Now decide whether your project would be a java or groovy project as by default you can&#8217;t use both together(grails has added this feature though). Say you are building a groovy project.<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\napply plugin: &#8216;groovy&#8217;<br \/>\napply plugin: &#8216;idea&#8217;\/\/optional, used to provide idea support for gradle project<br \/>\napply plugin: &#8216;spring-boot&#8217;<\/p>\n<p>buildscript {<br \/>\n    repositories { mavenCentral()}<br \/>\n    dependencies {<br \/>\n        classpath(&quot;org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE&quot;)<br \/>\n        classpath &#8216;org.springframework:springloaded:1.2.0.RELEASE&#8217;<\/p>\n<p>    }<br \/>\n}<\/p>\n<p>repositories { mavenCentral() }<\/p>\n<p>dependencies {<br \/>\n    compile &#8216;org.codehaus.groovy:groovy-all&#8217;<br \/>\n    compile &#8216;org.springframework.boot:spring-boot-starter-web&#8217;<br \/>\n\/\/all the other dependencies go here e.g. uncomment below<br \/>\n\/\/ compile(&quot;org.springframework.boot:spring-boot-starter-jetty&quot;)<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>3. Now, you will get a task <code>bootRun<\/code> 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.<\/p>\n<p>[code lang=&#8221;bash&#8221;]<\/p>\n<p>\u251c\u2500\u2500 build.gradle<br \/>\n\u251c\u2500\u2500 src<br \/>\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 main<br \/>\n\u2502\u00a0\u00a0     \u251c\u2500\u2500 groovy<br \/>\n\u2502\u00a0\u00a0     \u2502\u00a0\u00a0 \u2514\u2500\u2500 intellimeet<br \/>\n\u2502\u00a0\u00a0     \u2502\u00a0\u00a0     \u251c\u2500\u2500 ExampleController.groovy<br \/>\n\u2502\u00a0\u00a0     \u251c\u2500\u2500 java<br \/>\n\u2502\u00a0\u00a0     \u2514\u2500\u2500 resources<br \/>\n[\/code]<\/p>\n<p>4. Now create an entry point for your application(ExampleController in last step). Below is example file:<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\npackage intellimeet<\/p>\n<p>import org.springframework.boot.SpringApplication<br \/>\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration<br \/>\nimport org.springframework.context.ApplicationListener<br \/>\nimport org.springframework.context.annotation.Configuration<br \/>\nimport org.springframework.web.bind.annotation.RequestMapping<br \/>\nimport org.springframework.web.bind.annotation.RestController<\/p>\n<p>@RestController<br \/>\n@EnableAutoConfiguration<br \/>\n@Configuration<br \/>\npublic class ExampleController {<\/p>\n<p>    @RequestMapping(&quot;\/&quot;)<br \/>\n    String home() {<br \/>\n        return &quot;Hello World!SpringBoot runs spring app so quickly&quot;;<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws Exception {<br \/>\n        SpringApplication app = new SpringApplication(ExampleController.class)<br \/>\n        app.run(args)<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>5. We are done, Now go to root of your application and run command below:<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\ngradle bootRun<br \/>\n[\/code]<\/p>\n<p>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<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\njar {<br \/>\n    baseName = &#8216;gs-spring-boot&#8217;<br \/>\n    version =  &#8216;0.1.0&#8217;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>which specifies your jar base name and version(basename+version.jar would be typical file)<\/p>\n<p>or<\/p>\n<p>if you want to create a war add code below to your build.gradle<\/p>\n<p>[code lang=&#8221;groovy&#8221;]<br \/>\napply plugin: &#8216;war&#8217;<\/p>\n<p>war {<br \/>\n    baseName = &#8216;myapp&#8217;<br \/>\n    version =  &#8216;0.5.0&#8217;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Now, to create jar or war or both run task below:<\/p>\n<p>[code]<br \/>\ngradle build<br \/>\n[\/code]<\/p>\n<p>Or specifically for creating jar<\/p>\n<p>[code]<br \/>\ngradle jar<br \/>\n[\/code]<\/p>\n<p>or for creating war<\/p>\n<p>[code]<br \/>\ngradle war<br \/>\n[\/code]<\/p>\n<p>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.<span id=\"hs-cta-wrapper-dbc0d364-cd83-4305-904a-6c918a408f51\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-dbc0d364-cd83-4305-904a-6c918a408f51\" class=\"hs-cta-node hs-cta-dbc0d364-cd83-4305-904a-6c918a408f51\"><br \/>\n<!-- [if lte IE 8]>\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><br \/>\n<a href=\"http:\/\/www.tothenew.com\/insights\"><img decoding=\"async\" id=\"hs-cta-img-dbc0d364-cd83-4305-904a-6c918a408f51\" class=\"hs-cta-img aligncenter\" style=\"border-width: 0px;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/57-scaled.jpg\" alt=\"How to use Gradle build system for multi-module projects? | TO THE NEW Digital\" \/><\/a><\/span><\/span><br \/>\n<!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":119,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[1511,2073,2072,2071],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22505"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22505"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22505\/revisions"}],"predecessor-version":[{"id":54546,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22505\/revisions\/54546"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}