有没有办法使用Maven Scope'提供'部署到WAR时,但编译'在IDE中运行时?

时间:2017-04-28 15:00:11

标签: maven intellij-idea spring-boot dependency-management

我正在创建一个将在Tomcat服务器内运行的Spring Boot应用程序。我在主类上使用@SpringBootApplication标记,在pom.xml文件中,我使用范围compile作为Tomcat依赖项。

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

这样,当在我的IDE(IntelliJ)中运行时,应用程序启动自己的Tomcat服务器,这样我就可以轻松地测试应用程序。部署到WAR文件时,我编辑pom.xml文件并将tomcat的范围设置为&#39;提供&#39;,因为我将WAR文件加载到服务器上的现有Tomcat容器中。

我通过从命令行运行./mvnw clean package来创建WAR文件。

将范围设置为&#39;提供&#39;并在IDE中运行,我收到以下错误:

...
2017-05-02 10:40:19.529  WARN 14508 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [be.test.test.Test]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer$ErrorPageFilterConfiguration
2017-05-02 10:40:19.534 ERROR 14508 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@7e1b02ea: startup date [Tue May 02 10:40:19 CEST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) [spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    ...

有没有办法以我可以在&#34;测试模式&#34;中运行它的方式设置它?在IDE中,但是可以在不更改pom.xml文件的情况下创建WAR文件吗?我见过范围&#34;测试&#34;也可以,但从我的理解,这只在单元测试期间使用?或者这正是我要找的东西?

声明:
我是非常所有这一切的新手,所以如果我错误地了解各种基础知识并且请指出我应该研究的概念和技术,请耐心等待;)

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.test.test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>test</name>
    <description>test</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- Added to be able to load into Tomcat as a Servlet: https://stormpath.com/blog/tutorial-spring-boot-war-files -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>

            <!-- Use 'provided' when deploying to .WAR for Tomcat -->
                <!-- This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. -->
            <!-- Use 'compile'  when working in IntelliJ for development -->
                <!-- This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. -->
            <scope>compile</scope>

        </dependency>

    </dependencies>

<!-- Commenting this out to deploy to the existing Tomcat server: https://spring.io/blog/2014/03/07/deploying-spring-boot-applications
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->


</project>

0 个答案:

没有答案