How to Run Spring Boot Application

Posted on

Introduction

Basically, this is an article focusing on how to run spring boot application. For a general description, the spring boot application exist by creating it using any available Java IDE or any Java programming language editor. In this example, it is using IntelliJ IDEA. The spring boot application is associated with Maven. So, maven is the tool for running the spring boot application. First of all, take a look at the pom.xml build file description as in the build part in the following lines :

<build>
   <finalName>app-starter</finalName>
   <resources>
      <resource>
         <filtering>true</filtering>
         <directory>src/main/resources</directory>
      </resource>
      <resource>
         <directory>src/main/docs</directory>
         <filtering>true</filtering>
      </resource>
      <resource>
         <directory>src/main/java</directory>
         <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include>
         </includes>
      </resource>
   </resources>
   <testResources>
      <testResource>
         <filtering>true</filtering>
         <directory>src/test/resources</directory>
      </testResource>
      <testResource>
         <directory>src/test/java/</directory>
      </testResource>
   </testResources>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <executions>
            <execution>
               <configuration>
                  <outputDirectory>${basedir}/target/classes</outputDirectory>
                  <encoding>UTF-8</encoding>
                  <resources>
                     <resource>
                        <directory>${basedir}/src</directory>
                        <includes>
                           <include>**/*.xml</include>
                        </includes>
                     </resource>
                  </resources>
               </configuration>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-enforcer-plugin</artifactId>
         <version>3.0.0-M2</version>
      </plugin>
   </plugins>
</build>

Running the Spring Boot Application

So, using the existing Java web-based application with maven associated in it, the following are the steps for running the spring boot application :

  1. First of all, make sure that maven is available. It is executable in the command line. Just type ‘mvn’ in the command line as follows :
    Microsoft Windows [Version 10.0.19042.1052]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Mark Stuart>mvn
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.124 s
    [INFO] Finished at: 2021-06-24T07:06:19+07:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException
    
    C:\Users\Mark Stuart>
    
  2. So, after checking the maven command exist, just execute the command for running the Spring Boot application as follows :
    C:\application>mvn spring-boot:run
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------< com.github.adminfaces:app-starter >-----------------
    [INFO] Building app-starter 0.1-SNAPSHOT
    [INFO] --------------------------------[ war ]---------------------------------
    [INFO]
    [INFO] >>> spring-boot-maven-plugin:2.0.6.RELEASE:run (default-cli) > test-compile @ app-starter >>>
    [INFO]
    [INFO] --- maven-enforcer-plugin:3.0.0-M2:enforce (enforce-versions) @ app-starter ---
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ app-starter ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 4 resources
    [INFO] skip non existing resourceDirectory C:\application\src\main\docs
    [INFO] Copying 36 resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ app-starter ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ app-starter ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory C:\application\src\test\resources
    [INFO] skip non existing resourceDirectory C:\application\src\test\java
    [INFO]
    [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ app-starter ---
    [INFO] No sources to compile
    [INFO]
    [INFO] <<< spring-boot-maven-plugin:2.0.6.RELEASE:run (default-cli) < test-compile @ app-starter <<<
    [INFO]
    [INFO]
    [INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:run (default-cli) @ app-starter ---
    [INFO] Attaching agents: []
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.0.6.RELEASE)
    
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/m2/org/springframework/spring-core/5.0.10.RELEASE/spring-core-5.0.10.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
    WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
    

    As it is available in the above output of the command execution, the spring boot application is running properly. Just access the address of the spring boot application using any available web browsers. Where is the information about the address exist ?. Well, apparently it exist in the file with the name of ‘application.properties’ as part of the source code of the project. The following is the content of the file :

    server.port=8383
    
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.default-fetch-size=100
    mybatis.configuration.default-statement-timeout=30
    
    logging.config=classpath:log4j.properties
  3. In this context, just access the url http://localhost:8383 on the web browser to run the spring boot application. If there is no further problem exist, the application will appear in the web browser.

Leave a Reply