Ant Input and Passwords

Ant input task does not has a feature which will allow not to echo what user inputs, a necessity in case you are prompting for a password. As per the ant input task documentation

A regular complaint about this task is that it echoes characters to the console, this is a critical security defect, we must fix it immediately, etc, etc. We know it leaves something to be desired, but the problem is Java, not Ant. There is nothing we can do to stop the console echoing.

But as of JDK 1.6, which allows you to read password using console, I have created following ant task which allows you to prompt a message to user and accept a password and it will be set as a ant property.

You can also use the same to accept normal input.


package com.shivprasad.anttask;
import java.io.Console;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class YetAnotherInputTask extends Task {

	private String outputproperty;
	private String message;
	private Boolean password;
	
	public void setPassword(String password) {
		this.password = Boolean.parseBoolean(password);
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setOutputproperty(String outputproperty) {
		this.outputproperty = outputproperty;
	}
	
	@Override
	public void execute() throws BuildException {
		Console cons = System.console();
		String text=null;
		if (cons != null){
			if(password.booleanValue()){
				char[] passwd = cons.readPassword("[yainput] %s", message);
				text = new String(passwd);
			}else{
				text = cons.readLine("[yainput] %s", message);
			}
			getProject().setProperty(outputproperty, text);
		}
	}
}

You can download above as a jar.

Download ya-ant-input-task.jar

In order to use it in ant, you will have to have the above jar as part of classpath. Then define, ant task –


        <taskdef name="yainput" classname="com.shivprasad.anttask.YetAnotherInputTask">
            <classpath>
                <fileset dir="${dependency.lib}">
                    <include name="ya-ant-input-task.jar"/>
                </fileset>
            </classpath>
        </taskdef>

For actually using it –


    	<yainput outputproperty="db.tns" message="Enter the TNS entry for the database : " password="false" /> 
    	<yainput outputproperty="db.password" message="Enter the password for the database : " password="true" /> 

The outputproperty is the property which will be available in ant after user enters the text.

Updated: Above code is licensed under : http://ant.apache.org/license.html




Comments

  1. Matt StineNo Gravatar May 19th

    Comment Arrow

    This is awesome! Exactly what I needed for an upcoming demo at CommunityOne. Thanks!


  2. Richard Bullington-McGuireNo Gravatar August 4th

    Comment Arrow

    Would you be willing to explicitly license this code under the Apache 2.0 License?

    http://ant.apache.org/license.html

    That way it would be compatible with the Ant license and could be used anywhere the Ant source code is used.


  3. shivNo Gravatar August 4th

    Comment Arrow

    @Richard

    I hereby explicitly license this code under Apache 2.0 License.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

shiv

This author has not yet written a description. Please give them some time to get acquainted with the site and surely they will write their masterpiece.