如何在Gradle中使用if else条件 [英] How to use if else condition in Gradle

查看:2438
本文介绍了如何在Gradle中使用if else条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在gradle脚本中编写if else条件
我的意思是我有两种不同类型的zip文件,一种是LiceseGenerator-4.0.0.58,另一种是CLI-4.0.0.60。我的部署脚本工作正常,但我正在使用shell脚本来做到这一点,我希望所有的东西都在gradle中,而不是在shell脚本中进行。我想在部署LicenseGenerator时以不同的方式部署它,如果它是CLI那么它应该以其他方式进行部署。目前部署任务正在做everyting。如果我把其他条件,如何我可以调用task.Please让我知道,如果需要任何其他信息



下面是我的脚本

  // ------告诉脚本从artifactory获取依赖关系---- -  
buildscript {
repositories {
maven {
urlhttp://ct.ts.th.com:8/artifactory/libs-snapshot
}
}

// ------告诉脚本从artifactory获取依赖关系 - -----
依赖项{
classpath([com.trn.cm:cmplugin:1.1.118])
}
}

apply plugin:'com.trn.cm.cmgplugin'

/ **
*执行此任务需要以下-D参数
* - deployLayer =其中一个验收,最新,制作,测试
* /
// ------------------------------- -------------------------------------------------- ---------
//读取属性文件并根据环境获取值。
//
// -------------------------------------- -------------------------------------------------- -
if(!System.properties.deployLayer)抛出新异常(deployLayer must be set)
def thePropFile = file(config / $ {System.properties.deployLayer} .properties)
如果(!thePropFile.exists())抛出新的异常(无法从$ {thePropFile}加载指定的环境属性)
println部署$ {System.properties.jobName}。$ {System .properties.buildNumber}为$ {System.properties.deployLayer}

//加载从文件
DEF deployProperties部署属性=新属性()
thePropFile.withInputStream {
流 - > deployProperties.load(流)
}
//在构建环境中设置它们
project.ext {
deployProps = deployProperties
deployRoot = deployProperties [$ {System .properties.jobName} .deployroot ]
deployFolder = deployProperties [ $ {System.properties.jobName} .foldername ]
deployPostInstallSteps = deployProperties [ $ {System.properties.jobName} .postInstallSteps ]
}

任务deleteGraphicsAssets(类型:Delete,dependsOn:deploy){
def dirName =$ {deployRoot}
delete dirName

doLast {
文件(目录名).mkdirs()
}
}


任务myCustomTask(dependsOn:deleteGraphicsAssets)LT;< {
从'deploymentfiles'
复制{
到'$ {deployRoot}
}
}

任务cleanTempDir(类型:Delete ,dependsOn:myCustomTask){
删除文件树(导演: 构建/神器,排除: * .zip文件)
}

任务unzipArtifact(dependsOn:cleanTempDir)LT ;< {
file($ {buildDir} / artifacts)。eachFile(){
println部署$ {it}
// ant.mkdir(dir:$ {deployRoot} / $ {deployFolder})
ant.unzip(src:it,dest:$ {deployRoot})
}
}

任务setPerms(type :Exec,dependsOn:unzipArtifact){
workingDir$ {deployRoot}
可执行文件bash
args-c,dos2unix analyticsEngine.sh
args - c,chmod u + x analyticsEngine.sh&& ./analyticsEngine.sh
}

任务deployAll(dependsOn:setPerms){}

解决方案

如果 逻辑,因为它增加了复杂性,有时会导致令人惊讶和意外的结果。由于你有非常不同的工件,因此最好有两个不同的任务,而不是一对一的 deployAll 。当你处于不同的环境时,你应该调用相应的任务。


Can someone tell me how could I write the if else condition in the gradle script I mean i have two different types of zip files one is LiceseGenerator-4.0.0.58 and other one is CLI-4.0.0.60.My deployment script is working fine but I am using the shell script to do this and I want everything in gradle instead of doing it in the shell script.I want when I am deploying the LicenseGenerator it should deploy in differnet way and if it is CLI then it should deploy in other way.Currently deployall task is doing everyting.If I put if else condition how could I call the task.Please let me know if need any other information

Below is my script

// ------ Tell the script to get dependencies from artifactory ------
buildscript {
    repositories {
      maven {
        url "http://ct.ts.th.com:8/artifactory/libs-snapshot"
         }
    }

    // ------ Tell the script to get dependencies from artifactory ------
    dependencies {
    classpath ([ "com.trn.cm:cmplugin:1.1.118" ])
  }
}

apply plugin: 'com.trn.cm.cmgplugin'

/**
 * The folloing -D parameters are required to run this task
 *  - deployLayer = one of acceptance, latest, production, test
 */
//------------------------------------------------------------------------------------------
// Read the properties file and take the value as per the enviornment.
// 
//------------------------------------------------------------------------------------------
if(!System.properties.deployLayer) throw new Exception ("deployLayer must be set")
def thePropFile = file("config/${System.properties.deployLayer}.properties")
if(!thePropFile.exists()) throw new Exception("Cannot load the specified environment properties from ${thePropFile}")
println "Deploying ${System.properties.jobName}.${System.properties.buildNumber} to ${System.properties.deployLayer}"

// load the deploy properties from the file
def deployProperties = new Properties()
thePropFile.withInputStream { 
    stream -> deployProperties.load(stream) 
} 
// set them in the build environment
project.ext {
  deployProps = deployProperties
  deployRoot = deployProperties["${System.properties.jobName}.deployroot"]
  deployFolder = deployProperties["${System.properties.jobName}.foldername"]
  deployPostInstallSteps = deployProperties["${System.properties.jobName}.postInstallSteps"]
}

task deleteGraphicsAssets(type: Delete, dependsOn: deploy) {
    def dirName = "${deployRoot}"
    delete dirName

    doLast {
        file(dirName).mkdirs()
    }
}


task myCustomTask(dependsOn: deleteGraphicsAssets) << {
    copy {
        from 'deploymentfiles'
        into "${deployRoot}"
    }
}

task cleanTempDir(type: Delete, dependsOn: myCustomTask) {
      delete fileTree(dir: "build/artifacts", exclude: "*.zip")
  }

task unzipArtifact(dependsOn: cleanTempDir) << {
  file("${buildDir}/artifacts").eachFile() { 
    println "Deploying ${it}"
   // ant.mkdir(dir: "${deployRoot}/${deployFolder}")
    ant.unzip(src: it, dest: "${deployRoot}")
  }
}

task setPerms( type: Exec, dependsOn: unzipArtifact) {
  workingDir "${deployRoot}"
  executable "bash"
  args "-c", "dos2unix analyticsEngine.sh"
  args "-c", "chmod u+x analyticsEngine.sh && ./analyticsEngine.sh"
 }

 task deployAll(dependsOn: setPerms){}

解决方案

It's usually a bad practice to have if/else logic in the build script because it adds complexity and sometimes causes surprising and unexpected results. Since you have very different artifacts, it's advisable to have two different tasks for that, instead of one-for-all deployAll. And you should call corresponding task when you are in different environments.

这篇关于如何在Gradle中使用if else条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆