手把手教你搭建Jenkins實現自動化部署Jar


centeros7 安裝配置環境jdk1.8

1.先卸載centeros自帶jdkcss

rpm -qa | grep openjdk

查詢出來的自帶的openjdkjava

2.刪除node

rpm -e --nodeps 

3.再次查詢linux

rpm -qa | grep jdk  


4.在usr/local建立文件夾
nginx

cd /usr/local
mkdir java


5.將jdk1.8放到剛剛建立的文件中
git

cd  /usr/local/java

用xftp上傳後解壓web

tar -zxvf  jdk-8u181-linux-x64.tar.gz


6.進入剛剛解壓的文件夾面試

cd jdk1.8.0_181/
pwd      #(這一步記錄jdk1.8的路徑,一會配置環境用)


7.配置環境
spring

vim /etc/profile

按i插入下面路徑sql

export JAVA_HOME=/usr/local/java/jdk1.8.0_181
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

按Esc後:wq退出後

source /etc/profile


8.查看是否成功

java -version


maven 環境配置

1.在/usr/local 下建立maven文件夾

cd /usr/local
mkdir maven


2.進去新建的文件夾

cd maven

上傳 apache-maven-3.5.4-bin.zip

3.解壓

unzip apache-maven-3.5.4-bin.zip


4.進入解壓的文件夾

cd apache-maven-3.5.4/pwd   #(記錄下這個路徑,一會配置環境用到)

5.配置環境

vim /etc/profile

按i插入下面

export MAVEN_HOME=/usr/local/maven/apache-maven-3.5.4
export PATH=$PATH:$MAVEN_HOME/bin

按Esc後:wq保存後

source /etc/profile


6.查看是否成功

mvn -v 


centeros7安裝git

yum install git

一路下來y便可

查看是不是成功

git --version


查看git命令地址

which -a git

到此環境已經搭建好,開始對maven私服,gitlab搭建。

gitlab搭建

1.新建 /etc/yum.repos.d/gitlab-ce.repo

vim /etc/yum.repos.d/gitlab-ce.repo

後按下 i 鍵 插入下面內容

[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

2.

sudo yum makecache

sudo yum install gitlab-ce 
#(安裝最新的)      或   sudo yum install gitlab-ce-12.0.4 #(指定版本號)

3.配置 external_url

vim /etc/gitlab/gitlab.rb

修改external_url地址 爲: http://本機地址:端口號/

本機地址查看:ifconfig

查看端口號運行的應用 lsof -i:端口號

4.

gitlab-ctl reconfigure

5.

gitlab-ctl restart #啓動        

gitlab-ctl start|stop|status|restart # 其餘操做

6.訪問

http://本機地址:端口號/

常見問題 gitlab502 : 這個是端口被佔用

確保端口不被佔用後從4開始繼續來一遍

centeros7 搭建maven私服

下載nexus3.x:

https://www.sonatype.com/download-oss-sonatype

1.在/usr/local下建立nexus文件

cd /usr/local
mkdir nexus
cd nexus


2.上傳剛剛下載的nexus壓縮包並解壓

tar -zxvf nexus-3.18.1-01-unix.tar.gz 


3.

cd nexus-3.18.1-01/bin/
ls


4.啓動

./nexus run &

5.開啓遠程訪問端口

firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload


瀏覽器訪問

6.設置開機自啓動

1.建立服務

vim /usr/lib/systemd/system/nexus.service

2.按i將下面複製進去

[Unit] 
Description=nexus service

[Service]
 
Type=forking LimitNOFILE=65536 #警告處理
ExecStart=/usr/local/nexus/nexus-3.7.1-02/bin/nexus start
ExecReload=/usr/local/nexus/nexus-3.7.1-02/bin/nexus restart
ExecStop=/usr/local/nexus/nexus-3.7.1-02/bin/nexus stop
Restart=on-failure

[Install]

WantedBy=multi-user.target

3.將服務加入開機啓動

systemctl enable nexus.service

4.從新加載配置文件

systemctl daemon-reload


7.運行用戶爲root (編輯nexus bin下的nexus.rc)

cd /usr/local/nexus/nexus-3.18.1-01/bin/vim nexus.rc

內容改成:

run_as_user="root"

8.修改nexus3啓動時要使用的jdk版本

cd /usr/local/nexus/nexus-3.18.1-01/bin/
vim nexus

按i將下面添加進去

INSTALL4J_JAVA_HOME_OVERRIDE=/usr/local/java/jdk1.8.0_181   


java_home地址能夠用echo $JAVA_HOME 輸出 (前提是/etc/profile有export)

9.修改nexus3默認端口

文件在nexus-xx

etc/nexus-default.properties



這裏已經搭建好了,下面是deploy到私服和從私服上下載下來測試

1.建立facade 上傳到私服

在/usr下建立projects

cd  /usr
mkdir projects

將改zip解壓到projects進入到pom.xml所在目錄

mvn deploy -Dmaven.test.skip=true -U


已經上傳成功

這裏要把Allow anonymous users to access the server(容許匿名用戶訪問服務器)

由於本地maven沒有配置用戶

也能夠用idea maven命令depoly

<?xml version="1.0" encoding="UTF-8"?>
<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>com.netease</groupId>
    <artifactId>maven_test_demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- 定義常量 -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
        <nexus.host>192.168.20.143:8081</nexus.host>
    </properties>

    <!-- 定義deploy私服地址及id (這裏的id 應該和maven conf中settings中server標籤中的一致)-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Private Release Repository</name>
            <url>http://${nexus.host}/repository/maven-releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Private Snapshot Repository</name>
            <url>http://${nexus.host}/repository/maven-snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <!-- 定義編譯: https://blog.csdn.net/jiachunchun/article/details/90235498 -->
    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

settings.xml中servers中定義的id一致

在settings 添加下面鏡像

    <mirror>
        <id>Central</id>
        <url>http://repo1.maven.org/maven2</url>
        <mirrorOf>central</mirrorOf>
    </mirror>

最後settings文件。到這兒jar包已經能夠發佈到私服上去了

再看怎麼拉取的

maven_demo_impl項目的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.netease</groupId>
    <artifactId>maven_demo_impl</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 定義常量 -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
        <nexus.host>192.168.20.143:8081</nexus.host>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <!-- 定義倉庫地址 -->
    <repositories>
        <repository>
            <id>nexus-aliyun-repo</id>
            <name>nexus aliyun repo</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
        <!-- 私服 -->
        <repository>
            <id>maven-public</id>
            <name>maven public</name>
            <url>http://192.168.20.143:8081/repository/maven-public/</url>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 依賴 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netease</groupId>
            <artifactId>maven_test_demo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>

</project>

配置完成後就會自動去私服下載jar了

jenkins搭建

1.查看環境版本

java -version  #沒有的話查看上文jdk1.8安裝
mvn -v   #沒有的話查看上文maven安裝
git --version  # 沒有的話查看上文git安裝

2.安裝jenkenis

添加Jenkins源:

sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key

3.安裝

yum install jenkins

4.啓動

sudo service jenkins start

5.訪問

http://ip:8080/             #jenkenis默認是8080

第一次密碼:

vim /var/lib/jenkins/secrets/initialAdminPassword

點安裝推薦的插件(後面的選擇安裝不知道安裝什麼)

修改jenkins端口

vim /etc/sysconfig/jenkins

將JENKINS_PORT="8080" ===> 修改爲未佔用想修改的端口 JENKINS_PORT="8089"

重啓服務

service jenkins restart

遇到的問題:重裝了JDK, java地址變了

啓動jenkins報錯: Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.

按提示輸入systemctl status jenkins.service

解決方法:

1> 查看當前Java的環境變量

echo $JAVA_HOME

2> 複製Java的環境變量地址, 編輯/etc/init.d/jenkins文件, 指定位置添加該地址, 後綴附上/bin/java

vim /etc/init.d/jenkins


重點是配置Jenkins:

1.配置全局jdk maven git

點Manage Jenkins(系統管理) --> Global Tool Configuaration(全局工具配置)

1.maven配置:配置settings.xml地址

2.配置jdk地址 (能夠用echo $JAVA_HOME獲取)

3.git配置地址 能夠用(which -a git查看)

4.maven配置地址

2.新建任務

3.配置任務

1.配置源碼管理 下面version是gitlab的版本號

Repository URL:訪問的倉庫名稱
Credentials:訪問帳目密碼或者ssh證書
Branch Specifier (blank for ‘any’):訪問的分支,能夠多個
源碼庫瀏覽器:選擇源碼庫服務器
URL:源碼倉庫url
Version:源碼庫服務器版本號

2.配置構建

clean compile package install deploy -Dmaven.test.skip=true -U #將jar發佈


而後應用,配置完成!

3.在my view(個人視圖) -> 找到新建的項目 --> 點擊build now(當即構建)

在構建歷史中點擊剛剛的構建以下圖: #1 : 是構建的id ,控制檯輸出: 能夠看到具體的輸出

下面是輸入日誌 能夠看到之前拉取到指定的項目master分支

Started by user root
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/first
using credential fad2813a-c894-4386-ae16-11d6e6d349d4
Cloning the remote Git repository
Cloning repository http://192.168.20.137:9029/root/wx-ssm.git
> /usr/local/git/bin/git init /var/lib/jenkins/workspace/first # timeout=10
Fetching upstream changes from http://192.168.20.137:9029/root/wx-ssm.git
> /usr/local/git/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials
/usr/local/git/bin/git fetch --tags --progress http://192.168.20.137:9029/root/wx-ssm.git +refs/heads/*:refs/remotes/origin/*
/usr/local/git/bin/git config remote.origin.url http://192.168.20.137:9029/root/wx-ssm.git # timeout=10
/usr/local/git/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/# timeout=10
/usr/local/git/bin/git config remote.origin.url http://192.168.20.137:9029/root/wx-ssm.git # timeout=10
Fetching upstream changes from http://192.168.20.137:9029/root/wx-ssm.git
using GIT_ASKPASS to set credentials
/usr/local/git/bin/git fetch --tags --progress http://192.168.20.137:9029/root/wx-ssm.git +refs/heads/*:refs/remotes/origin/*
> /usr/local/git/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> /usr/local/git/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 47d13c3ca0bc12fc0465c3e4c582b2c542276eb6 (refs/remotes/origin/master)
> /usr/local/git/bin/git config core.sparsecheckout # timeout=10
> /usr/local/git/bin/git checkout -f 47d13c3ca0bc12fc0465c3e4c582b2c542276eb6
Commit message: "更新 pom.xml"
First time build. Skipping changelog.
[first] $ /project/java/maven/apache-maven-3.6.1/bin/mvn -s /project/java/maven/apache-maven-3.6.1/conf/settings.xml -gs /project/java/maven/apache-maven-3.6.1/conf/settings.xml clean package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.guojin:gjmetal-wx-ssm:jar:1.0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.httpcomponents:httpclient:jar -> version (?) vs 4.5.6 @ line 155, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ---------------------< com.guojin:gjmetal-wx-ssm >----------------------
[INFO] Building gjmetal-wx-ssm 1.0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ gjmetal-wx-ssm ---
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.9:prepare-agent (pre-test) @ gjmetal-wx-ssm ---
[INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=/var/lib/jenkins/workspace/first/target/jacoco.exec,includes=com.gjmetal.*
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gjmetal-wx-ssm ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ gjmetal-wx-ssm ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 27 source files to /var/lib/jenkins/workspace/first/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /var/lib/jenkins/workspace/first/src/main/java/com/gjmetal/Swagger2.java:[4,40] 程序包org.fuelteam.springboot.swagger2不存在
[ERROR] /var/lib/jenkins/workspace/first/src/main/java/com/gjmetal/Swagger2.java:[21,2] 找不到符號
  符號: 類 EnableSwagger2
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.183 s
[INFO] Finished at: 2019-08-23T17:05:09+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project gjmetal-wx-ssm: Compilation failure: Compilation failure:
[ERROR] /var/lib/jenkins/workspace/first/src/main/java/com/gjmetal/Swagger2.java:[4,40] 程序包org.fuelteam.springboot.swagger2不存在
[ERROR] /var/lib/jenkins/workspace/first/src/main/java/com/gjmetal/Swagger2.java:[21,2] 找不到符號
[ERROR]   符號: 類 EnableSwagger2
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

上面報錯是項目問題,能夠看到已經拉取到項目了。因此要保證項目的正確性。

下面是正確的兩個項目運行實例

在搭建好gitlab nexus jenkins後實現自動發jar到私服及啓動jar項目

背景: gitlab nexus jenkins都已經完成了

1.從gitlab下載項目後發佈jar到私服

從新配置gitlab地址:

配置maven命令:

clean compile package install deploy -Dmaven.test.skip=true -U


點擊build now 而後看控制檯輸出以下

Started by user root
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/maven_home
using credential fad2813a-c894-4386-ae16-11d6e6d349d4
> /usr/local/git/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /usr/local/git/bin/git config remote.origin.url http://192.168.20.143:8083/root/maven_demo.git # timeout=10
Fetching upstream changes from http://192.168.20.143:8083/root/maven_demo.git
> /usr/local/git/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials
> /usr/local/git/bin/git fetch --tags --progress http://192.168.20.143:8083/root/maven_demo.git +refs/heads/*:refs/remotes/origin/*
> /usr/local/git/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> /usr/local/git/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a26adc740d331eabe5b0fb2a46b372dbf6673d2c (refs/remotes/origin/master)
> /usr/local/git/bin/git config core.sparsecheckout # timeout
=10
> /usr/local/git/bin/git checkout -f a26adc740d331eabe5b0fb2a46b372dbf6673d2c
Commit message: "Update pom.xml"
> /usr/local/git/bin/git rev-list --no-walk a26adc740d331eabe5b0fb2a46b372dbf6673d2c # timeout=10
[maven_home] $ /project/java/maven/apache-maven-3.6.1/bin/mvn -s /project/java/maven/apache-maven-3.6.1/conf/settings.xml -gs /project/java/maven/apache-maven-3.6.1/conf/settings.xml clean compile package install deploy -Dmaven.test.skip=true -U
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.netease:maven_test_demo >---------------------
[INFO] Building maven_test_demo 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_test_demo ---
[INFO] Deleting /var/lib/jenkins/workspace/maven_home/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_test_demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven_home/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_test_demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /var/lib/jenkins/workspace/maven_home/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_test_demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven_home/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_test_demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven_test_demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ maven_test_demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven_test_demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven_test_demo ---
[INFO] Building jar: /var/lib/jenkins/workspace/maven_home/target/maven_test_demo-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_test_demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven_home/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_test_demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven_test_demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ maven_test_demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven_test_demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven_test_demo ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven_test_demo ---
[INFO] Installing /var/lib/jenkins/workspace/maven_home/target/maven_test_demo-1.0.0-SNAPSHOT.jar to /var/lib/jenkins/.m2/repository/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-SNAPSHOT.jar
[INFO] Installing /var/lib/jenkins/workspace/maven_home/pom.xml to /var/lib/jenkins/.m2/repository/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_test_demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven_home/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_test_demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven_test_demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ maven_test_demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven_test_demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven_test_demo ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven_test_demo ---
[INFO] Installing /var/lib/jenkins/workspace/maven_home/target/maven_test_demo-1.0.0-SNAPSHOT.jar to /var/lib/jenkins/.m2/repository/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-SNAPSHOT.jar
[INFO] Installing /var/lib/jenkins/workspace/maven_home/pom.xml to /var/lib/jenkins/.m2/repository/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ maven_test_demo ---
Downloading from nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml
Progress (1): 776 B

Downloaded from nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml (776 B at 6.5 kB/s)
Uploading to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.jar
Progress (1): 2.6 kB

Uploaded to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.jar (2.6 kB at 13 kB/s)
Uploading to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.pom
Progress (1): 2.1 kB

Uploaded to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.pom (2.1 kB at 19 kB/s)
Downloading from nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/maven-metadata.xml
Progress (1): 286 B

Downloaded from nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/maven-metadata.xml (286 B at 11 kB/s)
Uploading to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml
Progress (1): 776 B

Uploaded to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml (776 B at 4.6 kB/s)
Uploading to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/maven-metadata.xml
Progress (1): 286 B

Uploaded to nexus-snapshots: http://192.168.20.143:8081/repository/maven-snapshots/com/netease/maven_test_demo/maven-metadata.xml (286 B at 2.5 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.640 s
[INFO] Finished at: 2019-08-27T14:24:47+08:00
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS

2.將運行的jar發佈到指定的機器

確保jenkins安裝了Publish Over SSH插件 Manage Jenkins -> Manage Plugins -> 搜索ssh

而後配置ssh

配置後點擊Test Configuration 出現success

配置完成後新建Item(項目)只是多加了一個構建後(注意前面的配置要想要的改 : 項目的git地址)

#最後一個執行的腳本放到的位置是/projects/scripts
mkdir scripts
vim run.sh
#按i將下面內容複製進去

Project_name=$1
Port_no=$2


#echo $Project_name
#echo $Port_no


. /etc/profile


# 進入到項目
cd /projects/$Project_name


# 找到原進程,kill
pid=$(ps -ef | grep java| grep $Project_name|awk -F '[ ]+' '{print $2}')
kill -9 $pid



#啓動jar
nohup java -jar $Project_name-1.0-SNAPSHOT.jar --server.port=$Port_no -server -Xmx1000m -Xms1000m -Xmn700m -XX:+UseParallelGC &

echo "starting service..."
sleep 20


#根據重啓後是否有當前應用判斷啓動是否成功
pid=$(ps -ef | grep java| grep $Project_name|awk -F '[ ]+' '{print $2}')
#echo $pid
if [ -z $pid ];then
  echo "啓動失敗"
  exit 1
else
  echo $Project_name :  $pid  "SUCCESS"
fi


如今當即構建 查看控制檯輸出

Started by user root
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/maven_demo_impl
using credential fad2813a-c894-4386-ae16-11d6e6d349d4
> /usr/local/git/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /usr/local/git/bin/git config remote.origin.url http://192.168.20.143:8083/root/maven_demo_impl.git # timeout=10
Fetching upstream changes from http://192.168.20.143:8083/root/maven_demo_impl.git
> /usr/local/git/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials
> /usr/local/git/bin/git fetch --tags --progress http://192.168.20.143:8083/root/maven_demo_impl.git +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
> /usr/local/git/bin/git show-ref --tags -d # timeout=10
Checking out Revision bf0f9bd981c053359167a0e46e517f4f936c8c98 (origin/master)
> /usr/local/git/bin/git config core.sparsecheckout # timeout
=10
> /usr/local/git/bin/git checkout -f bf0f9bd981c053359167a0e46e517f4f936c8c98
Commit message: "Update TestDemoImpl.java"
> /usr/local/git/bin/git rev-list --no-walk bf0f9bd981c053359167a0e46e517f4f936c8c98 # timeout=10
[maven_demo_impl] $ /project/java/maven/apache-maven-3.6.1/bin/mvn -s /project/java/maven/apache-maven-3.6.1/conf/settings.xml -gs /project/java/maven/apache-maven-3.6.1/conf/settings.xml clean compile package -Dmaven.test.skip=true -U
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.netease:maven_demo_impl >---------------------
[INFO] Building maven_demo_impl 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from nexus-aliyun-repo: http://maven.aliyun.com/nexus/content/groups/public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml
Progress (1): 776 B

Downloaded from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven-metadata.xml (776 B at 4.2 kB/s)
Downloading from nexus-aliyun-repo: http://maven.aliyun.com/nexus/content/groups/public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.pom
Downloading from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.pom
Progress (1): 2.1 kB

Downloaded from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.pom (2.1 kB at 47 kB/s)
Downloading from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.jar
Progress (1): 2.6 kB

Downloaded from maven-public: http://192.168.20.143:8081/repository/maven-public/com/netease/maven_test_demo/1.0.0-SNAPSHOT/maven_test_demo-1.0.0-20190827.062446-3.jar (2.6 kB at 67 kB/s)
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ maven_demo_impl ---
[INFO] Deleting /var/lib/jenkins/workspace/maven_demo_impl/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ maven_demo_impl ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_demo_impl ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /var/lib/jenkins/workspace/maven_demo_impl/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ maven_demo_impl ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ maven_demo_impl ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ maven_demo_impl ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ maven_demo_impl ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ maven_demo_impl ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ maven_demo_impl ---
[INFO] Building jar: /var/lib/jenkins/workspace/maven_demo_impl/target/maven_demo_impl-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.5.RELEASE:repackage (default) @ maven_demo_impl ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.163 s
[INFO] Finished at: 2019-08-27T15:00:40+08:00
[INFO] ------------------------------------------------------------------------
SSH: Connecting from host [localhost.localdomain]
SSH: Connecting with configuration [192.168.20.143] ...
SSH: EXEC: STDOUT/STDERR from command [sh /projects/scripts/run.sh maven_demo_impl 10010] ...
starting service...
nohup: 把輸出追加到"nohup.out"
maven_demo_impl : 4406 SUCCESS
SSH: EXEC: completed after 20,262 ms
SSH: Disconnecting configuration [192.168.20.143] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS


能夠看到ssh已經有jar 並運行了。


1

END

1


推 薦 閱 讀


1.  終於仍是對TA下了手!小白教學:模擬登錄網站並爬取信息

2.  詳解Class類文件的結構(上)

3.  深刻理解 Java 內存模型,學習、面試、工做必看!

4.  詳談類加載的全過程

5.  分佈式系統 CAP 定理的含義

6.  深刻理解synchronized底層原理,一篇文章就夠了!

7.  一文解讀JDK8中HashMap的源碼

8.  談談遊戲數據分析的那點事




北風IT之路  

歡迎關注微信公衆號「北風IT之路」,一塊兒分享有趣的編程知識!

本文分享自微信公衆號 - 北風IT之路(beifengtz)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索