Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ant quiet without the -q flag?

I have an ant buildfile that is often run from vastly different environments. By default, I'm looking for the same behavior as using:

ant -q

However, since some team member's configurations vary, specifying the -q option in each person's environment is not easily accomplished in a uniform way (some people run ant from eclipse, some from the command line, some from debugging/profiling tools, etc. Each with a different method for specifying ant arguments like -q)

So I'm seeking a way for the ant file to call itself quietly...

Something like the following would be ideal:

<target name="default">
    <antcall quiet="yes" target="build" /> <!-- doesn't work -->
</target>

Can anyone think of anyway to accomplish something like this? All I'm after is for the build to run quietly whenever the default target is ran, regardless of whether -q is set.

like image 901
gMale Avatar asked Mar 28 '11 17:03

gMale


People also ask

How do you create an ant command?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

Does ant use Log4j?

Message events are logged according to their Ant logging level, mapping directly to a corresponding Log4j level. To use Log4j you will need the Log4j JAR file and a log4j. properties configuration file. Both should be placed somewhere in your Ant classpath.

What is Setproxy?

The setproxy task can be used to explicitly set a proxy in a build file. This manipulates the many proxy configuration properties of a JVM, and controls the proxy settings for all network operations in the same JVM from that moment.

Which one is true code for delete the directory in ant?

This task is used to delete a single file, directory or subdirectories. We can also delete set of files by specifying set of files. It does not remove empty directory by default, we need to use includeEmptyDirs attribute to remove that directory.


2 Answers

Based on other answers:

<macrodef name="quiet">
    <element name="body" implicit="yes"/>
    <sequential>
        <script language="javascript">
            project.getBuildListeners().firstElement().setMessageOutputLevel(0);
        </script>
        <body/>
        <script language="javascript">
            // TODO: restore last log level
            project.getBuildListeners().firstElement().setMessageOutputLevel(2);
        </script>
    </sequential>
</macrodef>

<target name="test-quiet">
    <quiet>
        <echoproperties/>
    </quiet>
</target>
like image 185
Vadzim Avatar answered Sep 25 '22 11:09

Vadzim


One option might be to set the logging level from within the target.

You can access loggers by means of a short script task. Something like:

<target ... >
    <script language="javascript">
        var logger = project.getBuildListeners( ).firstElement( );
        logger.setMessageOutputLevel( 0 );
    </script>
    ...
</target>

I'm not familiar with how Eclipse calls Ant, but it might be necessary to iterate over all the build listeners to get 'silence' all round.

Suggest that how ever you end up doing this, you make it easy to switch back to verbose running.

Edit - response to comment: You can access project properties from within the script using project.getProperty():

<property name="verboseFlag" value="1" />
<script language="javascript">
    var logger = project.getBuildListeners().firstElement();
    var verboseMode = project.getProperty( "verboseFlag" )
    if ( ! "1".equals( verboseMode ) )
        logger.setMessageOutputLevel( 0 );
</script>

(Somewhat old) API docs are here, including for the project class.

like image 20
martin clayton Avatar answered Sep 22 '22 11:09

martin clayton