Jenkins和maven自動化構建java程式

iTech發表於2016-02-16

 

轉自:http://www.cnblogs.com/gao241/archive/2013/04/08/3008380.html,版權歸原作者所有。

Jenkins是一個非常出色的持續整合伺服器,本文主要介紹在CentOS系統中Jenkins的基本安裝配置方法,供參考。

一. 軟體包:

1. 下載apache-maven-2.2.1-bin.tar
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-2.2.1-bin.tar.gz
2. 下載jenkins:http://mirrors.jenkins-ci.org/war/ 列出了jenkins的所有release版本,請選擇下載

二. 安裝

 

本文假設操作使用者為gistech。

1. 安裝jdk,參考《CentOS系統中安裝JDK1.6》
2. 安裝tomcat7,參考《CenOS系統中安裝Tomcat7》

修改tomcat配置檔案

$ vi /home/gistech/tomcat/conf/server.xml

 

修改8080埠的Connector節點,增加如下配置

URIEncoding="UTF-8"

 

3. 安裝maven
1) 解壓

$ tar -zvxf apache-maven-2.2.1-bin.tar.gz

 

2) 移動到其他目錄

$ mv apache-maven-2.2.1 /home/gistech/maven

3) 配置環境變數
編輯.bash_profile檔案

$ cd /home/gistech
$ vi .bash_profile

 

新增如下內容

MAVEN_HOME=$HOME/maven
PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin

export MAVEN_HOME
export PATH

 

使環境變數生效

$ source .bash_profile

 

4) 驗證是否安裝成功

$ mvn -v   

 

5) 配置maven
編輯maven的配置檔案

$ cd maven/conf
$ vi settings.xml

 

修改配置檔案示例如下:

複製程式碼
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>/home/gistech/~m2</localRepository>
    <servers>
        <server>
            <username>admin</username>
            <password>password</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <!-- This sends everything else to /public -->
            <id>nexus</id>
            <mirrorOf>central</mirrorOf>
            <url>http://192.168.120.247:8081/nexus/content/repositories/public</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <!-- Enable snapshots for the built in central repo to direct -->
            <!-- all requests to nexus via the mirror -->
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://192.168.120.247:8081/nexus/content/repositories/public</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://192.168.120.247:8081/nexus/content/repositories/public</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <!-- make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

    <pluginGroups>
        <!-- define the sonatype plugin group, so the nexus plugins will work without 
            typing the groupId -->
        <pluginGroup>org.sonatype.plugins</pluginGroup>
    </pluginGroups>
</settings>
複製程式碼

 

其中localRepository節點表示私服中下載的jar包存放路徑,根據實際存放的路徑修改。
所有url節點中包含的地址:http://192.168.120.247:8081/nexus/content/repositories/public,表示nexus私服的地址,請根據實際的地址修改。
username、password節點表示登入nexus私服的使用者名稱及密碼。
4、安裝jenkins
1) 複製到tomcat webapps目錄

$ mv jenkins.war /home/gistech/tomcat/webapps

 

2) 配置環境變數
編輯.bash_profile檔案
新增如下內容

JENKINS_HOME=$HOME/tomcat/webapps/jenkins

export JENKINS_HOME

 

使環境變數生效

source .bash_profile

 

3) 啟動tomcat,使用http://localhost:8080/jenkins訪問jenkins

三、jenkins的基本配置

 1、全域性配置

單擊首頁左邊的“系統管理”,進入“系統設定”,在這裡對jenkins進行全域性設定
1) JDK安裝。如果沒有讀取出系統變數,就手動設定好別名和JAVA_HOME就可以了
2) Maven同理

3) 配置管理員郵件通知
配置方式如下圖:

配置完成後可以點選"通過傳送測試郵件測試配置"按鈕進行測試。
2、建立job
1) 在頁面中點選“新job”,輸入任務名稱,選擇“構建一個maven2/3專案”,如下圖:

2) 點選ok按鈕進入任務配置頁面,首先配置專案的svn地址,如下圖:

3) 配置maven的pom檔案位置,可以使用相對路徑(相對於svn地址庫中的位置),如下圖:

4) 配置maven編譯引數,如下圖所示不執行單元測試:

關於maven的編譯引數可參考Maven基本命令
3、郵件通知
構建郵件通知配置如下圖,Recipients欄目中新增需要傳送郵件通知的郵件地址。

4、定時構建
構建觸發器可以配置構建的時間,如果需要定時構建,可以選擇 Build periodically,日程表引數解釋如下:

  • 第一個引數代表的是分鐘 minute,取值 0~59;
  • 第二個引數代表的是小時 hour,取值 0~23;
  • 第三個引數代表的是天 day,取值 1~31;
  • 第四個引數代表的是月 month,取值 1~12;
  • 最後一個引數代表的是星期 week,取值 0~7,0 和 7 都是表示星期天。

如0 * * * * 表示的就是每個小時的第 0 分鐘執行一次構建。

5、版本更新構建
很多情況需要jenkins定時輪詢版本庫,只要有版本更新就觸發構建,jenkins提供了Poll SCM支援版本更新構建,如上圖,配置引數與定時構建的引數一樣,如*/5 * * * *表示每隔5分鐘輪詢一次,檢查版本庫是否更新。

相關文章