打包Spring启动应用程序,包括JSP和静态资源

时间:2014-03-01 23:39:26

标签: java spring maven spring-mvc spring-boot

我想将一个spring-boot应用程序打包为jar,然后使用mvn package打包。

这会生成一个不包含任何/WEB-INF/jsp/src/main/webapp/resources的jar。

如何确保我的jar包含所需的一切?

这是我当前的pom.xml

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-samples</artifactId>
    <version>1.0.0.RC3</version>
</parent>

<packaging>jar</packaging>

<properties>
    <main.basedir>${basedir}/../..</main.basedir>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>

<dependencies>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1101-jdbc41</version>
    </dependency>

</dependencies>

<!-- Package as an executable JAR -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>

<!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 1.0.0.RELEASE) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>
<artifactId>com.example.app</artifactId>

5 个答案:

答案 0 :(得分:11)

以下示例适用于Spring Boot 1.3.3.RELEASE: https://github.com/ghillert/spring-boot-jsp-demo

关键是将静态jsp内容放在:

/src/main/resources/META-INF/resources/WEB-INF/jsp

并确保在application.properties中定义视图前缀/后缀:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

答案 1 :(得分:5)

你有什么理由不能使用战争包装类型吗? https://maven.apache.org/plugins/maven-war-plugin/usage.html 我建议使用war包装类型并使用默认的maven web-application结构。

如果您真的想为您的webapp使用jar插件,则需要为您的项目配置它。由于您的发布,我不了解您的结构,不能给您一个例子。在这里查看jar插件的用法:https://maven.apache.org/plugins/maven-war-plugin/usage.html

答案 2 :(得分:3)

将您的构建代码更改为

 <build>
<resources>
            <resource>
                <directory>${basedir}/src/main/webapp</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

答案 3 :(得分:1)

要使用Spring Boot创建可运行的JAR,请在pom.xml中使用spring-boot-maven-plugin

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

也许您想查看我的示例应用程序http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8/(源代码在GitHub上,App是实时的并且从JAR运行)。

您应该注意的一件事:由于某些嵌入式Tomcat问题,来自JAR的JSP无法正常工作。我正在使用Thymeleaf。如果您需要JSP,请继续使用WAR部署。

答案 4 :(得分:0)

我遇到了同样的问题。我在上面的答案中尝试了您标记为正确的答案,但这对我不起作用。

这有效...将pom.xml更改为...

<packaging>war</packaging>

...这将使maven创建一个可执行的WAR文件,就像这样...

java -jar yourwarfile.war

我在这里找到了类似问题的解决方案...

WEB-INF not included in WebApp using SpringBoot, Spring-MVC and Maven