Jenkins 常用的项目构建工具集成 02 Gradle

gradle相对于maven项目来说,打包速度会快一些(这个工具也需要Java的环境)和之前maven创建项目一样,这个不变。

下载gradle构建工具

Jenkins 常用的项目构建工具集成 02 Gradle_第1张图片

创建gradle项目 

Jenkins 常用的项目构建工具集成 02 Gradle_第2张图片

将项目推送到gitlab上面

Jenkins 常用的项目构建工具集成 02 Gradle_第3张图片

[root@jenkins-master ~]# cd devops-gradle-service-master
[root@jenkins-master devops-gradle-service-master]# ls
build.gradle  gradle  gradlew  gradlew.bat  Jenkinsfile  settings.gradle  src
[root@jenkins-master devops-gradle-service-master]# git init
Initialized empty Git repository in /root/devops-gradle-service-master/.git/
[root@jenkins-master devops-gradle-service-master]# git remote add origin http://139.198.170.122:81/root/devops-gradle-service.git
[root@jenkins-master devops-gradle-service-master]# git add .
[root@jenkins-master devops-gradle-service-master]# git commit -m "Initial commit"
[master (root-commit) eef0e43] Initial commit
 11 files changed, 475 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Jenkinsfile
 create mode 100644 build.gradle
 create mode 100644 gradle/wrapper/gradle-wrapper.jar
 create mode 100644 gradle/wrapper/gradle-wrapper.properties
 create mode 100755 gradlew
 create mode 100644 gradlew.bat
 create mode 100644 settings.gradle
 create mode 100644 src/main/java/com/example/demo/DemoApplication.java
 create mode 100644 src/main/resources/application.properties
 create mode 100644 src/test/java/com/example/demo/DemoApplicationTests.java
[root@jenkins-master devops-gradle-service-master]# git push -u origin master
Username for 'http://139.198.170.122:81': root
Password for 'http://[email protected]:81': 
Counting objects: 27, done.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (27/27), 58.48 KiB | 0 bytes/s, done.
Total 27 (delta 0), reused 0 (delta 0)
To http://139.198.170.122:81/root/devops-gradle-service.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Jenkins 常用的项目构建工具集成 02 Gradle_第4张图片

安装配置


unzip gradle-6.8.3-bin.zip -d /usr/local/
export GRADLE_HOME=/usr/local/gradle-6.8.3/
export PATH=$GRADLE_HOME/bin:$PATH
gradle -v

gradle项目的构建


常用命令

  • gradle build  构建项目
  • gradle build -x test 构建项目跳过测试
  • gradle clean 清空构建目录

我们这里使用远程的Jenkinsfile,也就是将Jenkinsfile存放在远程的版本系统里面,添加一个Jenkinsfile 

Jenkins 常用的项目构建工具集成 02 Gradle_第5张图片

配置仓库地址

Jenkins 常用的项目构建工具集成 02 Gradle_第6张图片  配置SCM这里会多出代码下载的步骤  

Jenkins 常用的项目构建工具集成 02 Gradle_第7张图片

现在要跳过这个阶段,下面生成代码

Jenkins 常用的项目构建工具集成 02 Gradle_第8张图片

options {
  skipDefaultCheckout true
}

 gradle在build的时候会去做测试,gradle buid会去做测试,如果gradle build -x test就会跳过测试

[root@jenkins-master devops-gradle-service-master]# gradle build
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :test
2021-06-02 14:52:43.469  INFO 9119 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

BUILD SUCCESSFUL in 3m 13s
6 actionable tasks: 6 executed

#测试报告的位置
[root@jenkins-master devops-gradle-service-master]# ls
build  build.gradle  gradle  gradlew  gradlew.bat  Jenkinsfile  settings.gradle  src
[root@jenkins-master devops-gradle-service-master]# cd build
[root@jenkins-master build]# ls
bootJarMainClassName  classes  generated  libs  reports  resources  test-results  tmp
[root@jenkins-master build]# cd test-results/
[root@jenkins-master test-results]# ls
test
[root@jenkins-master test-results]# cd test/
[root@jenkins-master test]# ls
binary  TEST-com.example.demo.DemoApplicationTests.xml

#包的位置
[root@jenkins-agent build]# ls libs/
demo-0.0.1-SNAPSHOT.jar
[root@jenkins-agent build]# pwd
/root/devops-gradle-service-master/build
def buildTools = ["gradle": "/usr/local/gradle-6.8.3"]

pipeline{
    agent {label "build"}
    
    options {
  skipDefaultCheckout true
}

    stages{
        stage("GetCode"){
          steps{
            script{
                println("下载分支代码----->${env.branchName}")
                checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '1d2790c6-c757-4011-8348-c4e91ce26ea9', url: "${env.gitHttpURL}"]]])
            }
          }
        }
        stage("Build"){
          steps{
            script{
                sh "${buildTools["gradle"]}/bin/gradle clean && ${buildTools["gradle"]}/bin/gradle build -x test"
            }
        }
      }
        stage("UnitTest"){
          steps{
            script{
                sh "${buildTools["gradle"]}/bin/gradle test"
           }
        }
        post{
            success{
                script{
                    junit 'build/test-results/test/*.xml'
                }
            }
        }
      }
    }


}

Jenkins 常用的项目构建工具集成 02 Gradle_第9张图片

你可能感兴趣的:(Devops,CI/CD,Jenkins,jenkins,java)