groovy - 如何在Gradle中使用if else条件

标签 groovy gradle build.gradle gradlew

有人可以告诉我如何在gradle脚本中编写if else条件吗?
我的意思是我有两种不同类型的zip文件,一种是LiceseGenerator-4.0.0.58,另一种是CLI-4.0.0.60。我的部署脚本运行良好,但是我正在使用Shell脚本来执行此操作,因此我希望所有内容都在gradle中我想在部署LicenseGenerator时以不同的方式进行部署,如果是CLI则应以其他方式进行部署。我给任务打电话了,如果需要其他信息请告诉我

下面是我的脚本

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

最佳答案

我以下面的方式使用它工作正常

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

关于groovy - 如何在Gradle中使用if else条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30331762/

相关文章:

python - 在 Rexster 中测试从 Groovy 命令行创建顶点

android - 更新 Gradle 插件后 Android maven 发布 Artifact 错误

gradle - 无论如何要在构建任务中执行特定任务吗?

gradle - 如何扩展 gradle 的 clean 任务来删除文件?

grails - grails 中的通用方法拦截(特别是 Controller )

maven - sonar maven分析仅选择.java文件

android - 所有支持库必须使用完全相同的版本规范。发现27.0.1.26.1.0

android - 将结构依赖项添加到 cordova build android 项目的问题

java - “没有这样的表”SqliteDatabase

android - 安装 gradle 以在 cordova build android 中使用