Jenkins 集成Maven打包SpringBoot项目并自动部署到Tomcat服务器

提前条件:

  1、在Jenkins服务器上安装Git、JDK和Maven

  2、准备另一台服务器并安装Tomcat

  3、Gitlab服务器

  4、Gitlab仓库中上传SpringBoot项目代码

第一步,Jenkins中配置JDK和Maven

1、配置JDK

2、配置Maven

第二步,在freestyle job中配置git和Maven

第三步,修改SpringBoot项目配置

1、项目入口类继承 SpringBootServletInitializer 并实现 configure方法

@SpringBootApplication
public class HotApplication  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HotApplication.class);
    }

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

}

2、Pom.xml排除tomcat容器

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

第四步,在Jenkins中构建SpringBoot项目

1、安装Publish over SSH插件

2、在“系统设置”中添加"Publish over SSH"主机,在高级选项中有用户名和密码配置

3、在构建中添加“Send files or execute commands over ssh”

4、所有配置以完成,点击该Job任务的“立即构建” 

第五步,验证效果

访问Tomcat服务器URL + 项目名称

总结:

1、Jenkins可以通过Web Hook监控gitlab中该项目git push动作,从而实现gitlab仓库代码更新后自动触发Jenkins构建;

2、如果是比较复杂的项目部署,Jenkins可以调用ansible,通过ansible执行playbook来完成更加复杂的自动部署。

原文地址:https://www.cnblogs.com/vincenshen/p/10490214.html