In this short tutorial, we are going to shed light on how to run Spring Boot application with Maven.

We will first start with a little insight into the mvn spring-boot:run command. Then, we will explain how to execute a Spring Boot app Using java -jar.

We will assume here that you have already installed and configured Maven on your machine and you are familiar with how to create Spring Boot applications.

Run Spring Boot App With Maven

First, we are going to create a basic Spring Boot application. To make things simple, let’s build an application with a single main class.

For instance, consider MainClassApp.java class. I used https://start.spring.io/ to bootstrap my Spring Boot app.

    
        @RestController
        @SpringBootApplication
        public class MainClassApp {
            @RequestMapping("/")
            public String index() {
                return "Welcome to devwithus.com";
            }
            public static void main(String[] args) {
                SpringApplication.run(MainClassApp.class, args);
            }
        }
    

@RestController tells Spring to consider MainClassApp as a restful controller. Feel free to take a look at how to build a REST API in Spring Boot.

POM and Maven Dependencies

Spring Boot Maven plugin provides a convenient way to package a project into a JAR/WAR file, run Spring Boot applications or generate build information.

To work with this plugin, we need to add it in the plugins section of the pom.xml:

    
        
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        
    

Please bear in mind that Spring Boot Maven plugin requires Maven 3.2 or later.

Mvn Spring Boot Run Command

The plugin offers a set of goals, each goal has a specific purpose. For example, it provides spring-boot:run to help us run a Spring Boot app with Maven.

To do so, we need to navigate to the root directory of the project and run:

    
        C:\Users\Asus\GIT\spbootexample>mvn spring-boot:run
        [INFO] Scanning for projects...
        ...
        [INFO] Attaching agents: []
          .   ____          _            __ _ _
         /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
        ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
         \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
          '  |____| .__|_| |_|_| |_\__, | / / / /
         =========|_|==============|___/=/_/_/_/
         :: Spring Boot ::                (v2.5.3)

        2021-07-31 23:26:39.586  INFO 13172 --- [           main] c.devwithus.spbootexample.MainClassApp   : Starting MainClassApp using Java 16.0.1 on DESKTOP-77TR72O with PID 13172 (C:\Users\Asus\GIT\spbootexample\target\classes started by Asus in C:\Users\Asus\GIT\spbootexample)
        2021-07-31 23:26:39.647  INFO 13172 --- [           main] c.devwithus.spbootexample.MainClassApp   : No active profile set, falling back to default profiles: default
        2021-07-31 23:26:40.162  INFO 13172 --- [           main] c.devwithus.spbootexample.MainClassApp   : Started MainClassApp in 1.163 seconds (JVM running for 1.709)
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  20.051 s
        [INFO] Finished at: 2021-07-31T23:26:40+02:00
        [INFO] ------------------------------------------------------------------------
    

As we can see in the maven log, the application is started with success. Now, open the browser and type: localhost:8080.

Let’s use curl command to check if our application is up and running properly:

    
        C:\Users\Asus>curl http://localhost:8080
        Welcome to devwithus.com
    

We can use mvn -DskipTests spring-boot:run to skip tests execution. Find more details about how to skip tests with Maven.

The best part about Spring Boot Maven plugin is that it allows us to customize the configuration.

For example, we can tune the profiles to enable when running the application:

    
        mvn spring-boot:run -Dspring-boot.run.profiles=prod,test
    

As shown above, we have enabled prod and test, we can override the default profiles with the help of the ${app.profiles} property:

    
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <profiles>${app.profiles}</profiles>
            </configuration>
        </plugin>
    

Now, let’s choose the dev profile:

    
        mvn spring-boot:run -Dapp.profiles=dev
    

Run Spring Boot App Using java -jar

In order to run a Spring Boot app using the java -jar command line, we need to package it in a JAR or WAR artifact first.

For that, we can use a Maven goal called package. This goal will simply package the compiled code in the specified distributable format under the target directory.

We only need to specify the format using the packaging tag in the pom.xml file:

    
        <packaging>jar</packaging>
    

Now, let’s package our Spring Boot application as a JAR file:

    
        C:\Users\Asus\GIT\spbootexample>mvn package
        [INFO] Scanning for projects...
        [INFO]
        [INFO] --------------------< com.devwithus:spbootexample >---------------------
        [INFO] Building spbootexample 0.0.1-SNAPSHOT
        [INFO] --------------------------------[ jar ]---------------------------------
        ...
        [INFO] Results:
        [INFO]
        [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
        [INFO]
        [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ spbootexample ---
        [INFO] Building jar: C:\Users\Asus\GIT\spbootexample\target\spbootexample-0.0.1-SNAPSHOT.jar
        [INFO]
        [INFO] --- spring-boot-maven-plugin:2.5.3:repackage (repackage) @ spbootexample ---
        [INFO] Replacing main artifact with repackaged archive
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
    

As we can see, Maven packaged the application as spbootexample-0.0.1-SNAPSHOT.jar under the target folder.

Unfortunately, the JAR file created by the mvn package command contains only the resources and the compiled Java classes.

So, we cannot execute it directly using the java -jar command. However, we can use the JAR file as a dependency in other projects.

Fortunately for us, Spring Boot Maven Plugin comes with an interesting goal called repackage.

As the name implies, it repackages the JAR produced by Maven and includes all the project runtime dependencies to make it executable.

As result, the final repackaged artifact can be executable using the command line java -jar.

    
        C:\Users\Asus\GIT\spbootexample>mvn clean package spring-boot:repackage
        [INFO] Scanning for projects...
        [INFO]
        [INFO] --------------------< com.devwithus:spbootexample >---------------------
        [INFO] Building spbootexample 0.0.1-SNAPSHOT
        [INFO] --------------------------------[ jar ]---------------------------------
        ...
        [INFO] --- spring-boot-maven-plugin:2.5.3:repackage (repackage) @ spbootexample ---
        [INFO] Replacing main artifact with repackaged archive
        [INFO]
        [INFO] --- spring-boot-maven-plugin:2.5.3:repackage (default-cli) @ spbootexample ---
        [INFO] Replacing main artifact with repackaged archive
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
    

Now, let’s try to run our Spring Boot application using java -jar and check if it works:

    
        C:\Users\Asus\GIT\spbootexample>java -jar target/spbootexample-0.0.1-SNAPSHOT.jar
          .   ____          _            __ _ _
         /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
        ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
         \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
          '  |____| .__|_| |_|_| |_\__, | / / / /
         =========|_|==============|___/=/_/_/_/
         :: Spring Boot ::                (v2.5.3)

        2021-08-13 23:27:30.172  INFO 11200 --- [           main] c.devwithus.spbootexample.MainClassApp   : Starting MainClassApp v0.0.1-SNAPSHOT using Java 16.0.1 on DESKTOP-77TR72O with PID 11200 (C:\Users\Asus\GIT\spbootexample\target\spbootexample-0.0.1-SNAPSHOT.jar started by Asus in C:\Users\Asus\GIT\spbootexample)
        2021-08-13 23:27:30.176  INFO 11200 --- [           main] c.devwithus.spbootexample.MainClassApp   : No active profile set, falling back to default profiles: default
        2021-08-13 23:27:31.954  INFO 11200 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
        2021-08-13 23:27:31.972  INFO 11200 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
        2021-08-13 23:27:31.976  INFO 11200 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.50]
        2021-08-13 23:27:32.099  INFO 11200 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
        2021-08-13 23:27:32.099  INFO 11200 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1810 ms
        2021-08-13 23:27:32.809  INFO 11200 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
        2021-08-13 23:27:32.828  INFO 11200 --- [           main] c.devwithus.spbootexample.MainClassApp   : Started MainClassApp in 3.444 seconds (JVM running for 4.093)
    

As shown above, our Spring Boot application is executed and started with success.

Conclusion

In this tutorial, we have showcased how to run Spring Boot applications with Maven using the mvn spring-boot:run command.

Along the way, we have explained how to accomplish the same thing using the java -jar command.