Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in custom ant tasks

Tags:

java

logging

ant

I'm creating a custom ant task, which performs an IO tasks based on the user received param(like an file write/append)

I wanted to write the task so as if the developer using it in the ant task runs it with a -v or -d flag, will output more,

I'm wondering how are the core ant tasks doing it. Are they checking the output level before printing to console or is it just done by using java.util.logging.Logger

like image 949
Avinash R Avatar asked Feb 22 '26 07:02

Avinash R


1 Answers

Follow this tutorial.

Extract :

Integration with TaskAdapter

Our class has nothing to do with Ant. It extends no superclass and implements no interface. How does Ant know to integrate? Via name convention: our class provides a method with signature public void execute(). This class is wrapped by Ant's org.apache.tools.ant.TaskAdapter which is a task and uses reflection for setting a reference to the project and calling the execute() method.

Setting a reference to the project? Could be interesting. The Project class gives us some nice abilities: access to Ant's logging facilities getting and setting properties and much more. So we try to use that class:

import org.apache.tools.ant.Project;

public class HelloWorld {

    private Project project;

    public void setProject(Project proj) {
        project = proj;
    }

    public void execute() {
        String message = project.getProperty("ant.project.name");
        project.log("Here is project '" + message + "'.", Project.MSG_INFO);
    } }

[...]

like image 150
Aerospace Avatar answered Feb 23 '26 20:02

Aerospace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!