gradle - 在gradle脚本中调用ant任务

标签 gradle ant sap hybris

通常,我们使用SAP-Hybris ant结构来构建我们的系统。当我们用它构建dockerimages时,它变得越来越复杂,所以我们想用gradle封装所有这些

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

repositories {
    jcenter()
}

task run() {
    doLast {
        exec {
            workingDir "${ProjectBaseDir}/bin/platform"
            executable "./hybrisserver.sh"
        }
    }
}
这是我们要做的第一步..在gradle中导入ant脚本,以便我们在gradle中也可以使用所有任务。
按照ant的观点,构建docker镜像的下一步将是:
ant production -Dproduction.include.tomcat=false -Dproduction.legacy.mode=false -Dtomcat.legacy.deployment=false -Dproduction.create.zip=false
其次是
ant createPlatformImageStructure
docker build -t platform .
我正在考虑定义一个新任务
task buildImage {
    dependsOn 'production'
    dependsOn 'createPlatformImageStructure'
    doLast {
       // do the docker stuff
    }
}
但是我如何将参数(ant production -Dproduction.include.tomcat=false -Dproduction.legacy.mode=false -Dtomcat.legacy.deployment=false -Dproduction.create.zip=false合并到“生产”中?
更新:
Ant 属性现在这样处理:
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
    }
仍然没有运气。当ant运行这些设置都不存在时

最佳答案

我还必须从Gradle执行平台ant命令。我构建了一个包装器,而不是导入脚本。它工作正常,所以希望对您有用。包装器是跨平台的。
文件:

  • scripts/ant.sh-在Unix / Linux上执行平台ant二进制文件
  • #!/usr/bin/env sh
    (
        . ./setantenv.sh
        echo ''
        echo Executing: ant $@
        echo ''
        ant $@
    )
    
  • scripts/ant.bat-在Windows上执行平台ant二进制文件
  • @echo off
    setlocal
    call setantenv.bat
    echo:
    echo Executing: ant %*
    echo:
    ant %*
    endlocal
    
  • gradle/platformScript.gradle-执行平台脚本(我仅实现了ant,但您可以添加更多脚本)
  • 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
    }
    
    示例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'
            ]
        }
    }
    

    关于gradle - 在gradle脚本中调用ant任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62997030/

    相关文章:

    java - Ant脚本中**/*的含义

    java - 在gradle中将类路径添加到ant任务的最佳方法

    database - 在哪里存储人员信息 LDAP 或数据库

    database - 为什么要将大多数文件存储在数据库中?

    VBA SAP 创建 session

    android - 在android库包中包含一个外部库

    java - Gradle maven插件: uploadArchives cannot find/tmp/gradle_empty_settings xml

    java - 遍历文件集时出现问题

    gradle - 如何使用 kotlinscript DSL (build.gradle.kts) 通过 url 添加 maven 存储库

    java - 扫描后项目未在 sonarqube 中更新