添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Tomcat部署SpringBoot项目的war包的方法
一、环境
1.新建系统变量名: CATALINA_HOME,变量值:D:\Development\apache-tomcat-8.5.20 ; (CATALINA_HOME中的变量值为你tomcat的安装路径,不包括\bin)
2.在系统变量Path中添加:%CATALINA_HOME%\bin。(;分号结尾,第一个%前面若没有分号,分号是英文状态下输入的)

简介:

1.Spring Boot使用了内嵌容器,因此它的部署方式也变得非常简单灵活,可以将Spring Boot项目打包成JAR包来独立运行,也可以打包成WAR包部署到Tomcat容器中运行,如果涉及大规模的部署, Jenkins 成为最佳选择之一。
生成jar包

Maven默认会将项目打成JAR包,也可以在pom.xml文件中指定打包方式。配置示例如下:

生成war包

Spring Boot项目既可以生成WAR包发布,也可以生成JAR包发布。那么它们有什么区别呢?
JAR包:通过内置Tomcat运行,不需要额外安装Tomcat。如果需修改内置Tomcat的配置,只需要在Spring Boot的配置文件中配置即可。内置Tomcat没有自己的日志输出,全靠JAR包应用输出日志,但是部署简单方便,适合快速部署。
WAR包:传统的应用交付方式,需要安装Tomcat,然后将WAR包放到webapps目录下运行,这样可以灵活选择Tomcat版本,也可以直接修改Tomcat的配置,同时有自己的Tomcat日志输出,可以灵活配置安全策略。WAR包相对JAR包来说没那么快速方便。

排除tomcat

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

在上面的示例中,将Tomcat组件的scope属性设置为provided,这样在打包产生的WAR中就不会包含Tomcat相关的JAR。

注册启动类

@SpringBootApplication
public class PackageApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(PackageApplication.class);
        public static void main(String[] args) {
                SpringApplication.run(PackageApplication.class, args);


在项目根目录下,在控制台执行如下命令:
mvn clean package -Dmaven.test.skip=true
1)mvn clean package其实是两条命令,mvn clean用于清除项目target目录下的文件,mvn package是打包命令。两个命令可以一起执行。
2)-Dmaven.test.skip=true:排除测试代码后进行打包。
将打包好的WAR包复制到Tomcat服务器中的webapps目录下启动即可。

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>net.say</groupId>
        <artifactId>sss</artifactId>
        <version>0.0.1</version>
    </parent>
    <groupId>net.say</groupId>
    <artifactId>backend</artifactId>
    <version>1.0</version>
    <name>backend</name>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.mapsay</groupId>
            <artifactId>core</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <!-- OKHttp3依赖 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.8.0</version>
        </dependency>
        <!-- 钉钉开放平台-推送消息 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dingtalk</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- Initialized JPA EntityManagerFactory 初始化成功关键,注意, lt 2023.06.10 ,否则报错:Error creating bean with name 'entityManagerFactory' defined in class path resource  或 Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.23.1-GA</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--Tomcat 启动报错>> Application run failed-->
        <!--org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration-->
        <!-- hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>
        <!-- validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
</project>

使用心得:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
六月 13, 2023 11:27:59 上午 org.springframework.boot.SpringApplication reportFailure
严重: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
  

在启动tomcat时报上面错误,无法启动,则考虑是tomcat版本问题,尝试用绿色版的tomcat 8.0 + 进行启动得于解决;

二、Tomcat启动war包

1.打包位置:启动后生成的war包(target包下),复制到tomcat目录下,记得设置tomcat的访问端口号。

2.机器下载的tomcat 8.5xx 压缩包,解压安装。
(1) 编辑 tomcat 配置文件设置 访问端口为 项目配置文件中配置的端口号(保持访问端口不变)
(2) 将打包好的 war 包复制到 tomcat/webapps 目录下
(3) 启动 tomcat:bin/startup.xx
(4) 启动成功,访问接口–>> 404
可能存在的问题: 项目在intellij idea里配置tomcat可以启动访问, 打成war包丢到tomcat webapps下能启动却访问不了相关的接口, 这个问题是因为idea会默认将项目以ROOT为目录的文件,而丢到tomcat的webapps下面则是解压成你项目名称为目录的文件,和ROOT是同级的


可以有以下几种解决方案
一:将war包放在Tomcat的ROOT包下(或者将你的war名称改成作为ROOT.war)

二:在tomcat的server.xml文件的Host标签内配置

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true"  autoDeploy="false">
             <Context Path="" docBase="/ROOT/" debug="0" reloadable="true" />

      //<Context path="/" docBase="你项目的地址" reloadable="true"/>
<Context path="/car" docBase="C:\Users\Admin\Desktop\apache-tomcat-8.5.54\webapps\ROOT\JavaWebTest.war"  reloadable="true"></Context>

三、设置Tomcat默认访问路径
tomcat默认访问路径为 http://localhost:8080/项目名/页面等路径,有些客户要求只输入IP,自动进入项目登录页面,设置如下:
修改tomcat/conf/server.xml
在标签中加入:

<Context path="" docBase="E:\tomcat\webapps\项目文件夹名称" debug="0" reloadable="true" />

path:war包解压后的项目名称,也就是访问路径(例:http://localhost:8080/car/)
docBase:war包的路径(需要把war包的后缀名加上,如上图所示,避免报错)
reloadable:自动加载
保存上述修改后,双击\bin\startup.bat启动Tomcat即可
三:前端访问不到后端接口问题
用tomcat发布时,将前端请求的路径加上你的项目名称
正常启动后,地址栏输入localhost:8080会进入tomcat路径,ROOTweb.xml指定的index页面。
所以要在地址栏输入localhost:8080/war包名称/ 就会进入tomcat路径中自己的文件夹。(该文件夹就是自己war被解析后生成的)

2.启动中文乱码问题:
启动Tomcat后,产生的日志有乱码的情况出现,不要慌,这并不影响你项目的正常运行。这是由于编码不一致引起的,Tomcat启动后默认编码UTF-8,而Windows的默认编码是GBK。因此你想让其不乱码,只需配置conf\logging.properties的编码格式即可

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>net.mapsay</groupId>
        <artifactId>szt</artifactId>
        <version>0.0.1</version>
    </parent>
    <groupId>net.mapsay</groupId>
    <artifactId>backend</artifactId>
    <version>1.0</version>
    <name>backend</name>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.mapsay</groupId>
            <artifactId>core</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <!-- OKHttp3依赖 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.8.0</version>
        </dependency>
        <!-- 钉钉开放平台-推送消息 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dingtalk</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- Initialized JPA EntityManagerFactory 初始化成功关键,否则报错:Error creating bean with name 'entityManagerFactory' defined in class path resource  或 Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.23.1-GA</version>
        </dependency>
    </dependencies>
    <!--<build>-->
        <!--<plugins>-->
            <!--<plugin>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
                <!--<configuration>-->
                    <!--&lt;!&ndash; 热部署 &ndash;&gt;-->
                    <!--<fork>true</fork>-->
                    <!--<executable>true</executable>-->
                    <!--&lt;!&ndash; jvm 运行参数,优化性能 &ndash;&gt;-->
                    <!--<jvmArguments></jvmArguments>-->
                <!--</configuration>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</build>-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
</project>

 <groupId>net.mapsay</groupId> <artifactId>backend</artifactId> <version>1.0</version> <name>backend</name> <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>

,这二段是关键。,,,,,


apache-tomcat-8.5.83_8011-jar_to_war\conf\server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8011" protocol="HTTP/1.1"  URIEncoding="utf-8"
               connectionTimeout="20000"
               redirectPort="8443" />    
<Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="false"> <Context Path="" docBase="/ROOT/" debug="0" reloadable="true" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host> </Engine> </Service> </Server>