首页 > 解决方案 > Calling ant task within gradle script

问题描述

Normally we use the SAP-Hybris ant structures to build our system. It is getting a bit more complicated when we build dockerimages with that, so we want to wrap all this with gradle

ant.importBuild "${ProjectBaseDir}/bin/platform/build.xml"

repositories {
    jcenter()
}

task run() {
    doLast {
        exec {
            workingDir "${ProjectBaseDir}/bin/platform"
            executable "./hybrisserver.sh"
        }
    }
}

This is the first step we do .. import the ant script in gradle so that we have all tasks available in gradle as well. The next steps to build docker images would (in terms of ant) would be :

ant production -Dproduction.include.tomcat=false -Dproduction.legacy.mode=false -Dtomcat.legacy.deployment=false -Dproduction.create.zip=false

followed by

ant createPlatformImageStructure
docker build -t platform .

I am thinking about defining a new task

task buildImage {
    dependsOn 'production'
    dependsOn 'createPlatformImageStructure'
    doLast {
       // do the docker stuff
    }
}

But how do i incorporate the Parameters (ant production -Dproduction.include.tomcat=false -Dproduction.legacy.mode=false -Dtomcat.legacy.deployment=false -Dproduction.create.zip=false to 'production' ?

Update :

Ant Properties are handled now like this :

task dockerimage (dependsOn : production) {
    doFirst {
        ant.properties['production.include.tomcat'] = false
        ant.properties['production.legacy.mode'] = false
        ant.properties['tomcat.legacy.deployment'] = false
        ant.properties['production.create.zip'] = false
    }

Still no luck. When ant is running none of these settings a there

标签: gradleantsaphybris

解决方案


I also had to execute platform ant commands from Gradle. I built a wrapper instead of importing scripts. It works fine, so I hope it will be useful for you. The wrapper is cross-platform.

Files:

  • scripts/ant.sh - executes platform ant binary on Unix/Linux
#!/usr/bin/env sh
(
    . ./setantenv.sh
    echo ''
    echo Executing: ant $@
    echo ''
    ant $@
)
  • scripts/ant.bat - executes platform ant binary on Windows
@echo off
setlocal
call setantenv.bat
echo:
echo Executing: ant %*
echo:
ant %*
endlocal
  • gradle/platformScript.gradle - executes platform scripts (I implemented only ant, but you can add more)
import org.gradle.internal.os.OperatingSystem

void platformScript(def parameters) {
    def script = parameters['script'] ?: 'ant'
    def arguments = parameters['arguments']
    if (!(arguments instanceof Collection)) {
        arguments = [arguments]
    }

    def args = []
    def extension
    if (OperatingSystem.current().isWindows()) {
        args << 'cmd'
        args << '/c'
        extension = 'bat'
    } else {
        extension = 'sh'
    }

    def scriptFile = "${rootProject.rootDir}/scripts/${script}.${extension}"
    if (!scriptFile.exists()) {
        throw new IllegalArgumentException("Script \"${script}\" does not exist! Full path: ${scriptFile.absolutePath}")
    }
    args << scriptFile.absolutePath

    if (OperatingSystem.current().isWindows()) {
        for (def argument in arguments) {
            def index = argument.indexOf('=')
            if (index == -1) {
                args << argument
            } else {
                def name = argument.substring(0, index)
                def value = argument.substring(index + 1).replace('\"', '\"\"')
                args << "${name}=\"${value}\""
            }
        }
    } else {
        args.addAll(arguments)
    }

    exec {
        workingDir "${ProjectBaseDir}/bin/platform"
        commandLine args
    }
}

ext {
    platformScript = this.&platformScript
}

Example build.gradle:

apply from: "${rootProject.rootDir}/gradle/platformScript.gradle"

task cleanPlatform() {
    doFirst {
        // executes: ant clean
        platformScript arguments: 'clean'
    }
}
tasks.clean.dependsOn('cleanPlatform')

task production() {
    doFirst {
        // executes: ant production -Dproduction.include.tomcat=false ...
        platformScript arguments: [
            'production',
            '-Dproduction.include.tomcat=false',
            '-Dproduction.legacy.mode=false',
            '-Dtomcat.legacy.deployment=false',
            '-Dproduction.create.zip=false'
        ]
    }
}

推荐阅读