[持續集成]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一鍵部署到不一樣服務器。 
相關文章
相關標籤/搜索