[持续集成]Jenkins 自动化部署 Maven 工程

一、Jenkins 持续部署原理图

image

基础服务:

1 SVN 服务

     SVN是Subversion的简称,是一个开放源代码的版本控制系统。说得简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的。(源自百度百科)

2 Nexus 服务

     Maven的一种仓库软件。

3 Jenkins服务

     持续集成工具。

4 Web容器服务(Tomcat)

     部署web应用的容器

二、工程代码配置

1 配置文件

分别提供不同部署环境下的配置文件组(通常包括数据库配置、文件存储目录、缓存地址、中间件地址等)

src/main/resources

       distribute

                debug   ---------- 调试服务器配置文件夹

                      config.properties

                      spring-xxxx.xml

                      …

                test     -----------  测试服务器配置文件夹

                      config.properties

                      spring-xxxx.xml

                      …

                prod   ------------  生产服务器配置文件夹

                      config.properties

                      spring-xxxx.xml

                      …

      config.properties   ------------ 默认本地开发使用的配置文件(直接存放在 src/main/resources 根目录)

      spring-xxxx.xml

      …

2 pom.xml

分别配置不同部署环境下的profile,实现在编译打包时,根据部署环境不同,替换不同的配置文件

<project>

         <profiles>

             …(此处可配置不同环境下的profile)

         </profiles>

</project> 

示例: 调试profile 配置

<profile>
    <id>debug</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy todir="${basedir}/src/main/webapp/WEB-INF/classes/" overwrite="true">
                                    <fileset dir="${basedir}/src/main/resources/distribute/debug/" />
                                </copy>

                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>           
    </build>
</profile>

注:蓝色字体实现了调试服务器配置文件的拷贝覆盖。

三、SVN应用

1 开发人员代码上传

2 为jenkins 配置代码下载账号

四、Tomcat配置

1 配置Tomat 角色 和 用户,用以实现远程部署

  ${Tomcat_home}/conf/tomcat-user.xml,增加角色和用户

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin" roles="manager-gui,manager-script"/>

五、Jenkins 构建项目配置

1 系统配置

系统管理-> Global Tool Configuration

jdk

maven

2 插件配置

系统管理-> 管理插件

安装部署插件:Deploy to container Plugin

安装版本插件:Subversion Plug-in

3 项目配置 -- 新建项目

应为不同部署环境,建立不同的Jenkins项目,分别配置不同的buiid 命令 和 不同的部署容器

(1)输入项目名称

(2)选择构建一个Maven项目

(3)SVN配置

         输入 Repository  URL

         Add Credentials 并选择(SVN 账号密码,推荐使用为Jenkins开通的账号

(4)Build

         Root POM: pom.xml

         Goal and options : clean install -U -Pdebug  (此处使用调试服务器配置进行编译打包,-P后单词应对应pom.xml 中 profile 的 id)

(5)构建后操作

        增加 deploy war/ear to a container

        WAR/EAR files : **/target/*.war

        containers :  TomcatN.x

                               Manager user name : admin     (此处配置应与tomcat 配置的用户一致)

                               Manager password : admin

                               Tomcat URL : http://IP:PORT/ (此处只应配置到端口号)

(6)保存,然后立即构建,可查看构建日志,根据构建日志,修正错误,直至显示

Finished: SUCCESS
 
至此,Maven项目可以实现通过Jenkins一键部署到不同服务器。 
 
 
原文地址:https://www.cnblogs.com/driftingshine/p/6673023.html