Post

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 -

1

For actually using it -

1

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

This post is licensed under CC BY 4.0 by the author.