springboot打jar包和war包部署(二)

您所在的位置:网站首页 jar文件修改 springboot打jar包和war包部署(二)

springboot打jar包和war包部署(二)

2023-04-18 09:45| 来源: 网络整理| 查看: 265

springboot打jar包和war包部署 jar包部署,在web模块pom.xml修改

因为在创建web模块时指定的打包方式是jar,所以只需修改以下配置即可

org.springframework.boot spring-boot-maven-plugin com.zm.web.WebApplication ZIP repackage

使用maven下的package进行打包

在这里插入图片描述 在这里插入图片描述 找到jar包位置,cmd进入命令 在这里插入图片描述 在这里插入图片描述 输入 cmd指令 java -jar web-0.0.1-SNAPSHOT.jar 在这里插入图片描述 测试一下,成功访问 在这里插入图片描述 war包部署 修改web模块下pom.xml打包方式 war web模块下pom.xml排除内置的Tomcat容器 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat

参考连接: https://blog.csdn.net/weixin_30329623/article/details/99421450.

org.apache.tomcat.embed tomcat-embed-core 8.5.23 注释web模块下pom.xml的打jar包配置 org.springframework.boot spring-boot-maven-plugin com.zm.web.WebApplication web模块下pom.xml新增打war包配置 web org.apache.maven.plugins maven-war-plugin 3.0.0 false

最终web模块下的pom.xml如下:

4.0.0 zm org.example 1.0-SNAPSHOT com.zm web 0.0.1-SNAPSHOT web Demo project for Spring Boot 1.8 war ${project.parent.groupId} service ${project.parent.version} ${project.parent.groupId} entity ${project.parent.version} org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.apache.tomcat.embed tomcat-embed-core 8.5.23 web org.apache.maven.plugins maven-war-plugin 3.0.0 false org.springframework.boot spring-boot-maven-plugin com.zm.web.WebApplication

配置步骤: 1、继承SpringBootServletInitializer 外部容器部署的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,此时我们需要在启动类中继承SpringBootServletInitializer并实现configure方法: 这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似,只不过在这里不需要编写额外的XML文件了。

package com.zm.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication(scanBasePackages = "com.zm") @MapperScan("com.zm.dao") public class WebApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebApplication.class); } }

使用maven下的package进行打包,打包成功后将war包放在tomcat下webapps启动运行

在这里插入图片描述 注意:启动成功后访问 /项目名称/hello,比jar包部署方式多了访问项目名称

访问成功

在这里插入图片描述


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3