jenkins部署java項目到遠程linux(四)


(1)新建java項目(maven構建)

pom.xml

<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>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            -->
            <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>cn.demo.jenkins_jar.demo.Demo</mainClass>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                </transformers>
            </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

 

(2)新建jenkins項目

(3)下載插件  Publish over SSH

(4)配置publish over ssh (點擊系統管理 ---》  系統設置)

(5)進入系統配置頁面,前面的配置保持不變,下拉到Publish Over SSH配置

Passphrase:這里是你的主機生成 SSH key時的Passphrase值,如果你一直是enter ,這里就是空,什么都不用寫 ,空白就行

Path to key :這是寫主機 秘鑰的位置,記住是id_ras,而不是 id_ras.pub

key:如果你上面沒寫主機秘鑰 位置,這里就直接將秘鑰內容復制--粘貼過來

 

Name :這個名字你自己取 ,都可以  我這里取的是 用戶名@主機號.com

Hostname: 這里必須寫 主機IP

Username :是用戶名  一般為 root

 

(6)繼續如下配置:到這里配置完后,選擇 testConfiguration  ,沒寫錯 就會出現 success

 

(7)出現success后 表示配置成功,然后就可以開始進行項目的配置了,前面的配置不變 ,在增加構建后操作這里配置 選擇Send buiild artifacets over SSH

(8)進入配置

Name:是剛剛在系統設置里面Publish OVer SSH 配置的 name

Source Filew:項目jar包的位置,這是jenkins自動打包編譯的,都在 target目錄下

Remove prefix : 移除前綴 target

Remote directory : 遠程linux存放項目jar包的目錄

Exec Command:執行的shell腳本  #這里和部署到本地的腳本類似

腳本內容:

#!/bin/sh
cd /root/home/program/pro_java/  #進入項目jar包存放目錄

#得到進程ID pid,kill該進程
pid=`cat /root/home/program/pro_java/pid`  #得到該目錄下 pid文件中的進程id
if [ -n "$pid" ]
then
    echo "kill -9 的pid:" $pid
    kill -9 $pid    #kill該進程
fi

#執行jar,並將進程掛起,保存進程ID到 pid文件
echo "Execute shell Finish"
BUILD_ID=dontKillMe nohup java -jar /root/home/program/pro_java/jenkins_jar.jar  & echo "$!" > pid   #執行項目jar包,將進程掛起,然后將進程id寫入當前目錄下的pid文件中

然后就可以完成構建了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM