使用 Maven 构建 JAR。 Spring MVC 应用程序。静态资产,如 JS、JSP、CSS 未内置到 JAR 中

Building JAR using Maven. Spring MVC Application. Static assets such as JS, JSP, CSS not built into JAR

我的目标是为 运行 我的 Spring MVC 应用程序构建一个带有嵌入式 Tomcat 服务器的可执行 JAR。

您可以在此处查看我的项目目录结构的屏幕截图:http://i.imgur.com/0TweATX.png

当我运行“mvn clean install”时,会生成一个新的 JAR,并且 运行 可用。我执行 JAR 并看到 Spring 消息,我的所有端点都飞过。

当我尝试转到 localhost:8080/ANYENDPOINT 时,我看到 Spring 提供的通用 404 页面(告诉我我没有映射 /error 端点)。

查看日志,我收到以下错误:

2015-03-17 16:27:10.948  INFO 12160 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]:Initializing Spring FrameworkServlet 'dispatcherServlet' 2015-03-17 16:27:10.949  INFO 12160 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 
2015-03-17 16:27:10.987  INFO 12160 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet :FrameworkServlet 'dispatcherServlet': initialization completed in 38 ms 2015-03-17 16:27:11.085  WARN 12160 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/jsp/homepage.jsp] in DispatcherServlet with name 'dispatcherServlet'

它看不到我的'homepage.jsp'。但是如果你看看我的目录结构,它就在那里。但是,当我查看构建的 JAR 的内容时,我发现项目中的 none 个静态文件已添加到我的 JAR 中。这是已编译 JAR 内容的屏幕截图:http://i.imgur.com/hFuDuNC.png

我希望将我的 'webapp' 文件夹包含在构建的 JAR 中并可供我的 Spring 应用程序使用,以避免我在上面详述的错误。

我对整个部署这件事还很陌生。我一直只是在 Eclipse 中进行开发工作,在外部 Tomcat 服务器上验证更改,然后推送代码。现在我想学习如何对付部署怪物。如果我做的任何事情都很愚蠢,请告诉我。

与我的问题相关的分步资源 link 也会有所帮助。

这是我的 POM:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.github.xxxx</groupId>
    <artifactId>xxxx</artifactId>
    <packaging>jar</packaging>
    <version>1.8-SNAPSHOT</version>
    <name>Maven Webapp</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>StudentEnrollmentWithMyBatis</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.2.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-scm-plugin</artifactId>
                        <version>1.8.1</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

您构建应用程序的方式不适合使用 Spring Boot 创建可执行 jar。

如 Spring 引导文档的 this 部分所述,您需要将静态资产移动到以下之一:/static/public/resources, /META-INF/resources

嵌入式 servlet 容器也不能像前面提到的那样与 JSP 一起流畅运行 here. I suggest you use one the templated engines that are supported 开箱即用。