You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Chris Brown <br...@reflexe.fr> on 2003/05/23 14:39:44 UTC

How can I determine which targets will be executed, using the Ant API?

Hello,

How can I determine which targets will be executed, using the Ant API?

I'm writing a very specific task that should behave differently depending
upon what will be done later as the script continues executing.

It's used in almost all build scenarios, and has a lot of parameters.  I'd
like to put it in an "init" target, instead of copying to the start of lots
of other targets (to avoid cut-and-paste errors as the project evolves).

How does Ant track this internally, and how can I access this information?

Thanks,
Chris



Re: How can I determine which targets will be executed, using the Ant API?

Posted by Dale Anson <da...@germane-software.com>.
I looked at my code for antelope, there is a trace functionality that
does what you're talking about. The core method is
Target.getDependencies(), which tells you what targets the given target
depends on. Of course, there is more to it than that. It also goes
through that list and calls getDependencies on each, plus looks within
the given target for 'ant' and 'antcall' tasks and looks at those
targets as well. From all of this, you can find out what targets your
target is going to execute. The source file name is TraceTarget.jave and
is in the antelope source at antelope.sourceforge.net. You could
probably adapt it for your needs.

Hope this helps!

Dale


Chris Brown wrote:

>I guess that means that there's no way to do this in Ant?  (would confirm my
>suspicions).
>
>Here's my use-case, for what it's worth.
>
>I have a version of the <buildnumber> task (totally home-made, but with
>similar aims).  I want to increment the build number if I'm going to attempt
>to compile a new version of executable code, but don't want to increment it
>if I'm only going to run a documentation-related task (for example).  The
>documentation-related tasks include the build number in documentation files,
>such as Javadocs.  Even if the compile fails, my home-brew <buildnumber>
>replacement records this failure in a database.
>
>In case anyone's wondering why I wrote my own version, it's because we work
>in a team, compiling versions of the application on different branches of a
>CVS repository, so build numbers do not simply increase in a linear way; we
>use these serial numbers to track what build corresponds to which version,
>and information about the environment in which the build was run.
>
>Thanks again for your help Peter.
>
>- Chris
>
>----- Original Message ----- 
>From: "peter reilly" <pe...@corvil.com>
>To: "Ant Users List" <us...@ant.apache.org>
>Sent: Friday, May 23, 2003 7:13 PM
>Subject: Re: How can I determine which targets will be executed, using the
>Ant API?
>
>
>Ah, I see.
>
>Peter
>On Friday 23 May 2003 17:49, Chris Brown wrote:
>  
>
>>Hi Peter,
>>
>>I'm trying to access the list of targets to be executed, not the list of
>>ALL targets in the build file.  I'd already looked at the javadocs and the
>>source code.... I tried using getTargets(), but that returns a list of all
>>targets in the project's build file.
>>
>>Obviously, if I had the basic list, I could use topoSort(...) to resolve
>>dependencies, but I'm aware of that, and that's not the problem here.
>>
>>The list of targets being executed seems to be stored as local variables
>>within method calls, and is not stored as an instance variable in
>>"Project.java".  In the source code for Ant 1.5.3, in line 609 of
>>org/apache/tools/ant/Main.java, a private instance variable (Vector
>>targets) is passed to the "executeTargets" method of Project, but these
>>targets are not exposed either in "Main.java" or "Project.java", so I
>>    
>>
>can't
>  
>
>>tell if a specific target is going to be called (unless of course a
>>BuildException occurs).  Furthermore, I don't think it's wise to access
>>"Main.java", as it's possible to execute targets in a project directly
>>using the API without using "main" (I think so anyway, I've never tried).
>>
>>Thanks anyway,
>>Chris
>>
>>----- Original Message -----
>>From: "peter reilly" <pe...@corvil.com>
>>To: "Ant Users List" <us...@ant.apache.org>
>>Sent: Friday, May 23, 2003 6:33 PM
>>Subject: Re: How can I determine which targets will be executed, using the
>>Ant API?
>>
>>
>>The target info is maintained per project in the Project
>>object's targets hashmap;
>>Project uses "topoSort()" to sort the targets.
>>These are publiclly visable.
>>
>>The following task uses this information.
>>This does not deal with targets generated
>>using ant or antcall.
>>
>>import org.apache.tools.ant.Project;
>>import org.apache.tools.ant.Task;
>>import org.apache.tools.ant.Target;
>>import org.apache.tools.ant.BuildException;
>>import java.util.*;
>>
>>public class TargetDepends extends Task {
>>    private String target;
>>    public void setTarget(String target) {
>>        this.target = target;
>>    }
>>    public void execute() {
>>        if (target == null) {
>>            throw new BuildException(
>>                "Need to specify target");
>>        }
>>        Vector dependList = getProject().topoSort(
>>            target, getProject().getTargets());
>>        log("Target [" + target + "] depends on");
>>        for (int i = 0; i < dependList.size(); ++i) {
>>            Target t = (Target) dependList.get(i);
>>            log(" - " + t.getName());
>>
>>            if (t.getName().equals(target)) {
>>                break;
>>            }
>>        }
>>    }
>>}
>>
>>Peter
>>
>>On Friday 23 May 2003 13:39, Chris Brown wrote:
>>    
>>
>>>Hello,
>>>
>>>How can I determine which targets will be executed, using the Ant API?
>>>
>>>I'm writing a very specific task that should behave differently
>>>      
>>>
>depending
>  
>
>>>upon what will be done later as the script continues executing.
>>>
>>>It's used in almost all build scenarios, and has a lot of parameters.
>>>I'd like to put it in an "init" target, instead of copying to the start
>>>of
>>>      
>>>
>>lots
>>
>>    
>>
>>>of other targets (to avoid cut-and-paste errors as the project evolves).
>>>
>>>How does Ant track this internally, and how can I access this
>>>information?
>>>
>>>Thanks,
>>>Chris
>>>
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>>>For additional commands, e-mail: user-help@ant.apache.org
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>>For additional commands, e-mail: user-help@ant.apache.org
>>
>>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>>For additional commands, e-mail: user-help@ant.apache.org
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>  
>


Re: How can I determine which targets will be executed, using the Ant API?

Posted by Chris Brown <br...@reflexe.fr>.
I guess that means that there's no way to do this in Ant?  (would confirm my
suspicions).

Here's my use-case, for what it's worth.

I have a version of the <buildnumber> task (totally home-made, but with
similar aims).  I want to increment the build number if I'm going to attempt
to compile a new version of executable code, but don't want to increment it
if I'm only going to run a documentation-related task (for example).  The
documentation-related tasks include the build number in documentation files,
such as Javadocs.  Even if the compile fails, my home-brew <buildnumber>
replacement records this failure in a database.

In case anyone's wondering why I wrote my own version, it's because we work
in a team, compiling versions of the application on different branches of a
CVS repository, so build numbers do not simply increase in a linear way; we
use these serial numbers to track what build corresponds to which version,
and information about the environment in which the build was run.

Thanks again for your help Peter.

- Chris

----- Original Message ----- 
From: "peter reilly" <pe...@corvil.com>
To: "Ant Users List" <us...@ant.apache.org>
Sent: Friday, May 23, 2003 7:13 PM
Subject: Re: How can I determine which targets will be executed, using the
Ant API?


Ah, I see.

Peter
On Friday 23 May 2003 17:49, Chris Brown wrote:
> Hi Peter,
>
> I'm trying to access the list of targets to be executed, not the list of
> ALL targets in the build file.  I'd already looked at the javadocs and the
> source code.... I tried using getTargets(), but that returns a list of all
> targets in the project's build file.
>
> Obviously, if I had the basic list, I could use topoSort(...) to resolve
> dependencies, but I'm aware of that, and that's not the problem here.
>
> The list of targets being executed seems to be stored as local variables
> within method calls, and is not stored as an instance variable in
> "Project.java".  In the source code for Ant 1.5.3, in line 609 of
> org/apache/tools/ant/Main.java, a private instance variable (Vector
> targets) is passed to the "executeTargets" method of Project, but these
> targets are not exposed either in "Main.java" or "Project.java", so I
can't
> tell if a specific target is going to be called (unless of course a
> BuildException occurs).  Furthermore, I don't think it's wise to access
> "Main.java", as it's possible to execute targets in a project directly
> using the API without using "main" (I think so anyway, I've never tried).
>
> Thanks anyway,
> Chris
>
> ----- Original Message -----
> From: "peter reilly" <pe...@corvil.com>
> To: "Ant Users List" <us...@ant.apache.org>
> Sent: Friday, May 23, 2003 6:33 PM
> Subject: Re: How can I determine which targets will be executed, using the
> Ant API?
>
>
> The target info is maintained per project in the Project
> object's targets hashmap;
> Project uses "topoSort()" to sort the targets.
> These are publiclly visable.
>
> The following task uses this information.
> This does not deal with targets generated
> using ant or antcall.
>
> import org.apache.tools.ant.Project;
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.Target;
> import org.apache.tools.ant.BuildException;
> import java.util.*;
>
> public class TargetDepends extends Task {
>     private String target;
>     public void setTarget(String target) {
>         this.target = target;
>     }
>     public void execute() {
>         if (target == null) {
>             throw new BuildException(
>                 "Need to specify target");
>         }
>         Vector dependList = getProject().topoSort(
>             target, getProject().getTargets());
>         log("Target [" + target + "] depends on");
>         for (int i = 0; i < dependList.size(); ++i) {
>             Target t = (Target) dependList.get(i);
>             log(" - " + t.getName());
>
>             if (t.getName().equals(target)) {
>                 break;
>             }
>         }
>     }
> }
>
> Peter
>
> On Friday 23 May 2003 13:39, Chris Brown wrote:
> > Hello,
> >
> > How can I determine which targets will be executed, using the Ant API?
> >
> > I'm writing a very specific task that should behave differently
depending
> > upon what will be done later as the script continues executing.
> >
> > It's used in almost all build scenarios, and has a lot of parameters.
> > I'd like to put it in an "init" target, instead of copying to the start
> > of
>
> lots
>
> > of other targets (to avoid cut-and-paste errors as the project evolves).
> >
> > How does Ant track this internally, and how can I access this
> > information?
> >
> > Thanks,
> > Chris
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org




Re: How can I determine which targets will be executed, using the Ant API?

Posted by peter reilly <pe...@corvil.com>.
Ah, I see.

Peter
On Friday 23 May 2003 17:49, Chris Brown wrote:
> Hi Peter,
>
> I'm trying to access the list of targets to be executed, not the list of
> ALL targets in the build file.  I'd already looked at the javadocs and the
> source code.... I tried using getTargets(), but that returns a list of all
> targets in the project's build file.
>
> Obviously, if I had the basic list, I could use topoSort(...) to resolve
> dependencies, but I'm aware of that, and that's not the problem here.
>
> The list of targets being executed seems to be stored as local variables
> within method calls, and is not stored as an instance variable in
> "Project.java".  In the source code for Ant 1.5.3, in line 609 of
> org/apache/tools/ant/Main.java, a private instance variable (Vector
> targets) is passed to the "executeTargets" method of Project, but these
> targets are not exposed either in "Main.java" or "Project.java", so I can't
> tell if a specific target is going to be called (unless of course a
> BuildException occurs).  Furthermore, I don't think it's wise to access
> "Main.java", as it's possible to execute targets in a project directly
> using the API without using "main" (I think so anyway, I've never tried).
>
> Thanks anyway,
> Chris
>
> ----- Original Message -----
> From: "peter reilly" <pe...@corvil.com>
> To: "Ant Users List" <us...@ant.apache.org>
> Sent: Friday, May 23, 2003 6:33 PM
> Subject: Re: How can I determine which targets will be executed, using the
> Ant API?
>
>
> The target info is maintained per project in the Project
> object's targets hashmap;
> Project uses "topoSort()" to sort the targets.
> These are publiclly visable.
>
> The following task uses this information.
> This does not deal with targets generated
> using ant or antcall.
>
> import org.apache.tools.ant.Project;
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.Target;
> import org.apache.tools.ant.BuildException;
> import java.util.*;
>
> public class TargetDepends extends Task {
>     private String target;
>     public void setTarget(String target) {
>         this.target = target;
>     }
>     public void execute() {
>         if (target == null) {
>             throw new BuildException(
>                 "Need to specify target");
>         }
>         Vector dependList = getProject().topoSort(
>             target, getProject().getTargets());
>         log("Target [" + target + "] depends on");
>         for (int i = 0; i < dependList.size(); ++i) {
>             Target t = (Target) dependList.get(i);
>             log(" - " + t.getName());
>
>             if (t.getName().equals(target)) {
>                 break;
>             }
>         }
>     }
> }
>
> Peter
>
> On Friday 23 May 2003 13:39, Chris Brown wrote:
> > Hello,
> >
> > How can I determine which targets will be executed, using the Ant API?
> >
> > I'm writing a very specific task that should behave differently depending
> > upon what will be done later as the script continues executing.
> >
> > It's used in almost all build scenarios, and has a lot of parameters. 
> > I'd like to put it in an "init" target, instead of copying to the start
> > of
>
> lots
>
> > of other targets (to avoid cut-and-paste errors as the project evolves).
> >
> > How does Ant track this internally, and how can I access this
> > information?
> >
> > Thanks,
> > Chris
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


Re: How can I determine which targets will be executed, using the Ant API?

Posted by Chris Brown <br...@reflexe.fr>.
Hi Peter,

I'm trying to access the list of targets to be executed, not the list of ALL
targets in the build file.  I'd already looked at the javadocs and the
source code.... I tried using getTargets(), but that returns a list of all
targets in the project's build file.

Obviously, if I had the basic list, I could use topoSort(...) to resolve
dependencies, but I'm aware of that, and that's not the problem here.

The list of targets being executed seems to be stored as local variables
within method calls, and is not stored as an instance variable in
"Project.java".  In the source code for Ant 1.5.3, in line 609 of
org/apache/tools/ant/Main.java, a private instance variable (Vector targets)
is passed to the "executeTargets" method of Project, but these targets are
not exposed either in "Main.java" or "Project.java", so I can't tell if a
specific target is going to be called (unless of course a BuildException
occurs).  Furthermore, I don't think it's wise to access "Main.java", as
it's possible to execute targets in a project directly using the API without
using "main" (I think so anyway, I've never tried).

Thanks anyway,
Chris

----- Original Message ----- 
From: "peter reilly" <pe...@corvil.com>
To: "Ant Users List" <us...@ant.apache.org>
Sent: Friday, May 23, 2003 6:33 PM
Subject: Re: How can I determine which targets will be executed, using the
Ant API?


The target info is maintained per project in the Project
object's targets hashmap;
Project uses "topoSort()" to sort the targets.
These are publiclly visable.

The following task uses this information.
This does not deal with targets generated
using ant or antcall.

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.BuildException;
import java.util.*;

public class TargetDepends extends Task {
    private String target;
    public void setTarget(String target) {
        this.target = target;
    }
    public void execute() {
        if (target == null) {
            throw new BuildException(
                "Need to specify target");
        }
        Vector dependList = getProject().topoSort(
            target, getProject().getTargets());
        log("Target [" + target + "] depends on");
        for (int i = 0; i < dependList.size(); ++i) {
            Target t = (Target) dependList.get(i);
            log(" - " + t.getName());

            if (t.getName().equals(target)) {
                break;
            }
        }
    }
}

Peter
On Friday 23 May 2003 13:39, Chris Brown wrote:
> Hello,
>
> How can I determine which targets will be executed, using the Ant API?
>
> I'm writing a very specific task that should behave differently depending
> upon what will be done later as the script continues executing.
>
> It's used in almost all build scenarios, and has a lot of parameters.  I'd
> like to put it in an "init" target, instead of copying to the start of
lots
> of other targets (to avoid cut-and-paste errors as the project evolves).
>
> How does Ant track this internally, and how can I access this information?
>
> Thanks,
> Chris
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org





Re: How can I determine which targets will be executed, using the Ant API?

Posted by peter reilly <pe...@corvil.com>.
The target info is maintained per project in the Project
object's targets hashmap;
Project uses "topoSort()" to sort the targets.
These are publiclly visable.

The following task uses this information.
This does not deal with targets generated
using ant or antcall.

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.BuildException;
import java.util.*;

public class TargetDepends extends Task {
    private String target;
    public void setTarget(String target) {
        this.target = target;
    }
    public void execute() {
        if (target == null) {
            throw new BuildException(
                "Need to specify target");
        }
        Vector dependList = getProject().topoSort(
            target, getProject().getTargets());
        log("Target [" + target + "] depends on");
        for (int i = 0; i < dependList.size(); ++i) {
            Target t = (Target) dependList.get(i);
            log(" - " + t.getName());
            
            if (t.getName().equals(target)) {
                break;
            } 
        }
    }
}

Peter
On Friday 23 May 2003 13:39, Chris Brown wrote:
> Hello,
>
> How can I determine which targets will be executed, using the Ant API?
>
> I'm writing a very specific task that should behave differently depending
> upon what will be done later as the script continues executing.
>
> It's used in almost all build scenarios, and has a lot of parameters.  I'd
> like to put it in an "init" target, instead of copying to the start of lots
> of other targets (to avoid cut-and-paste errors as the project evolves).
>
> How does Ant track this internally, and how can I access this information?
>
> Thanks,
> Chris
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org