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

1、Jenkins 持续部署原理图

image

基础服务:

1 SVN 服务web

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

2 Nexus 服务数据库

     Maven的一种仓库软件。apache

3 Jenkins服务缓存

     持续集成工具。tomcat

4 Web容器服务(Tomcat)服务器

     部署web应用的容器app

2、工程代码配置

1 配置文件

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

src/main/resourceswebapp

       distributemaven

                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>

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

3、SVN应用

1 开发人员代码上传

2 为jenkins 配置代码下载帐号

 

4、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"/>

 

5、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一键部署到不一样服务器。 
相关文章
相关标签/搜索