使用Maven将Spring框架应用程序打包为war

时间:2015-08-04 07:08:14

标签: java eclipse spring maven tomcat

我按照简单的官方Spring教程指南Enabling Cross Origin Requests for a RESTful Web Service,您可以从github repo下载文件。

我正在尝试使用Maven将此应用程序打包到war文件并将其部署在tomcat上,因此我在Packaging executable jar and war files上关注另一个正式的Spring文档,然后更改pom.xml以使其具有:

<packaging>war</packaging>

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

我还将java版本更改为7:

<properties>
    <java.version>1.7</java.version>
</properties>

我的IDE是Eclipse Luna 4.4.2,所以我保存了新的pom.xml文件,右键单击项目,选择Maven - &gt; updated the project,然后我右键单击该项目,选择run as - &gt; Run on Server选择Tomcat服务器,但无法加载内容,错误页面如下所示:

enter image description here

我不知道如何解决这个问题。我认为我的tomcat是可以的,因为我可以在其上运行其他Web应用程序。我的操作系统是Windows 7 64位,Java是JDK 7,maven是3.3.3

Tomcat错误跟踪:

...
INFO: Starting Servlet Engine: Apache Tomcat/8.0.23
Aug 04, 2015 12:10:12 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 04, 2015 12:10:12 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error during ServletContainerInitializer processing
javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:160)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:274)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:627)
    at java.lang.Thread.run(Thread.java:809)
Caused by: java.lang.NoClassDefFoundError: org.glassfish.jersey.server.ResourceConfig
    at java.lang.J9VMInternals.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1920)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:157)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.server.ResourceConfig
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
    ... 11 more

Aug 04, 2015 12:10:12 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/gs-rest-service-cors] startup failed due to previous errors
Aug 04, 2015 12:10:12 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 04, 2015 12:10:12 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 04, 2015 12:10:12 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2439 ms

完成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>org.springframework</groupId>
    <artifactId>gs-rest-service-cors</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.7</java.version>
    </properties>


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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

1 个答案:

答案 0 :(得分:0)

您还需要在子项目中指定依赖项。这在maven指南http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

中有所描述

因此,在您的情况下,您需要在pom.xml

中添加此依赖项
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
</dependency>

解决您的下一个问题。似乎war文件中的Application类没有为传统部署做好准备。因此,您需要将其更改为:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

有待进一步参考,您可以查看此link