27

The task name starts with a hyphen "-".

<?xml version="1.0" encoding="UTF-8"?>
<project name="proj">
    <target name="-task1">
        <echo>Done!</echo>
    </target>
</project>

How can I specify this task when running ant script from command line? This would not work:

ant -task1 -f test.xml
AlBlue
  • 23,254
  • 14
  • 71
  • 91
alex2k8
  • 42,496
  • 57
  • 170
  • 221

3 Answers3

28

Enclose the task name in quotes.

ant "-task1" -f test.xml

Update: From Ant docs

Targets beginning with a hyphen such as "-restart" are valid,
and can be used to name targets that should not be called directly
from the command line.
For Ants main class every option starting with hyphen is an option for Ant itself
and not a target. For that reason calling these target from command line is not
possible. On the other hand IDEs usually don't use Ants main class as entry 
point and calling them from the IDE is usually possible.
Jim
  • 22,354
  • 6
  • 52
  • 80
  • 5
    This would not work, at least on Windows. The error messages is: Unknown argument: -task1 – alex2k8 Nov 23 '10 at 14:52
  • 2
    Sorry this wasn't helpful, was tested on a linux box. – Jim Nov 23 '10 at 14:54
  • Wonder if it even possible. May be Android team used "-" to make some tasks invisible from outside. – alex2k8 Nov 23 '10 at 15:04
  • 3
    Nope. The quotes are the purview of the shell, which will make that into [ant] [-task1] when executing the command, which is the same problem. If quoted rule name works for you, then unquoted also works for you. – Chad Dec 20 '12 at 15:55
  • 1
    Good information about why some tasks are started with -, but I think the correct answer is the one provided by David W. I'm not sure why enclosing it in quotes worked - I would think that would not work even under UNIX. – skiphoppy Jun 19 '14 at 19:57
  • Doesn't work for me on CentOS with Ant 1.9.2. Ant just treated the target with dash as an option. – Hei Sep 01 '17 at 14:01
  • See Heefan's answer. – froggythefrog Dec 26 '18 at 18:12
13

Some people start internal targets with dashes just to make sure users cannot run them from the command line. In fact, I make it a standard practice to have all internal targets start with - just for this reason.

You can try the old double-dash trick. I don't have Ant installed on my current system, so I can't test it. Double dashes is a common Unix trick that most commands use to help end parameters when you have files and stuff that start with a dash. By the way, the tasks should be the last thing on your command line:

$ ant -f test.xml -- -task1

Worse comes to worse, you can simply define another target in your build.xml file that depends upon this target with the dash in it:

<task name="sneaky"
    depends="-task1"/>

Then you should be able to call sneaky:

$ant -f test.xml sneaky
David W.
  • 105,218
  • 39
  • 216
  • 337
  • 5
    Sadly the double-dash trick doesn't seem to work. I tested it on Windows and Linux and just get: `Unknown argument: --` – Martin McNulty Oct 25 '16 at 15:04
  • @MartinMcNulty Doesn't work for me on CentOS with Ant 1.9.2 neither. Ant just treated the target with dash as an option. – Hei Sep 01 '17 at 14:02
  • 1
    Yes, the double dash is not processed by the shell but given to the called command like any other argument. If `ant` doesn't handle that in a particular way, it's not going to help. – David Tonhofer Jan 06 '18 at 15:08
6

From the ANT target doc

Targets beginning with a hyphen such as "-restart" are valid, and can be used to name targets that should not be called directly from the command line. For Ants main class every option starting with hyphen is an option for Ant itself and not a target. For that reason calling these target from command line is not possible.

So, user cannot call the target with hyphen from commandline.

Tested on Windows Platform on 21 April 2016.

Heefan
  • 385
  • 2
  • 8