Setup Spring-Boot Using CLI:

0
448
Springboot

Introduction:

In this article we will see how to setup Spring-boot using CLI (Command Line Interface) and it is convenient to create springboot project through command line.

I like using command line to create spring boot projects via command line. Doing this it saves time and is more convenient once you are aware of the dependencies you need.

Download Spring-Boot CLI:

First step would be to download the zip of springboot CLI from the repository link like seen below:

http://repo.spring.io/release/org/springframework/boot/spring-boot-cli

The latest version seen is spring-boot-cli-2.1.1.RELEASE.

Setup Environment Variable:

Create a system variable SPRING_HOME, and set its value like to the downloaded path of the unzipped 2.1.1.RELEASE folder as seen in screenshot below:

C:\Softwares\spring-boot-cli-2.1.1.RELEASE-bin

SPRING_HOME ENV VARIABLE for Setup Spring-boot using CLI


Then, update the PATH variable adding at the end as ;%SPRING_HOME%\bin like seen below:

PATH ENV Variable for Setup Spring-boot using CLI

Verify SpringBoot CLI Setup:

Now to check if Springboot CLI is configured correctly. Execute the below command like shown in below from your command prompt:

C:\Users\demo>spring --version

Spring CLI v2.1.1.RELEASE

C:\Users\demo>spring --help

usage: spring [--help] [--version]

       <command> [<args>]

Available commands are:

  run [options] <files> [--] [args]

    Run a spring groovy script

  grab

    Download a spring groovy script's dependencies to ./repository

  jar [options] <jar-name> <files>

    Create a self-contained executable jar file from a Spring Groovy script

  war [options] <war-name> <files>

    Create a self-contained executable war file from a Spring Groovy script

  install [options] <coordinates>

    Install dependencies to the lib/ext directory

  uninstall [options] <coordinates>

    Uninstall dependencies from the lib/ext directory

  init [options] [location]

    Initialize a new project using Spring Initializr (start.spring.io)

  encodepassword [options] <password to encode>

    Encode a password for use with Spring Security

  shell

    Start a nested shell

Common options:

  -d, --debug Verbose mode

    Print additional status information for the command you are running

See 'spring help <command>' for more information on a specific command.

C:\git\repo>spring init –list

The above command list all capabilities of spring init. There are more features like seen in screenshot below:

Spring init-list lists capabilities for Setup Spring-boot using CLI

Create SpringBoot Project:

We will use spring init command to create spring boot project like seen below:

For Example:

spring init --name=<projectname>

--dependencies=<project dependencies like web,data-jpa,etc>

--package-name=<com.demo.spring>

--groupId=<com.demo.spring.example>

--artifactId=<springInitializerExample> <ProjectName>

C:\git\repo> spring init --name=springdemo --dependencies=web,data-jpa,mysql,devtools --package-name=com.demo.spring.initializer --groupId=com.demo.spring.initializer --artifactId=springInitializerExample SpringInitializerDemo

Using service at https://start.spring.io

Failed to retrieve metadata from service at 'https://start.spring.io' (start.spring.io)

Setup Proxy Configuration:

The above command fails because the proxy is not configured. Now, lets create a user-variable JAVA_OPTS to provide proxy information like below and save it:

JAVA_OPTS Variable

JAVA_OPTS = -Dhttp.proxyHost=yourproxhost.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost= yourproxhost. com -Dhttps.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=”password”

Now lets try again with the below command:

C:\git\repo> spring init --name=springdemo --dependencies=web,data-jpa,mysql,devtools --package-name=com.demo.spring.initializer --groupId=com.demo.spring.initializer --artifactId=springInitializerExample SpringInitializerDemo

Using service at https://start.spring.io

Project extracted to 'C:\git\vgt\SpringInitializerDemo'

So we successfully created the SpringInitializerDemo project.

I can change the name of project to SpringDemo and other parameters. I can do that by adding –force at the end like seen below:

C:\git\repo> spring init --name=SpringDemo --dependencies=web,data-jpa,mysql,devtools --package-name=com.demo.spring.initializer --groupId=com.demo.spring.initializer --artifactId=springInitializerExample SpringInitializerDemo --force

Using service at https://start.spring.io

Project extracted to 'C:\git\vgt\SpringInitializerDemo'

So we have successfully updated the name if we check the parent pom.xml like seen below:

parent pom.xml for Setup Spring-boot using CLI

In this tutorial, you learned how to setup Spring-boot using CLI (Command Line Interface), i.e an easier way for developing the project. Please check out my tutorial on setting up spring-boot project with-memory database. I hope it helps!!

References:

Setting up spring-boot using command-line: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html