Why in "ant -?" targets are written in this way: [target [target2 [target3] ...]]? - ant

Why when you type
ant -?
in cmd, targets are written in this way?
ant [options] [target [target2 [target3] ...]]
Why not like this?
ant [options] [target] [target2] ... [target3]

You cannot have a second target if you don't have the first - the target you supply would be the first. So this notation [target [target2 [target3] ...]] should be read as follows:
you can have an optional target.
if you do have a target, you could optionally have a second target.
if you have two targets, you could optionally have a third target.
etc...

Related

Specify "--build_python_zip" flag within Bazel py_binary rule

Is it possible to specify the bazel "--build_python_zip" flag from within the py_binary rule so that I don't need to add this flag every time I use Bazel in my workspace?
It doesn't seem that there's a way to specify this per py_binary target, according to this issue.
However, you can use a bazelrc file to store common options. Add the following line to your workspace's bazelrc at <workspace>/.bazelrc:
build --build_python_zip
Note that using this method, every invocation of bazel build, bazel test and bazel run will include this flag.
After PR 9453, you can:
filegroup(
name = "foo_zip",
srcs = [":foo_binary"],
output_group = "python_zip_file",
)
py_binary(
name = "foo_binary",
srcs = ["foo.py"],
)
Then you can invoke bazel as:
bazel build :foo_zip
and don't have to specify --build_python_zip. This will also allow par_binary rules to co-exist with native zips.

Create ant target with command line arguments

I'm doing an ant XML to manage repositories and some stuff, and I want to run a target passing command line arguments like this :
ant deploy-stable 0.0.1
How can I configure my target to receive "0.0.1" as argument ?
<target name="deploy-stable">
<!-- Run git targets, previously defined, using the version passed by command line -->
</target>

How to pass an argument to a dependency in Ant Build Tool

I'm creating a target in the build.xml file which depends on three other targets:
<target name="test" depends="target1,target2,target3">
<echo message="Build Successful!"/>
</target>
What I'm trying to do here is to pass some argument to 'target2'. Can this be done in Ant? If so, how can I do it?
Any help is truly appreciated!!
Do you understand how depends works?
When you call target test, it will run the following targets:
target1
target2
target3
test
In that order. There's no way for target test to pass any arguments to target2.
You can pass properties to your build.xml script using the -D parameter:
$ ant -Dsource.version="1.5" compile
When your build script is executed, the property source.version will be set to 1.5.
Otherwise, look at macrodef task as a way of passing arguments to particular routines.
By the way, if I had more information what you want, I could give you a better answer.

Conditional execution in Ant

I have a target, that is executed if my_step==true:
<target name="pre-compile" if="my_step">
...
</target>
But I want to make that pre-compile target available regardless of the value of my_step, so that I could execute my action manually using ant do_my_step:
<target name"-do_my_step">
...
</target>
Question. How can I run make pre-compile execute -do_my_step target?
That is, if property my_step is true, then pre-compile step will execute -do_my_step target.
Obviously, I could simply copy-paste contents of -do_my_step target into pre-compile target, but I want to keep my target cleanly separated.
Target names with prefix '-' are a common practice to make the target somewhat 'private' as it's impossible to call it via commandline. ant -f yourbuildfile.xml -yourprivatetarget won't work as ant commandline interface uses leading '-' for options. So strip the leading '-' from your target name to call it via ant -f yourbuildfile.xml do_my_step
Also consider :
"..Question. How can I run make pre-compile execute -do_my_step target? .."
Ant has antcall task for calling a target within the same buildscript. But antcall should be avoided because it opens a new project scope (so it needs more memory and will slow down your build) and breaks the dependency chain which is normally built via <target name="..." depends"=..."> . antcall is superfluous since ant 1.6 introduced macrodef for that purpose (reuse of functionality).
<target name="pre-compile" if="my_step" depends="-do_my_step">
...
</target>
When pre-compile is called, it will run -do_my_step before.

How can I get a list of build targets in Ant?

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?
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).
To get all the targets in the build file
ant -p -verbose
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)
You can check the list of target and default target in build.xml by the following command
ant -p built.xml

Resources