209

My codebase has a long build.properties file written by someone else. I want to see the available built targets without having to search through the file manually. Does ant have a command for this - something like ant show-targets - that will make it list all the targets in the build file?

Brighid McDonnell
  • 4,293
  • 4
  • 36
  • 61
Shwetanka
  • 4,976
  • 11
  • 44
  • 68

4 Answers4

279

The -p or -projecthelp option does exactly this, so you can just try:

ant -p build.xml

From ant's command line documentation:

The -projecthelp option prints out a list of the build file's targets. Targets that include a description attribute are listed as "Main targets", those without a description are listed as "Other targets", then the "Default" target is listed ("Other targets" are only displayed if there are no main targets, or if Ant is invoked in -verbose or -debug mode).

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • 36
    To make this the default when just invoking `ant`, create a target like ` ` and make "help" the default target in your project. – sschuberth Oct 18 '12 at 07:41
  • 19
    I think it's worth noting that `ant -p` will only show targets that have descriptions. To show every target associated with a `build.xml` file, you need to run `ant -p -v` Also, `ant -p build.xml` is redundant. `ant -p` will do the same thing, and if you're not in the `build.xml` directory, you'll need to use `ant -p -buildfile path/to/build.xml`, anyway. – Andrew Feb 26 '13 at 13:25
  • 1
    the `target name="help"` thing works fine, but not when run within Eclipse Ant environment (it just hangs, but `-diagnostics` or others are ok with it) :-( (no vm fork, Kepler with ant 1.8.4 (2014-05-22) and also tried with ant 1.9.4 (2014-04-29)) (just if you ask yourself ... I was creating a standalone ant env which I'd like to test within Eclipse ... of course I've got my *Ant View* there) – Andreas Covidiot Jul 23 '14 at 12:23
36

To get all the targets in the build file

ant -p -verbose

Shweta
  • 924
  • 14
  • 23
  • 1
    we need to use -p cominded with -v If we require all targets (by all targets i mean the targets without description in them as well) . if Only the main targets is required ( by Main targets i mean the ones with description in them ) using -p alone does the trick. – Wills Jan 17 '15 at 10:05
  • 2
    I couldn't see the targets that i need with only -p, this works perfect. – JacopKane Aug 31 '15 at 14:50
16

The -p or -projecthelp option does exactly this, so you can do:

ant -p build.xml

You can make a target to invoke this like:

<target name="help">
    <java classname="org.apache.tools.ant.Main">
        <arg value="-projecthelp" />
        <arg value="-buildfile" />
        <arg value="${ant.file}" />
    </java>
</target>

which you can then set as the default, so just typing ant will list the available targets.

(Combining @Grodriguez' answer and @sschuberth's comment - I thought it was worth an answer by itself)

rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • 5
    small suggestion. make "help" target as default. As a result running "ant" will invoke "help" target that will print all available targets. – user1697575 Aug 07 '15 at 13:49
2

You can check the list of target and default target in build.xml by the following command

ant -p built.xml