You may want to execute the target of Ant build.xml from Gradle, such as when you already have Ant assets in a project using Java.
I've found out how to do that, so I'll spell it out with a note.
Use the ant.importBuild () method to import.
▼ Prepare one target to echo in build.xml
build.xml
<?xml version="1.0" encoding="utf-8" ?>
<project name="antsample" default="target01">
<target name="target01">
<echo message="message01" />
</target>
</project>
▼ Import so that build.xml can be used on the build.gradle side
build.gradle
ant.importBuild 'build.xml'
▼ The target defined in build.xml can be used.
$ gradle tasks --all
~abridgement~
Other tasks
-----------
prepareKotlinBuildScriptModel
target01
$ gradle target01
> Task :target01
[ant:echo] message01
BUILD SUCCESSFUL in 846ms
1 actionable task: 1 executed
As a caveat, if the task name prepared in Gradle is duplicated with target in build.xml, an error will occur. Therefore, it seems good to be able to execute the target of ant with ant. [Target name] as shown below.
build.gradle
ant.importBuild('build.xml') {
antTaskName -> "ant.${antTaskName}".toString()
}
$ gradle tasks --all
~abridgement~
Other tasks
-----------
ant.build
prepareKotlinBuildScriptModel
You could specify an argument with -D when running Ant, but you can do the same with Gradle.
▼ Define property on build.xml side
build.xml
<?xml version="1.0" encoding="utf-8" ?>
<project name="antsample" default="target01">
<property name="message" value="default message"/>
<target name="target01">
<echo message="${message}" />
</target>
</project>
▼ Gradle side only needs to import build.xml
build.gradle
ant.importBuild 'build.xml'
▼ I was able to specify a message and echo
$ gradle target01 -Dmessage='message param'
> Task :target01
[ant:echo] message param
BUILD SUCCESSFUL in 726ms
1 actionable task: 1 executed
I just had to run Ant from Gradle with arguments, but I had a lot of trouble. At first, I used ant.properties on the gradle.build side to pass arguments, but I was able to execute target directly from the command line with "-D" without using it, so it was quite refreshing.
Gradle and Groovy are interesting to touch, so I'll post if I've learned anything again.
Gradle User Guide Gradle (build.gradle) Introduction to reading and writing
Thank you for your cooperation.
Recommended Posts