SpringBoot打包为war包并部署到Tomcat服务器

SpringBoot打包为war包并部署到Tomcat服务器

从打包到部署花费了两天时间,特此记录一下!

目录

1、 打包为war、jar包的区别

2、 SpringBoot打包为war的过程

3、 修改Tomcat配置并部署项目

一、打包为war、jar包的区别

jarwar
包中内容class、properties文件,是文件封装的最小单元;包含Java类的普通库、资源(resources)、辅助文件(auxiliary files)等Servlet、JSP页面、JSP标记库、JAR库文件、HTML/XML文档和其他公用资源文件,如图片、音频文件等
区别通常是开发时要引用通用(JAVA)类,打成包便于存放管理;是做好一个(web)应用后,通常是网站,打成包部署到容器中

jar和war何时使用

当你的项目在没有完全竣工的时候,不适合使用war文件,因为你的类会由于调试之类的经常改,这样来回删除、创建war文件很不爽,最好是你的项目已经完成了,不改了,那么就打个war包吧,这个时候一个war文件就相当于一个web应用程序;而jar文件就是把类和一些相关的资源封装到一个包中,便于程序中引用

二、SpringBoot打包为war的过程

1、将打包方式修改为war包

(1)找到项目中的pom.xml文件

(2)将war代码插入到pom.xml文件里面

<packaging>war</packaging>

2、去除SpringBoot内置Tomcat服务器

在pom.xml文件中添加依赖用于去除内置的Tomcat服务器

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

3、设置war名称

因为使用外部Tomcat部署访问的时候,application.properties(或者application.yml)中配置的

server.port=

server.servlet.context-path=

将失效

为了防止应用上下文所导致的项目访问资源加载不到的问题

建议设置war名称

在pom.xml文件中在<build></build>里面插入<finalName></finalName>标签
里面的war包名称自己起一个

    <build>
        <finalName>war名称</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

注意: 要是不设置war名称打包过程中maven也会给你自动生成名称但是名称会很长

4、修改启动类

这里我们需要继承SpringBootServletInitializer类重写configure方法,用于生成war包需要的相关文件

public class 启动类 extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
        return builder.sources(启动类.class);
    }
}

5、生成war包(跳过测试)

mvn clean package  -Dmaven.test.skip=true

6、生成的war包在项目的target文件夹里面

三、修改Tomcat配置并部署项目

注意:要是不修改Tomcat配置访问项目URL地址为

127.0.0.1:8080/war名称

修改了配置之后访问项目的URL地址为

127.0.0.1:8080

1、将打包的war放入Tomcat的webapps目录中

2、找到Tomcat服务器里面的conf目录里面的server.xml文件

找到如下代码

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
		
		<Context path="/" docBase="D:\apache-tomcat-9.0.29\webapps\one"  reloadable ="true" debug="0" privileged="true">
		</Context> 
		
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

      </Host>

将如下代码插入到<Host></Host>l里面

	<Context path="/" docBase="D:\apache-tomcat-9.0.29\webapps\one"  reloadable ="true" debug="0" privileged="true">
	
	</Context> 

如下:

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
		<!--代码插入-->
		<Context path="/" docBase="D:\apache-tomcat-9.0.29\webapps\one"  reloadable ="true" debug="0" privileged="true">
		</Context> 
		
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

      </Host>

只有修改了Tomcat的配置才能在访问项目时前后访问资源报404问题

注意: 也可以不修改配置 只不过你要把项目的所有接口在前面加上war包名称

例如:

没有打包之前运行项目

<a href="index.html">进入首页</a>

打包之后不修改Tomcat配置

<!--这里会报404的-->
<a href="index.html">进入首页</a> 

<!--你得加上war名称才能正常访问-->
<a href="/war名称/index.html">进入首页</a>

打包之后修改了Tomcat配置

<!--修改了配置就能直接访问-->
<a href="index.html">进入首页</a> 

到此启动Tomcat里bin目录下的startup.bat文件,访问127.0.0.1:8080就能访问项目了

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值