[JAVA] How to run Ant in Gradle

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.

Import build.xml

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

When the target name of Ant and the task name of Gradle are duplicated

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

Property setting method

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

Summary

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.

reference

Gradle User Guide Gradle (build.gradle) Introduction to reading and writing

Thank you for your cooperation.

Recommended Posts

How to run Ant in Gradle
How to run a djUnit task in Ant
How to run JUnit in Eclipse
How to filter JUnit Test in Gradle
How to use Gradle
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to master programming in 3 months
How to run JavaFX on Docker
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to run npm install on all projects in Lerna
How to use InjectorHolder in OpenAM
How to dynamically switch JDK when building Java in Gradle
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to concatenate strings in java
How to install Swiper in Rails
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to run a job with docker login in AWS batch
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to compile Java with VsCode & Ant
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
[Swift] How to fix Label in UIPickerView
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to fix system date in JUnit
How to implement coding conventions in Java
How to embed Janus Graph in Java
[rails] How to configure routing in resources
Do not accept System.in in gradle run
How to map tsrange type in Hibernate
How to get the date in java
How to implement ranking functionality in Rails
How to use environment variables in RubyOnRails
How to implement asynchronous processing in Outsystems
How to publish a library in jCenter
How to specify id attribute in JSF
Understand in 5 minutes !! How to use Docker
How to overwrite Firebase data in Swift
How to use credentials.yml.enc introduced in Rails 5.2
How to run Blazor (C #) with Docker