2015-05-19 795 views
2

有人可以告訴我如何在gradle腳本中編寫if else條件 我的意思是我有兩種不同類型的zip文件,一種是LiceseGenerator-4.0.0.58,另一種是CLI -4.0.0.60。我的部署腳本工作正常,但我正在使用shell腳本來做到這一點,我希望所有的東西都在gradle中,而不是在shell腳本中做。我想在部署LicenseGenerator時以不同的方式部署LicenseGenerator如果是CLI,則應該以其他方式部署。目前部署任務正在做每一個。如果我把其他條件如何我可以怎麼稱呼任務。請讓我知道是否需要任何其他信息如何在Gradle中使用if else條件

以下是我的腳本

// ------ 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){} 

回答

0

我用下面的方法,它正在罰款

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

     // ------ Tell the script to get dependencies from artifactory ------ 
     dependencies { 
     classpath ([ "c.t.c:cmgin:1.1.118" ]) 
     } 
    } 

    apply plugin: 'com.t.c.cmlugin' 

    /** 
    * 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"] 
     deploydir = deployProperties["${System.properties.jobName}.deploydir"] 
     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 copyartifactZip << { 
     copy { 
      from "${deployRoot}" 
      into "${deploydir}/" 
     } 
    } 

    task copyLicenseZip << { 
     copy { 
      from "${deployRoot}" 
      into "${deploydir}/${deployFolder}" 
     } 
    } 

    task myCustomTask(dependsOn: deleteGraphicsAssets) << { 
     copy { 
      from 'deploymentfiles' 
      into "${deployRoot}" 
     } 
    } 
    task unzipArtifact(dependsOn: myCustomTask) << { 
     def theZip = file("${buildDir}/artifacts").listFiles().find { it.name.endsWith('.zip') } 
     println "Unzipping ${theZip} the artifact to: ${deployRoot}" 
     ant.unzip(src: theZip, dest: "${deployRoot}", overwrite: true) 
    } 

    task setPerms(type:Exec, dependsOn: unzipArtifact) { 
     workingDir "${deployRoot}" 
     executable "bash" 
     args "-c", "chmod -fR 755 *" 

     } 
    def dirName = "${deploydir}/${deployFolder}" 
    task zipDeployment(type: GradleBuild, dependsOn: setPerms) { GradleBuild gBuild -> 
     def env = System.getenv() 
     def jobName=env['jobName'] 
    if (jobName.equals("LicenseGenerator")) { 
     delete dirName 
     file(dirName).mkdirs() 
     gBuild.tasks = ['copyLicenseZip'] 
     } else { 
     gBuild.tasks = ['copyartifactZip'] 
    } 
    } 

    task deployAll(dependsOn: zipDeployment){} 
0

它通常是一個不好的做法,有if/else邏輯在構建腳本,因爲它增加了複雜性,有時會導致令人驚訝和意外的結果。既然你有非常不同的文物,建議有兩個不同的任務,而不是一勞永逸的deployAll。當你處於不同的環境時,你應該調用相應的任務。