{"id":7397,"date":"2021-07-30T10:08:58","date_gmt":"2021-07-30T14:08:58","guid":{"rendered":"https:\/\/springframework.guru\/?p=7397"},"modified":"2024-10-21T09:32:39","modified_gmt":"2024-10-21T13:32:39","slug":"spring-boot-cli","status":"publish","type":"post","link":"https:\/\/springframework.guru\/spring-boot-cli\/","title":{"rendered":"How to Use Spring Boot CLI &#8211; Command Line Interface"},"content":{"rendered":"<p>The Spring Boot CLI (Command Line Interface) is a command-line tool that you can use to run and test Spring Boot Applications from a Terminal. The CLI is one of the fastest ways to develop a Spring-based application.<\/p>\n<h2>How does Spring Boot CLI work?<\/h2>\n<p>It uses Spring Boot Starter and Spring Boot AutoConfigure components to internally resolve all dependencies and execute the application.<\/p>\n<p>Dependencies get resolved automatically due to the Groovy and Grape dependency manager present in the CLI.<\/p>\n<p>Groovy is an object-oriented dynamic programming language and Grape is a JAR dependency manager that is embedded into Groovy. Therefore, you can run groovy scripts without so much boilerplate code.<\/p>\n<p>In this post, you will learn how to configure Command Line Interface for Spring and execute simple terminal commands.<\/p>\n<h2>CLI Setup on Windows<\/h2>\n<p>For Windows Operating System, you can follow these steps to complete its installation.<\/p>\n<p>Download the latest version of CLI API as ZIP archive from Spring software repository.<\/p>\n<p>Unzip the zip distribution into a convenient location as done below.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7398\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1.png\" alt=\"\" width=\"912\" height=\"219\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1.png 912w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1-300x72.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1-768x184.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1-848x204.png 848w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-unzip1-410x98.png 410w\" sizes=\"(max-width: 912px) 100vw, 912px\" \/><\/a><\/p>\n<p><strong> Set SPRING_HOME pointing to the installation directory as shown in the below figure<\/strong><\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7399\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path1.png\" alt=\"\" width=\"652\" height=\"164\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path1.png 652w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path1-300x75.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path1-410x103.png 410w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/a><\/p>\n<p>Set your PATH variable pointing to the bin directory of the installation, as set in the below figure.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7400\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path.png\" alt=\"\" width=\"523\" height=\"488\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path.png 523w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path-300x280.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-path-410x383.png 410w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/a><\/p>\n<p>Once done, check the version using the command <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">spring --version<\/code>.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-version.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7401\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-version.png\" alt=\"\" width=\"708\" height=\"212\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-version.png 708w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-version-300x90.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-version-410x123.png 410w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/a><\/p>\n<p>Hence, you can see the Spring version as <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">2.5.2<\/code><\/p>\n<h2>CLI Setup with SDKMAN<\/h2>\n<p>SDKMAN (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI. Get SDKMAN from <a href=\"http:\/\/sdkman.io\" target=\"_blank\" rel=\"noopener\">sdkman.io<\/a> and install Spring Boot by using the following commands:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">sdk install springboot\nspring --version\n<\/pre>\n<h2>CLI Setup on Linux\/Mac<\/h2>\n<p>If you are on a Mac and using <a href=\"https:\/\/brew.sh\/\" target=\"_blank\" rel=\"noopener\">Homebrew<\/a>, all you need to do to install the Spring Boot CLI is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">brew tap pivotal\/tap\nbrew install springboot\n<\/pre>\n<h2>CLI Setup with GVM<\/h2>\n<p>GVM (the Groovy Environment Manager) can be used for managing multiple versions of various Groovy and Java binary packages, including Groovy itself and the Spring Boot CLI. Get gvm from <a href=\"https:\/\/github.com\/flofreud\/gvm\" target=\"_blank\" rel=\"noopener\">gvmtool.net<\/a> and install Spring Boot with:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">gvm install springboot\nspring --version\n<\/pre>\n<h2>Spring Boot CLI Example<\/h2>\n<p>In this example, we will develop a simple Spring Boot MVC RestController.<\/p>\n<p>Since CLI runs groovy scripts, therefore, let us write one.<\/p>\n<p>This is the code for <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">DemoCLI.groovy<\/code> class.<\/p>\n<p><strong>DemoCLI.groovy<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">@RestController\nclass DemoCLI {\n    @RequestMapping(\"\/get-message\")\n    String greetHello() {\n        \"Hi ! This is a message from groovy script\"\n    }\n}\n<\/pre>\n<p>It is a simple REST Controller annotated class with a request handler method.<\/p>\n<h2>Run and Test the Script<\/h2>\n<p>CLI provides a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">spring<\/code> command to run Spring Boot Groovy scripts from Command Prompt.<\/p>\n<p>Open the command prompt and type <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">spring run DemoCLI.groovy<\/code><\/p>\n<p>This is how the console window looks:<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-7415\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-1024x364.png\" alt=\"\" width=\"1024\" height=\"364\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-1024x364.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-300x107.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-768x273.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-848x301.png 848w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console-410x146.png 410w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-console.png 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>On executing the spring run command, the embedded Tomcat server starts at the default port number: 8080.<\/p>\n<p>You can open the browser to access the URL <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">http:\/\/localhost:8080\/get-message<\/code><\/p>\n<p>This is the output.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-browser.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7416\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-browser.png\" alt=\"\" width=\"375\" height=\"108\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-browser.png 375w, https:\/\/springframework.guru\/wp-content\/uploads\/2021\/07\/cli-browser-300x86.png 300w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<p>We are able to access our first Spring Boot MVC RESTful WebService.<\/p>\n<h2>Summary<\/h2>\n<p>Spring Boot CLI avoids lots of boilerplate code and Spring Configuration, as it automatically detects which dependency JARs are to be downloaded based on the classes and annotations used in code, thereby reducing development time.<\/p>\n<p>You can find the source code of this post here on <a href=\"https:\/\/github.com\/spring-framework-guru\/sfg-blog-posts\/tree\/master\/springboot-cli\" target=\"_blank\" rel=\"noopener\">Github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Spring Boot CLI (Command Line Interface) is a command-line tool that you can use to run and test Spring Boot Applications from a Terminal. The CLI is one of the fastest ways to develop a Spring-based application. How does Spring Boot CLI work? It uses Spring Boot Starter and Spring Boot AutoConfigure components to [&hellip;]<a href=\"https:\/\/springframework.guru\/spring-boot-cli\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":111,"featured_media":4655,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21,104],"tags":[40,29,367],"class_list":["post-7397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-spring-boot","tag-spring","tag-spring-boot","tag-spring-boot-cli"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/07\/NewBannerBOOTSWeb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-1Vj","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/7397"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=7397"}],"version-history":[{"count":21,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/7397\/revisions"}],"predecessor-version":[{"id":8276,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/7397\/revisions\/8276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4655"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=7397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=7397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=7397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}