jenkins部署java項目,腳本文件放在遠程倉庫中 和jar一塊兒打包(六)

jenkins部署java項目到遠程linux上,腳本文件和項目一塊兒上傳到gogs上,直接執行gogs上的腳本文件來執行項目java

(1)新建maven項目linux

pom.xml的配置web

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

    <groupId>cn.demo</groupId>
    <artifactId>jenkins_jar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>jenkins_jar</name>
    <url>http://maven.apache.org</url>
  
    <build>
        <finalName>jenkins_jar</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <version>2.6</version>  
                <configuration>  
                    <archive>  
                    <!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 -->  
<!--                         <index>true</index> -->  
                        <manifest>  
                            <mainClass>cn.demo.jenkins_jar.demo.Demo</mainClass>  
                        </manifest>  
                       
                    </archive>  
                </configuration>  
            </plugin>  
            
             <plugin>
               <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>${project.version}</id><!--名字任意 -->
                        <phase>package</phase>   <!-- 綁定到package生命週期階段上 -->
                        <goals>
                            <goal>single</goal>   <!-- 只運行一次 -->   
                        </goals>
                        
                        <configuration>
                            <descriptors>   <!--描述文件路徑--> 
                                <descriptor>script.xml</descriptor>
                            </descriptors>
                            <!--這樣配置後,mvn deploy不會把assembly打的zip包上傳到nexus-->
                            <attach>false</attach>
                        </configuration>

                    </execution>

                </executions>

            </plugin>
</plugins>

</build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>    
        
</project>
View Code

>maven-assembly-plugin插件指定的 script.xml文件 (文件存放的位置:直接在項目下 (非src))shell

script.xmlapache

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
          http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>script</id>

    <formats><!--打包的文件格式 -->
        <format>zip</format>
    </formats>

    <fileSets>
        <fileSet>
            <directory>script</directory><!--須要打包的目錄 -->
            <outputDirectory>/</outputDirectory> <!-- 打包後輸出的路徑 輸出子啊target目錄下 -->
        </fileSet>
    </fileSets>

</assembly>
View Code

在該項目下直接新建文件夾 script ,在文件夾下新建腳本文件 start.sh  內容以下服務器

start.shmaven

#!/bin/sh
cd /root/home/program/pro_java/

#獲得進程ID pid,kill該進程
pid=`cat /root/home/program/pro_java/pid`
if [ -n "$pid" ]
then
    echo "kill -9 的pid:" $pid
    kill -9 $pid
fi

#執行jar,並將進程掛起,保存進程ID到 pid文件
echo "Execute shell Finish"
BUILD_ID=dontKillMe nohup java -jar /root/home/program/pro_java/jenkins_jar.jar  & echo "$!" > pid
View Code

 

 

(2)新建jenkins項目,選擇構建maven項目,開始配置ide

項目名稱:新建項目時,填的名稱post

描述:這裏是對項目的描述,解釋 本身定義就好了 不影響ui

 

源碼管理: Git

Repository URL:這裏填gogs的倉庫地址

Credential:這裏選擇gogs的登陸帳號和密碼(若是沒有就點擊右側的add按鈕,添加  只須要填寫 用戶名和密碼就能夠了)

 

構建觸發器(這裏的配置其實都沒什麼影響,隨便寫個令牌,只要選中了觸發遠程構建就好了,在gogs上對應的倉庫裏配置web鉤子就行)

使用web鉤子的做用:只有遠程代碼有push ,jenkins就會自動構建 (推薦使用)

也能夠不選擇觸發遠程構建,選中Poll SCM 配置 H/5 * * * * 表示每5分鐘檢查一次代碼,發現有變更就觸發構建 ,可是這種效率很差,不推薦

 

Build 執行構建pom.xml

執行 maven命令clean package,先clean項目,再從新打包編譯

Post Steps:這個勾中第一個就好了,表示構建成功時才運行

 

點擊 add-post build step

選擇 send files or execute commands over SSH

 

進入以下配置

SSH Server   ,

Name: root@172.150.12.32   //安裝publish over SSH插件,進行系統設置時的配置 Name名 

Source files: target/jenkins_jar.jar,target/jenkins_jar-script.zip  將項目的jar包傳送到服務器,將打包shell腳本的zip壓縮包傳給服務器  //表示須要傳送的文件,多個文件用逗號分開 

remove prefix:遠程linux須要移除的目錄名

Remote Directory:遠程linux接收這些文件的目錄

Exec Command:執行的腳本命令,命令以下

#!/bin/sh
cd /root/home/program/pro_java/   #進入遠程目錄

unzip -o -d /root/home/program/pro_java/ jenkins_jar-script.zip  #將存放腳本文件的壓縮包解壓

cd jenkins_jar   #打開解壓後的文件夾
sh start.sh  #執行該腳本   該腳本文件內容在文章開頭  (1)中

 

配置到此結束,能夠執行構建了

相關文章
相關標籤/搜索