gradle - 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
解决方案
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 platformant
binary on Unix/Linux
#!/usr/bin/env sh
(
. ./setantenv.sh
echo ''
echo Executing: ant $@
echo ''
ant $@
)
scripts/ant.bat
- executes platformant
binary on Windows
@echo off
setlocal
call setantenv.bat
echo:
echo Executing: ant %*
echo:
ant %*
endlocal
gradle/platformScript.gradle
- executes platform scripts (I implemented onlyant
, 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'
]
}
}
推荐阅读
- php - html下拉菜单在wordpress网站中不起作用
- algorithm - 找到一个最接近 0 且未被任何区间采用的数字(表示左右值的两个数字的集合)
- api - NoSuchMethodError:在 null 上调用了 getter 'length'。在调用 put api 时?
- google-sheets - 使用 Arrayformula 在 A 列中显示百分比符号
- r - 为什么 Trelliscope 查看器显示空面板,没有图像?
- javascript - 多标签对齐
- .net - 为什么我在 VSTO 插件中的图标看起来很模糊?
- android - 莫西。父抽象类中的@Json 注解被忽略
- javascript - 处理未定义函数后的异常
- javascript - 重新初始化数据表jquery后行数据未定义