web-dev-qa-db-ja.com

Gradleでifelse条件を使用する方法

誰かがgradleスクリプトにifelse条件を書く方法を教えてもらえますか?つまり、2つの異なるタイプのZipファイルがあります。1つはLiceseGenerator-4.0.0.58で、もう1つはCLI-4.0.0.60です。デプロイメントスクリプトは正常に機能していますが、シェルスクリプトを使用してこれを行っていますが、シェルスクリプトではなく、すべてをgradleで実行する必要があります。LicenseGeneratorをデプロイするときは、別の方法でデプロイする必要があります。CLIの場合は、別の方法でデプロイする必要があります。 。現在、deployallタスクはすべてを実行しています。他の条件を設定した場合、タスクを呼び出す方法を教えてください。他の情報が必要な場合はお知らせください。

以下は私のスクリプトです

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

私はそれがうまく機能している以下の方法で使用しました

  // ------ 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
unknown

ビルドスクリプトにif/elseロジックを含めることは、複雑さを増し、時には意外で予期しない結果を引き起こす可能性があるため、通常は悪い習慣です。アーティファクトは非常に異なるため、1対1のdeployAllではなく、2つの異なるタスクを使用することをお勧めします。また、異なる環境にいる場合は、対応するタスクを呼び出す必要があります。

1
Ivan Frolov