How to eliminate leading [java] label from Ant's <java> task output? - ant

I'm invoking the <java> task using Ant to run a program that prints out some stuff to stdout that ultimately, I'd like the user to be able to copy-paste easily. However, each line of stdout is prefixed with [java], which makes things needlessly challenging for the user.
Is there some way to print just the output of System.out.println(...) without getting prefixed with [java]?

Set ANT_ARGS=-emacs, see ant FAQ:
Ant adds a "banner" with the name of the current task in front of all
logging messages - and there are no built-in regular expressions in
your editor that would account for this.
You can disable this banner by invoking Ant with the -emacs switch.
[...]
Also ant manual Running Apache Ant listing all cli options might be helpful :
-emacs, -e produce logging information without adornments

Related

Set a system property with ant

I have an ant script which has a taskdef and the task creates an https internet connection and somethin with that SSL stuff is wrong. Thus I want to set the system property javax.net.debug=all to get some more information.
In java I would do this using the -D option, but in ant this is used for ant properties which is not the same as a system property.
If this wouldn't be a taskdef but instead a java task, I could use the sysproperty property, but it is no java-task.
Googling for this is frustratingly complicated because ant properties and system properties in ant are so similar that most search results are about the other (or about the java-task).
Obviously I am not the only one with the problem, but other people's questions that I have found (like here) are unanswered or went for hack (like here).
One way to set such a property is the ANT_OPTS system variable. You have to be very carefully to not simply skim over answers on google that state that options can be set that way, because it sounds so much like not what it does:
The documentation says:
ANT_OPTS - command-line arguments that should be passed to the JVM.
For example, you can define system properties or set the maximum Java
heap size here.
Who what have expected that? ANT_OPTS are options for the JVM and not for ant like the name suggests. The var which is used for ant options is called ANT_ARGS.
Now I can launch ant like this: ANT_OPTS="-Djavax.net.debug=all" ant myTarget and can see tons of log output.
(However this leaves the question open whether such a variable can be set using XML).
You can declare system properties in the xml with <sysproperty key="key" value="value"/>.
http://www.java2s.com/Code/Java/Ant/SetsystempropertiesinAntbuildscript.htm
You can use scripting:
<script language="javascript">
java.lang.System.setProperty('myKey', 'myValue');
</script>

echoproperties task depending on log level

Is there a chance to make the echoproperties task depend on the log level (something similar to the level parameter in echo task)? It would be helpful to have some echoproperties outputs only when ant runs in debug mode (-debug).
Alternative: how can I detect in Ant the current log level? Is there e a property that contains the log level?
Thx!
Frank
Consider using ant record task for this use case.
See How do I capture the output of an antcall inside of ant?.

Enable verbose output from groovy's AntBuilder

Is there a way to turn debug/verbose output on for groovy's AntBuilder? In other words, I want to enable more detailed output like one would get with ant -verbose from the command line.
I know about the recorder task, as described here, but this writes to a file, and I want to write to stdout or possibly a logger (like the one provided for maven plugins written in groovy).
As inspired by the first result from google (rewritten for Groovy conciseness):
ant.project.buildListeners[0].messageOutputLevel = 4
Values are documented here, with 4 being most verbose.

How to get the return code from cccheckout task in ant

Im trying to write a logic that depends on the success or failure of cccheckout command. Is there something similar to returnProperty attribute as in exec task?
Thanks,
Aarthi
Looking at the CCCheckout documentation, I would rather use the exception mechanism to process any failure.
failonerr
Throw an exception if the command fails. Default is true.
Since ant 1.6.1
In ant, that means you can separate your ant process in two (one if no failure, one one exception), using the ant trycatch task.
It's possible you're asking that question because of cleartool.exe behaves strangly
sometimes, means it returns RC -1 even if no real error occured.
Means using cccheckout with failonerr="true" would sometimes cause an unneccessary Build failed
as any RC != 0 is handled as error by exec task.
But you might use the <exec> task directly with executable cleartool.exe and set attribute
resultproperty to make the RC available as property
outputproperty to make stdout available as property
errorproperty to make stderr available as property
for further processing, i.e. use condition task to check those properties..
Some try/catch/finally feature provided by Ant addons like :
AntContrib
Flaka
might come in handy, as VonC already mentioned.
If it gets more complicated afterwards use Groovy ant task or script task.

Is it possible to display the call "graph" of an ant extension-point?

I have an extension-point defined in ant :
<extension-point name="foo"/>
A lot of tasks contribute to this point in several imported ant files :
<bindtargets targets="bar" extensionPoint="foo" />
However I'm kinda lost as to exactly which tasks are contributing. Is there a way to have ant report the tasks that would be triggered by a given extension point ? More generaly, is there a way to display the "call-graph" (or simply the list of dependencies) of an ant task ?
I tried using verbose options for ant (-v and such), with no luck.
Thanks
First of all, you can try to debug the ANT process in your IDE using remote debugging by adding some parameters to ANT_OPTS (mine is set in ~/.profile):
http://blog.dahanne.net/2010/06/03/debugging-any-java-application/
And profiling may help. I found project Antro on ANT Wiki...
http://sourceforge.net/projects/antro
Maybe you can try it out. The project is said to be designed for ANT, which looks promising in solving your problem.
Also you can use Yourkit Java Profiler to do a CPU profiling. YJP can show the call graph of a java application, but I'm not sure if one can find out which are ANT targets.
The following document shows how to start a java application with YJP agent.
http://www.yourkit.com/docs/95/help/agent.jsp
I know of 2 ways to get this information:
You can get the effective target/extension-point invocation sequence from Ant's console logger. To do this, place Ant's logging facility into verbose mode by passing -verbose on the command line to Ant. There are two lines, one after the other, that dump to the console immediately before most targets as they are invoked in your build script:
A line that shows a summary of the targets in the call sequence starting with the text, Build sequence for target(s) 'artifact' is [...].
A line showing the detailed call sequence (nested targets and antcalls included). This line starts with the text, Complete build sequence is [...]. This listing considers, as much as reasonably possible, the evaluation of any if and unless attributes of each target listed at the point the line is logged to the console.
Simply invoke your Ant build as you would normally with the -verbose option and your console should have the information you're looking for.
You can get a pictorial representation of the call sequence using a tool called Grand. However, it hasn't been updated for quite some time and thus doesn't support extension-points (which is what you're concerned with here). It will interpret antcall's, ant, and depend'encies. It doesn't evaluate the if and unless attributes but simply identifies potential execution sequence - more of a dependency hierarchy than an actual call graph. The project is on Github so an update to support extension-points may not be too difficult.
The graphic is rendered using Graphviz.
For an actual call sequence, use option 1.
This is pretty sloppy, but it works. Ant is actually pretty easily scripted, and if you are using at least Java 6 (or it might be Java 7), javascript support is built in and thus can be used right out of the box. This defines a task that will echo the dependencies of any target in call order:
<scriptdef name="listdepends" language="javascript">
<attribute name="target"/>
<![CDATA[
var done = [];
var echo = project.createTask("echo")
function listdepend(t) {
done.push(t.getName());
var depends = t.getDependencies();
while (depends.hasMoreElements()) {
var t2 = depends.nextElement();
if (done.indexOf(t2)==-1) listdepend(project.getTargets().get(t2));
}
echo.setMessage(t.getName());
echo.perform();
}
var t = attributes.get("target");
if (t!=null) {
var targ = project.getTargets().get(t);
listdepend(targ);
}
]]>
</scriptdef>
In your case, you can create a new target (or not) and call it like so:
<target name="listfoo">
<listdepends target="foo"/>
</target>
As I said, this is somewhat sloppy. It probably isn't very fast (although unless your target triggers thousands of others, it probably isn't noticeably slow). It won't handle antcall tasks (although it could be modified to do so easily) or respond to if and unless attributes. If dependencies nest too far, it may hit a recursion depth limit (but I doubt any project nest them deep enough).
The array is used to make sure that each dependency is listed once (ant would only run them once).

Resources