【问题标题】:How to use if else condition in Gradle如何在 Gradle 中使用 if else 条件
【发布时间】:2018-12-13 22:01:30
【问题描述】:

谁能告诉我如何在 gradle 脚本中编写 if else 条件 我的意思是我有两种不同类型的 zip 文件,一种是 LiceseGenerator-4.0.0.58,另一种是 CLI-4.0.0.60。我的部署脚本工作正常,但我使用 shell 脚本来执行此操作,我希望一切都在 gradle 中在 shell 脚本中执行此操作。我希望在部署 LicenseGenerator 时它应该以不同的方式部署,如果它是 CLI,那么它应该以其他方式部署。当前 deployall 任务正在做所有事情。如果我把 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){}

【问题讨论】:

    标签: groovy gradle build.gradle gradlew


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在构建脚本中包含if/else 逻辑通常是一种不好的做法,因为它会增加复杂性,并且有时会导致令人惊讶和意外的结果。由于您有非常不同的工件,因此建议为此设置两个不同的任务,而不是一劳永逸的deployAll。并且在不同的环境中调用相应的任务。

      【讨论】:

      • 我认为你的建议很好,但它没有回答 OP 的问题。在免责声明之后,如果答案回答问题会更好。
      猜你喜欢
      • 2019-04-25
      • 2019-12-28
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2017-01-11
      相关资源
      最近更新 更多