How do I get the target as a property in Ant? - ant

Possibly one of those really terrible beginner questions where the manual will tell you everything, but, anyway, take this line below:
ant -Dfoo=bar buildme
in my build script, what is the property that holds "buildme"?

The list of targets invoked is available in the property
ant.project.invoked-targets
If there is a default target specified, then that will be the invoked target. If one or more targets are specified on the command line, these appear comma-separated in the property. Note that the property only becomes set once execution passes to a target - if you try to read the property outside of any target, it will be unset.
So, if the project has a default target 'zero':
$ ant one two
# ant.project.invoked-targets is set to:
one,two
and
$ ant
# ant.project.invoked-targets is set to (default):
zero

Seems like ant.project.invoked-targets is not available in ant 1.7.1

Not sure I understand your question, but "buildme" is the target to execute, not a property.
ant [options] [target [target2 [target3] ...]]
You "pick it" by creating the corresponding target:
<target name="buildme">
<!-- tasks that will execute here -->
</target>
As for the foo property, you "pick it" by using ${foo}.

Related

calling a target from a particular directory

I have script that call the below target but before calling this target i want that i should be at the following location which is stored in the below variable ..
todir="${release.deployment.tool}
the target that is called is shownb below..
<target name="deploy-ion" description="Used to deploy to a given aaa.">
so finally when my target deploy-ion is being called i should make sure that I should be in
directory stored in todir , please advise how to achieve this.
I meant you can use absolute path of your property reference while performing set of tasks inside your target, as by default it uses basedir default value as cur-dir or one which is specified at project level at beginning of your build.xml file.
Also alternatively you can use ant's ant task : https://ant.apache.org/manual/Tasks/ant.html with usage like
<ant dir="${release.deployment.tool}" target="deploy-ion" />

Alias/synonym for ant target

I have an ant target called "unittest" which I find clunky and I would like to use "test" instead. I need to keep the name "unittest" around though because other people also use the ant build file.
Is there a way that I could add aliases or synonyms for particular targets?
I tried using a comma separated list as possible in other attributes such as depends but that this not work. I get Target "test" does not exist in the project ...
<target name="test,unittest">
I know that I could simulate this using depends but I want to avoid the extra unnecessary output this produces.
<target name="test" depends="unittest">
Is there any way to achieve this? I will be fine with accepting the depends approach if there really is no better way.
Move the contents of the unittest target into a macrodef and replace the contents of the unittest target with a call to the macro. Create a test target calling the same macro. Job done.

Run ant target multiple times but replace fileset it refers to

I have an Ant target which runs unit tests on a fileset with a specific name, hardcoded inside of the target.
I would like to call that target multiple times, each time setting the fileset to a new value. I cannot change the name of the fileset which targets looks up, I can only define the fileset prior to calling a target.
Is it possible to call a target like this, and re-define same fileset during each target call?

Manually Start ANT Task

For various reasons that I won't go into (I promise it's necessary to do this with the current code base; I know it's goofy), I want to execute a target twice in the same build task.
For example, I want to execute the target foo, then bar, then foo again. This is a simplified version of what I already tried:
<target name="foo">
...
</target>
<target name="bar" depends="foo">
...
</target>
<target name="project" depends="foo,bar">
...
</target>
In this case when executing the project target, foo only ran once. I also tried getting rid of the depends attribute on the bar target and making the project's depends attribute "foo,bar,foo", but still the same result.
Is there a way to force a task to execute, even if it's already successfully completed? Or is there a better way to go about this?
The antcall task allows you to explicitly call a target.
I think that's a better solution than using the depends mechanism. As you've identified, this determines what has already run. Antcall instructs the target to run regardless of whether it's run before.
You can parameterise the call to customise what it does on each invocation.

Override the compiler attribute in an Ant javac task

I'm trying to override Ant compiler attributes via the command line so that all 'javac' tasks use my specified compiler.
The problem I've run into is that any target that sets its own value for compiler overrides the one that I set at the commmand line. So, even though I'm entering the following command.
ant -Dbuild.compiler=mycompiler
Any target that has the following is going to use the modern compiler instead of mycompiler because of that compiler="modern" attribute
<javac srcdir="."
destdir="${classes.dir}/core"
compiler="modern"
encoding="UTF-8">
<include name="org/**" />
<include name="com/**" />
<compilerarg line="${config.build.compilerarg}" />
</javac>
Is there any way to override this from the command line, or am I stuck editing the build file?
The Ant javac task documentation says:
It is possible to use different compilers. This can be specified by either setting the global build.compiler property, which will affect all tasks throughout the build, or by setting the compiler attribute, specific to the current task. Valid values for either the build.compiler property or the compiler attribute are:
It sounds as if you can either specify the global build.compiler property or set a specific compiler attribute.
So, it looks like you will need to modify your build file and either:
remove the compiler attribute from the javac calls and allow the
global build.compiler setting to
cascade down
change the values of the compiler
attribute from a hard-coded string
compiler="modern" to be property
compiler="${javac.compiler}"

Resources