Run Spring-boot's main using IDE

前端 未结 9 1839
灰色年华
灰色年华 2020-11-30 01:51

I have a spring-boot application that needs to:

  • Be deployable as a war in a servlet container
  • Be runnable via `mvn spring-boot:run``

I\

相关标签:
9条回答
  • 2020-11-30 02:12

    mvn spring-boot:run includes provided dependencies when it's creating the classpath. It sounds like IntelliJ IDEA does not. Without Tomcat on the classpath, Spring Boot's unable to create an embedded servlet container which causes the exception you're seeing. Arguably this is a bug in IntelliJ as, if there's no container to provide the dependency, then it really needs to be on the classpath.

    You may be able to fix the problem by overriding the default classpath that IntelliJ uses when running the main method to include the spring-boot-starter-tomcat dependency.

    0 讨论(0)
  • 2020-11-30 02:12

    Using the profile and instructions below, you can add a profile to maven that allows development in IntelliJ without changing things for other environments.

    <!-- Leave original dependency as-is -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <profiles>
        <!-- Enable this profile to run in IntelliJ. IntelliJ excludes provided dependencies from compile by default. -->
        <profile>
            <id>intellij</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    

    Click the Maven Projects button on the right side of IntelliJ, and under Profiles, select intellij.

    0 讨论(0)
  • 2020-11-30 02:13

    I was able to make this work by changing the scope of the spring-boot-starter-tomcat dependency to "compile" under Project structure->Dependencies tab. This doesn't effect pom.xml but allows this dependencies to be available to spring boot run configuration

    Click here for image on where to change this setting in idea

    0 讨论(0)
  • 2020-11-30 02:23

    I believe this could be related to https://youtrack.jetbrains.com/issue/IDEA-107048

    IntelliJ IDEA is not injecting the provided dependencies into the CLASSPATH and as Andy stated this is why spring is unable to create the embedded servlet container.

    They have a feature request since 2005 about this: https://youtrack.jetbrains.com/issue/IDEABKL-99

    Workarounds mentioned in the comments includes having a fake module with the necessary libs and using it as classpath, using the -Xbootclasspath JVM argument or using custom maven profiles for running (compiled) vs building (provided).

    0 讨论(0)
  • 2020-11-30 02:23

    Follow these steps:

    1. On the top right side of intellij window, click the drop down and select edit configuration and a new window will open.

    2. In this window, on top left side, click "+" button and select sprint boot.

    1. Then add you main class, and other details as shown in screenshot.

    2. Now Run the application.

    0 讨论(0)
  • 2020-11-30 02:26

    A workaround that is strongly inspired from https://youtrack.jetbrains.com/issue/IDEA-140041 is to start your main class with the test classpath (which includes the embedded servlet.)

    Steps (IntelliJ 16):

    1. Run -> Edit Configurations -> Add new configuration -> Pick Application type.
    2. Set Main class to <your.main.class>
    3. Set Use classpath of module to <*>_test (the test module!)
    4. Ok and Run it!
    0 讨论(0)
提交回复
热议问题