使用Maven构建JAR。 Spring MVC应用程序。诸如JS,JSP,CSS之类的静态资产没有内置到JAR中

时间:2015-03-17 20:50:35

标签: maven spring-mvc jar spring-boot

我的目标是使用嵌入式Tomcat服务器构建可执行JAR来运行我的Spring MVC应用程序。

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

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

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

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

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的内容时,我发现我的项目中没有任何静态文件进入我的JAR。以下是已编译JAR的内容的屏幕截图:http://i.imgur.com/hFuDuNC.png

我想将我的'webapp'文件夹包含在内置的JAR中,并可以通过我的Spring应用程序使用,以避免上面详述的错误。

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

指向与我的问题相关的分步资源的链接也会有所帮助。

这是我的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>

1 个答案:

答案 0 :(得分:2)

使用Spring Boot创建可执行jar的方式构建应用程序的方式不正确。

如Spring Boot文档的this部分所述,您需要将静态资产移至以下某个位置:/static/public/resources,{{ 1}}

同样,嵌入式servlet容器不能像JSP here那样顺利地使用JSP。我建议您使用supported模板化的引擎,无缝地开箱即用。