使用gradle进行RPM打包 - Gradle buildRpm

此方法基于gradle-ospackage-plugin插件

参考文档

nebula:rpm
Gradle:RPM-Plugin wiki
Gradle:copying_files

环境准备

安装 Gradle

可以使用构建好的gradle镜像, 参考Dockerfile

  • 依赖 java 1.8

下载gradle二进制包,并设置环境变量

$ VERSION=4.3.1

$ curl -O https://services.gradle.org/distributions/gradle-${VERSION}-bin.zip

$ export PATH=$PATH:/usr/local/gradle-${VERSION}/bin

验证是否安装成功

$ gradle -v

------------------------------------------------------------
Gradle 4.3.1
------------------------------------------------------------

Build time:   2017-11-08 08:59:45 UTC
Revision:     e4f4804807ef7c2829da51877861ff06e07e006d

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_111 (Oracle Corporation 25.111-b14)
OS:           Linux 3.10.0-514.el7.x86_64 amd64

构建jre的rpm包

这里使用Gradle 2.1开始支持的新写法,更加简洁;老版本写法

需要创建构建文件build.gradle

Step 1: 引用依赖包 - Build script snippet

plugins {
  id "nebula.rpm" version "4.5.1"
}

Step 2: 添加task

task buildJRE(type: Rpm) {
    packageName = 'jre-8u111'
    version = '0.0.1'
    release = 'dsky'
    packageDescription = 'Digital sky - JRE Runtime Environment'
    arch = X86_64
    os = LINUX
    
    ......
}

最终生成的build.gradle

$ cat build.gradle
plugins {
  id "nebula.ospackage" version "4.5.1"
}

task buildJRE(type: Rpm) {
    packageName = 'jre-8u111'
    version = '0.0.1'
    release = 'dsky'
    packageDescription = 'Digital sky - JRE Runtime Environment'
    arch = X86_64
    os = LINUX

    /*
    preInstall file('scripts/preInstall.sh')

    requires('epel-release')
    */

    FileTree jre_files = tarTree('jre-8u111-linux-x64.tar.gz')

    from(jre_files) {
        into '/usr/local/'
    }

    from('scripts/java.sh') {
        into '/etc/profile.d/'
    }

    link('/bin/java', '/usr/local/jre1.8.0_111/bin/java')
}

println relativePath(buildJRE.archivePath)

开始构建

  • 构建task: buildJRE
$ gradle clean buildJRE #buildJRE为task名称


> Configure project : 
build/distributions/jre-8u111-0.0.1-dsky.x86_64.rpm


BUILD SUCCESSFUL in 26s
2 actionable tasks: 2 executed
  • 验证安装
$ rpm -ivh build/distributions/jre-8u111-0.0.1-dsky.x86_64.rpm