【Jenkins教程三】基於Linux實現Jenkins+maven+git的自動化部署

前言

  • 上一篇教程詳細介紹了Jenkins+maven+svn+tomcat的自動化構建部署項目教程,這篇將介紹使用git爲版本控制系統的教程。

安裝Git

配置tomcat用戶

  • 在tomcat-user.xml配置用戶
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="password" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
  • 這個用戶在咱們構建好項目war包須要發佈到tomcat,而這個用戶就是咱們能夠發佈到tomcat的憑證。

配置pom.xml構建命令

  • 須要在pom.xml裏面配置項目構建命令,不然該Jenkins啓動不會報錯也不會發布到tomcat上
<packaging>war</packaging>

    <!--項目構建插件-->
    <build>
        <plugins>
            <!--編譯插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 修改webapp目錄爲web -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 設置WebContent目錄爲Web目錄 -->
                    <webappDirectory>${basedir}/web</webappDirectory>
                    <warSourceDirectory>${basedir}/web</warSourceDirectory>
                    <warName>demo</warName>
                </configuration>
            </plugin>
        </plugins>
        <!--資源-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 是否替換資源中的屬性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

插件安裝

  • 【發佈插件】Deploy to container Plugin
  • 【Git插件】Git plugin
  • 【構建Maven項目插件】Maven Integration plugin
  • 【本地化插件】Locale,這個須要到【系統設置】-【Default Language】輸入【zh-CN】並勾選Ignore browser preference and force this language to all users這個選項才能轉化爲中文

配置Git

  • 點擊【系統管理】-【全局工具配置】
  • 選擇Git-【新增Git】
  • 【Name】輸入自定義Git名稱
  • 【Path to Git executable】輸入Git安裝路徑 這裏寫圖片描述
不知道Git安裝路徑,執行 whereis git

詳細步驟

  • 在首頁點擊【新建】
  • 輸入項目名稱且選中【構建一個Maven項目】,點擊下方【肯定】 這裏寫圖片描述
  • 勾選【use Svn-Partial Release Manager】和【use Subversion Release Manager】,【丟棄舊的構建】 這裏寫圖片描述
  • 選擇【源碼管理】,勾選Git,【Repository URL】輸入Git項目的Https連接,不要ssh。【Credentials】輸入Git帳號密碼,沒有點擊右邊添加。 這裏寫圖片描述
  • 在【Build】,輸入【Root POM】不輸默認是根目錄下的pom.xml,若是pom.xml不在根目錄下面,則須要指定相對目錄,輸入【Goals and options】輸入構建須要執行的mvn命令。
  • 在【Post Step】,選擇【run regardless of build result】不管構建結果怎麼都執行 這裏寫圖片描述
  • 在【構建設置】勾選【E-mail Notification】,輸入通知郵件和相關選項
  • 在【構建後操做】,選擇【Deploy war/ear to a container】
  • 【WAR/EAR files】這裏根據你maven生成war包的名稱填寫,可是路徑前面必須加上target不然會構建不成功,格式爲【**/*.war】。而且不會產生任何錯誤,也不會部署項目到tomcat,這裏是須要進行注意的
  • 【Context path】是生成war包的名稱,若是是 /那麼就是ROOT.war 這裏寫圖片描述
  • 點擊【應用】,進入項目點擊【當即構建】
  • 查看【Console Output】,構建成功。 這裏寫圖片描述

後語

  • Git的教程和Svn的教程相差無幾,區別在於要配置Git和安裝相關插件以及和發佈配置那邊【Root Path】的配置區別。只要動手作一遍,就會發現該軟件的便利之處了。
相關文章
相關標籤/搜索