服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot war包部署到Tomcat服务器

SpringBoot war包部署到Tomcat服务器

2021-04-18 14:15gdpuzxs Java教程

这篇文章主要介绍了SpringBoot war包部署到Tomcat服务器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

(1)pom.xml文件修改<packaging>war</packaging>,默认是jar包,<build>节点中增加<finalname>springboot</finalname>,即生成war包的名字,完整pom.xml文件内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>springboot</groupid>
  <artifactid>springboot</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>war</packaging>
 
  <name>springboot</name>
  <description>demo project for spring boot</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.4.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.7</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter</artifactid>
      <exclusions>
        <exclusion>
          <groupid>org.springframework.boot</groupid>
          <artifactid>spring-boot-starter-logging</artifactid>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-thymeleaf</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-log4j</artifactid>
      <version>1.3.8.release</version>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-redis</artifactid>
    </dependency>
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
    </dependency>
    <dependency>
      <groupid>org.mybatis.spring.boot</groupid>
      <artifactid>mybatis-spring-boot-starter</artifactid>
      <version>1.1.1</version>
    </dependency>
  </dependencies>
 
  <build>
    <finalname>springboot</finalname>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

(2)修改项目启动类,继承springbootservletinitializer,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package springboot;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.builder.springapplicationbuilder;
import org.springframework.boot.web.support.springbootservletinitializer;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.scheduling.annotation.enablescheduling;
 
@springbootapplication
@enablescheduling
@enablecaching
public class springbootapplication extends springbootservletinitializer{
 
  public static void main(string[] args) {
    springapplication.run(springbootapplication.class, args);
  }
  @override
  protected springapplicationbuilder configure(springapplicationbuilder application) {
    return application.sources(springbootapplication.class);
  }
}

(3)打包:可以通过eclipse run as ->maven install,也可以进入项目的根目录,也就是pom.xml同一级目录,启动cmd控制台,执行mvn install package,如下:

SpringBoot war包部署到Tomcat服务器

(4)环境搭建(linux环境jdk安装与tomacat安装(springboot支持tomcat7以上))

(5)将打包好的war包上传至tomcat目录下webapp目录下,启动tomcat服务器。

(6)访问项目路径:http://ip地址:端口号/项目打包名称/方法名(项目打包名称即pom.xml中的<finnalname>的值)

(7)设置tomcat开机自动启动

  (1)修改脚本文件rc.local:vim /etc/rc.d/rc.local

  (2)在rc.local中增加:export java_home = jdk安装路径 ,tomcat安装路径/bin/startup.sh start

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/gdpuzxs/p/7224959.html

延伸 · 阅读

精彩推荐