71

5 Ways to Run Spring Boot Apps

 5 years ago
source link: https://www.tuicool.com/articles/hit/MjaEreU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

5 Ways to Run Spring Boot Apps

DZone's Guide to

5 Ways to Run Spring Boot Apps

Look for different ways to run your Spring Boot application? Check out this post to learn more about the different ways you can use Spring Boot.

Oct. 08, 18 · Java Zone ·

Free Resource

Join the DZone community and get the full member experience.

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

In this article, we will discuss different ways of running Spring Boot applications:

1. Running from an IDE

2. Running as a Packaged Application

3. Using the Maven Plugin

4. Using External Tomcat

5. Using the Gradle Plugin

1. Running From an IDE

You can run a Spring Boot application from your IDE as a simple Java application (Application.java or Main class).

aY3emiu.png!web

2. Running as a Packaged Application

If you use the Spring Boot Maven or Gradle plugins to create an executable jar, you can run your application using java -jar . For example, you need to change directory to the current project directory and run following command in cmd.

$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar

It is also possible to run a packaged application with remote debugging support enabled. Doing so lets you attach a debugger to your packaged application, as shown in the following example:

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar

3. Using the Maven Plugin

The Spring Boot Maven plugin includes a run goal that can be used to quickly compile and run your application. Applications run in an exploded form, as they do in your IDE. The following example shows a typical Maven command to run a Spring Boot application:

$ mvn spring-boot:run

We can also use the MAVEN_OPTS operating system environment variable, as shown in the following example:

$ export MAVEN_OPTS=-Xmx1024m

4. Using External Tomcat

We can also deploy a Spring Boot web application WAR file to the external Tomcat servlet container. There are three steps we can follow to create a war file and deploy in the external Tomcat servlet container.

Step 1: Change the Packaging Type

<packaging>war</packaging>

Step 2. Add Spring-Boot-Starter-Tomcat as the Provided Scope

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
  </dependency>

Step 3: Spring Boot Application or Main Class Extends the SpringBootServletInitializer

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
    }
}

You can learn more about how to deploy a Spring Boot WAR file to the external Tomcat server with this step-by-step example .

5. Using the Gradle Plugin

The Spring Boot Gradle plugin also includes a bootRun   task that can be used to run your application in an exploded form. The  bootRun   task is added whenever you apply the   org.springframework.boot   and Java plugins, as shown in the following example:

$ gradle bootRun

You might also want to use the JAVA_OPTS operating system environment variable, as shown in the following example:

$ export JAVA_OPTS=-Xmx1024m

You may also interested in this post: Installing Spring Boot With Build Tools Maven and Gradle ! Happy coding!

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK