基于Maven构建SpringBoot项目并发布到外部tomcat

一、基于Maven构建SpringBoot项目

1、springboot版本

官网最新版本:2.6.1(https://spring.io/

当前使用版本:2.1.1

2、构建方式

(1)方式一:通过官网https://spring.io/进行springboot项目构建,然后在本地通过IDEA导入打开即可

(2)方式二:本地IDEA构建配置

a.新建maven项目

    选择JDK,确认包名项目名,选择maven仓库等

b.在pom.xml进行依赖配置

 1 pom.xml配置参考:
 2 <!--继承了SpringBoot框架的父级依赖-->
 3   <parent>
 4     <groupId>org.springframework.boot</groupId>
 5     <artifactId>spring-boot-starter-parent</artifactId>
 6     <version>2.1.1.RELEASE</version>
 7     <relativePath/> <!-- lookup parent from repository -->
 8   </parent>
 9 
10   <groupId>org.lkw</groupId>
11   <artifactId>SpringBootTest</artifactId>
12   <version>1.0-SNAPSHOT</version>
13   <name>SpringBootTest</name>
14   <description>Demo project for Spring Boot</description>
15 
16   <!--Maven属性配置-->
17   <properties>
18     <java.version>1.8</java.version>
19   </properties>
20 
21   <dependencies>
22     <!--SpringBoot框架开发web项目的起步依赖-->
23     <dependency>
24       <groupId>org.springframework.boot</groupId>
25       <artifactId>spring-boot-starter-web</artifactId>
26     </dependency>
27     <!--SpringBoot框架的起步测试依赖-->
28     <dependency>
29       <groupId>org.springframework.boot</groupId>
30       <artifactId>spring-boot-starter-test</artifactId>
31       <scope>test</scope>
32     </dependency>
33     <!-- springboot 开发自动热部署,另外还要在IDEA里作两个配置操作才可以使热部署生效:
34     1.勾上File-Settings-Compiler-Build Project automatically
35     2.ctrl + shift + alt + /,选择Registry,勾上 Compiler autoMake allow when app running
36     -->
37     <dependency>
38       <groupId>org.springframework.boot</groupId>
39       <artifactId>spring-boot-devtools</artifactId>
40       <optional>true</optional>
41     </dependency>
42   </dependencies>
43 
44   <build>
45     <plugins>
46       <!--SpringBoot的打包编译插件-->
47       <plugin>
48         <groupId>org.springframework.boot</groupId>
49         <artifactId>spring-boot-maven-plugin</artifactId>
50       </plugin>
51     </plugins>
52   </build>

c.创建SpringBoot项目启动类

 1 //SpringBoot启动类
 2 import org.springframework.boot.SpringApplication;
 3 import org.springframework.boot.autoconfigure.SpringBootApplication;
 4 
 5 
 6 @SpringBootApplication //springboot的全局的自动配置注解
 7 public class Application {
 8 
 9     public static void main(String[] args) {
10         // 固定的代码 启动springboot程序 初始化spring容器
11         SpringApplication.run(Application.class, args);
12     }
13 
14 }

d.创建控制器类进行测试

//控制类测试
@RestController
public class HelloController {

    @RequestMapping("/hello/getHello")
    public String getHello(){
        System.out.println("receive one request=>/hello/getHello");
        return "Hello SpringBoot!";
    }
}

e.运行项目,在浏览器输入ip:8080/hello/getHello(默认端口)进行测试即可

二、把项目打包并发布到外部tomcat上

1、打包配置过程

 

//(1)排除springboot自带的tomcat插件
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <!--(方法一)1.将springboot项目进行打包部署到外部tomcat时,需要将自带的tomcat插件进行排除-->
      <!--<exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>-->
</dependency>
<!--(方法二)1.将springboot项目进行打包部署到外部tomcat时,需要将自带的tomcat插件进行排除-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是打包阶段做了exclude操作-->
      <scope>provided</scope>
</dependency>

//(2)引入依赖javax.servlet-api
<!--2.servlet相关依赖-->
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
</dependency>

//(3)更改项目名和打包方式
<groupId>org.lkw</groupId>
<artifactId>SpringBootTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootTest</name>
<description>Demo project for Spring Boot</description>

<build>
    <!--打包war名称-->
    <finalName>SpringBootTest</finalName>
    <plugins>
      <!--SpringBoot的打包编译插件-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
</build>

//(4)创建SpringBootStartApplicatin类,SpringBootServletInitializer
//该类相当于spring里的web.xml,用于对serlvet、过滤器filter和监听器listener进行初始化
public class SpringBootStartApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //一定要指向springboot启动类,即@SpringBootApplication标记的类
        return builder.sources(Application.class);
    }
}

 

2、tomcat发布

(1)方式一:把war包置入webapps目录里

    直接把war包丢到webapps目录下,tomcat会对其自动解压,其相关编译文件会出现在work文件中,访问路径为ip:端口/项目名(执行优先级:第三)

(2)方式二:通过配置conf/servlet.xml,增加<Context>节点,使tomcat运行指定路径的web项目即外部的web项目,不推荐使用更改server.xml的方式,因为该文件为不可重载即更改后需要重启tomcat才生效(执行优先级:第一)

 

 1 <Host name="localhost"  appBase="webapps"
 2             unpackWARs="true" autoDeploy="true">
 3 
 4         <!-- SingleSignOn valve, share authentication between web applications
 5              Documentation at: /docs/config/valve.html -->
 6         <!--
 7         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
 8         -->
 9         #此<Context>为新增配置,path为指定外部访问项目名,docBase为项目实际路径,reloadable为true时当项目文件变动不需重启
10         <Context path="/B" docBase="/opt/bigdata/java/project/SpringBootTest" debug="0" reloadable="true"/> 
11 
12         <!-- Access log processes all example.
13              Documentation at: /docs/config/valve.html
14              Note: The pattern used is equivalent to using pattern="common" -->
15         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
16                prefix="localhost_access_log" suffix=".txt"
17                 pattern="%h %l %u %t &quot;%r&quot; %s %b" />
18 
19 </Host>

 

(3)方式三:在conf/Catalina/localhost下新增xxx.xml文件,在里面添加即可,不用重启,访问路径为ip:端口/xxx,如果xml命名为ROOT.xml则访问路径为ip:端口/(不需要带项目名)(执行优先级:第二)

#xxx.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/opt/bigdata/java/project/SpringBootTest" reloadable="true" />

 

3、FAQ(问题集锦)

 

(1)更改完conf/server.xml,执行./startup.sh不生效?

 

答:server.xml为不可重载资源,当tomcat服务已经启动的时候,则需要重启才能生效,即:先./shutdown.sh,再./startup.sh

 

posted @ 2022-01-03 20:30  chance_for_ready  阅读(251)  评论(0)    收藏  举报