Java 打包包含 JSP 和静态资源的 Spring Boot 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22121918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Package a spring boot application including JSPs and static resources
提问by yglodt
I want to package a spring-boot application as jar, and I do so with mvn package
.
我想将 spring-boot 应用程序打包为 jar,我使用mvn package
.
This produces a jar which does not contain any /WEB-INF/jsp
nor /src/main/webapp/resources
.
这样就产生了不包含任何一个罐子/WEB-INF/jsp
,也没有/src/main/webapp/resources
。
How can I make sure my jar contains everything needed ?
如何确保我的 jar 包含所需的一切?
Here my current pom.xml
:
这是我的当前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>
采纳答案by jz15
The following example works with Spring Boot 1.3.3.RELEASE:
https://github.com/ghillert/spring-boot-jsp-demo
以下示例适用于 Spring Boot 1.3.3.RELEASE:
https://github.com/ghillert/spring-boot-jsp-demo
The key is to put the static jsp content in:
关键是把静态jsp内容放在:
/src/main/resources/META-INF/resources/WEB-INF/jsp
and ensure you define the view prefix/suffix in your application.properties:
并确保在 application.properties 中定义视图前缀/后缀:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
回答by Martin Baumgartner
Is there any reason why you can't use the war packaging type? https://maven.apache.org/plugins/maven-war-plugin/usage.htmlI would recommend to use the war packaging type and use default maven web-application structure.
有什么理由不能使用战争包装类型吗?https://maven.apache.org/plugins/maven-war-plugin/usage.html我建议使用 war 打包类型并使用默认的 maven web-application 结构。
If you really want to use the jar plugin for your webapp, you need to configure it for your project. Due to your posting, I don't understand your structure and can't give you an example. Check out the usage of jar plugin here:https://maven.apache.org/plugins/maven-war-plugin/usage.html
如果你真的想为你的 webapp 使用 jar 插件,你需要为你的项目配置它。由于你的帖子,我不明白你的结构,不能给你一个例子。在这里查看jar插件的用法:https: //maven.apache.org/plugins/maven-war-plugin/usage.html
回答by Michael Simons
To create a runnable JAR with Spring Boot, use the spring-boot-maven-plugin in your pom.xml
要使用 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>
Maybe you want to have a look at my example application http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8/(Source is on GitHub, App is live and runs from a JAR).
也许你想看看我的示例应用程序http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8 /(源在 GitHub 上,应用程序是实时的并从 JAR 运行)。
One thing that you should note: JSP from JAR doesn't work due to some embedded Tomcat problems. I'm using Thymeleaf. If you need JSP, stay with the WAR deployment.
您应该注意的一件事是:由于某些嵌入式 Tomcat 问题,来自 JAR 的 JSP 不起作用。我正在使用百里香叶。如果您需要 JSP,请继续使用 WAR 部署。
回答by Preetam Myneni
Change your build tag to
将您的构建标记更改为
<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>