欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot war包部署到Tomcat服务器

程序员文章站 2023-01-03 17:09:46
(1)pom.xml文件修改war,默认是jar包,节点中增加

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

<?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,如下:

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以上))

jdk安装参考://www.jb51.net/linuxjishu/66536.html

tomcat安装参考:

(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

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