通常,我们使用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'
]
}
}