springboot war打包步骤_c_zyer的博客-程序员秘密

技术标签: java  javaweb  

packaging的设置

<packaging>war</packaging>  

修改pom.xml文件依赖

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

Application类继承servletInitialize类

  public class Application extends SpringBootServletInitializer{
    
               .... 
}

或者新增ServletInitializer类(此项为推荐项):

import org.springframework.boot.builder.SpringApplicationBuilder;  
import org.springframework.boot.context.web.SpringBootServletInitializer;  

public class ServletInitializer extends SpringBootServletInitializer {
      

    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  (此处Application为主类,如果命名为其他的需要修改)
    }  
}  

mvn 打包并启动tomcat

mvn package
java -jar target/(项目名)test-0.0.1-SNAPSHOT.jar

相关问题

开发环境使用jdk1.8.0_60,把springboot 项目打成war包后,
部署到apache-tomcat-7.0.68时报错如下,换成apache-tomcat-8.0.9解决

org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/my-springboot-web-0.0.1]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1091)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1980)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
    at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:149)
    at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5573)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    ... 10 more
六月 22, 2017 12:19:15 上午 org.apache.catalina.startup.HostConfig deployWAR
严重: Error deploying web application archive E:\Data\apache-tomcat-7.0.68\webapps\my-springboot-web-0.0.1.war
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/my-springboot-web-0.0.1]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:903)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1091)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1980)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

另外springboot项目打成war包部署到tomcat时需要改变启动方式,否则运行tomcat时war包只会解压,但是加载不了

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication  //- springboot项目,并配置扫描路径
@EnableAutoConfiguration //- springboot自动配置,这个注解可以根据你依赖的包自动生成相关配置
public class StartApp extends SpringBootServletInitializer {
    
  public static void main(String[] args) {
    SpringApplication.run(StartApp.class, args);
  }
  /**
  * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
  */
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(StartApp.class);
  }

部署到外部tomcat时,注意pom.xml配置,否则会报错

<!-- 这里指定打war包的时不再需要tomcat相关的包,但是本地运行时必须注释掉,否则会报错 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>1.3.5.RELEASE</version>
      <scope>provided</scope>
    </dependency>

报错信息:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
  at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
  at com.StartApp.main(StartApp.java:13)
Caused by: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
  at org.apache.catalina.util.LifecycleBase.<clinit>(LifecycleBase.java:37)
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:158)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:160)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
  ... 8 more
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
  at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 12 more

springboot项目使用jsp页面时会添加以下依赖,但是在外部tomcat运行时会报错
因为tomcat /lib下自带的jar包(jasper.jar,jsp-api.jar等)会跟war包项目路径/lib下的解析jsp的jar包冲突
删除项目路径/lib下的相关jar包即可,如下图

<!-- 添加jsp支持 -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <version>8.0.33</version>
    </dependency>

报错信息:

[ERROR][2017-06-22 00:48:28,303][org.springframework.boot.context.web.ErrorPageFilter]Forwarding to error page from request [/excel] due to exception [java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/excel_jsp, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type org/apache/tomcat/InstanceManager used in the signature]
javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/excel_jsp, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type org/apache/tomcat/InstanceManager used in the signature

默认配置下就可以通过http://localhost:8080/war包名/@RequestMapping.value访问了

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/c_zyer/article/details/78667828

智能推荐

详解16个CSS新特性(2021最新版-下)_阿里巴巴淘系技术团队官网博客的博客-程序员秘密

2021 CSS 的新特性和之前两年的相比有相似也有不同,本文就带大家继续看今年的 CSS 到底说了什么。上篇指路:详解16个CSS新特性(2021最新版-上)CSS 等比缩放CSS 等比...

win10 tensorflow ssd制作自己的数据集出现错误_SuperLuu7的博客-程序员秘密

图像标注后,用了自己的图像名字,报错如下:(最终还是采用原文件命名格式)_sys.exit(main(argv)) File &quot;D:/work/SSD-Tensorflow-master/tf_convert_data.py&quot;, line 58, in main pascalvoc_to_tfrecords.run(FLAGS.dataset_dir, FLAGS.output_...

magento教程---在layout中,重新布置block_而立了吗?的博客-程序员秘密

视频地址:https://www.youtube.com/watch?v=tnx0ov5e6fI&amp;list=PL9Q0S-CmtAhJVjwnK5ip6H84sCuRQcACY&amp;index=12把要修改的module写在自己定义的module的module.xml内。自己写的module,depend on这个module。用来修改他。是先后顺序。然后在自己的module内重新定义一个相同的xml。进行move就可以了。...

QEMU搭建虚拟化开发环境_ding2zz的博客-程序员秘密

QEMU搭建虚拟化开发环境环境:ubuntu 20.041. qemu安装1.1 命令行安装sudo apt-get install qemu qemu-system qemu-user1.2 源码安装待补充2. qemu运行arm32/arm64程序2.1 arm32/64交叉编译器安装# arm32sudo apt-get install gcc-arm-linux-gnueabihf# arm64sudo apt-get install gcc-aarch64-linux-g

ART工作流程及特性_般若程序蝉的博客-程序员秘密

首发于我的博客网站(prajna.top) 欢迎大家前去交流,有pdf版本。ART (Android Runtime)是运行于 Android 5.0(API 21)及以上的默认运行时环境,用来替换Dalvik,它大大提升了 Android 平台和应用的性能和流畅度。ART兼容Dalvik,能运行它的字节码,它的启用是系统默认的,不需要做什么配置,可以通过调System.getProperty...

随便推点

图书管理系统python代码课程设计报告_数据结构图书管理系统课程设计报告_weixin_39633437的博客-程序员秘密

如文档对你有用,请下载支持!一、设计题目与要求【问题描述】设计一个计算机管理系统完成图书管理基本业务。【基本要求】(1)每种书的登记内容包括书号、书名、著作者、现存量和库存量;(2)对书号建立索引表(线性表)以提高查找效率;(3)系统主要功能如下:①采编入库:新购一种书,确定书号后,登记到图书帐目表中,如果表中已有,则只将库存量增加;②借阅:如果一种书的现存量大于0,则借出一本,登记借阅者的书证号...

(转载)Android数据库高手秘籍(七)——体验LitePal的查询艺术_星屑的王子的博客-程序员秘密

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/40153833经过了多篇文章的学习,我们已经把LitePal中的绝大部分内容都掌握了。现在回想起来了,增删改查四种操作中的前三种我们都已经学完了,不知道现在使用起数据库来,你有没有感觉到格外的轻松和简单。但是呢,我们都知道,在所有的数据库操作当中,查询操作肯定是最复杂的,用法

Java提高篇——通过分析 JDK 源代码研究 Hash 存储机制_混沌之光的博客-程序员秘密

通过 HashMap、HashSet 的源代码分析其 Hash 存储机制HashMap 的存储实现Hash 算法的性能选项HashMap 的读取实现HashSet 的实现HashMap 和 HashSet 是 Java Collection Framework 的两个重要成员,其中 HashMap 是 Map 接口的常用实现类,HashSet 是 Set 接口的常用实现

uni-app 跳转Android原生界面(Activity),并传值交互_THMAIL的博客-程序员秘密_uniapp 接收intent

应大家的要求,我研究了一下相互传值操作一、uni-app 跳转Android原生界面(Activity)并传值前端传值操作&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt; &lt;meta charset="utf-8"/&gt;&lt;/head&gt;&lt;body&gt;&lt;input type="button" value="js start native Activity" onclick="jsCallNative.

css+js实现下拉菜单栏_yuan487639的博客-程序员秘密_js下拉栏

直接上代码吧,没什么复杂逻辑<!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head> <style type="text/css"> .out { } .out + ul { display: block; } .in

推荐文章

热门文章

相关标签