Is there a way to mask the password when using the <input .../>
task in ANT from the Eclipse IDE ?
I see a way to do it from the command line:
<input message="secure-input:" addproperty="the.password">
<handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>
But this doesn't work in eclipse.
EDIT : As ant secure Input handler still doesn't work in eclipse 4.x, here's a solution based on ant script task, working with builtin javascript engine (since JDK 1.6.0_06), so no extra libraries needed :
<project>
<script language="javascript">
// imports
importClass(javax.swing.JPasswordField);
importClass(javax.swing.JOptionPane);
var pw = new JPasswordField();
var choice = JOptionPane.showConfirmDialog(null, pw, "Enter Password..", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (choice == JOptionPane.OK_OPTION) {
// create ant property
project.setNewProperty("pwd", pw.getText());
} else {
throw "Password required !!";
}
</script>
<echo>$${pwd} => ${pwd}</echo>
</project>
The method JPasswordField.getText() is deprecated, normally you would use getPassword(), but it doesn't work.
// create ant property
var s = new String(pw.getPassword());
project.setNewProperty("pwd", s);
takes only the chararray, whereas :
// create ant property
var s = String.valueOf(pw.getPassword());
project.setNewProperty("pwd", s);
results in :
[echo] ${pwd} => function String() { [native code for String.String, arity=1] }
maybe a bug in the javascript engine !? (using jdk 1.7.0_60)
Since version 1.7.1 Ant has support for Java 1.6's secure console input feature, see Ant Manual . What version of Eclipse and Java do you use ? Alternatively you may use :
AntForms which has a lot of input dialogs and support for passwords too
or
Jera Ant Tasks which has a query task with optional password masking
EDIT : adding a specific example using groovy..
You may use a scripting language to open a dialog, here is an example using Groovy =
<project>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
import groovy.swing.SwingBuilder
import javax.swing.JFrame
boolean isAlive = true
swing = new SwingBuilder()
button = swing.button('OK')
frame = swing.frame(title:'Password', defaultCloseOperation:JFrame.EXIT_ON_CLOSE) {
panel {
pw = passwordField(columns:10)
widget(button)
}
}
button.actionPerformed = {
// set Ant property for further processing
properties.'password' = pw.text
isAlive = false
}
frame.pack()
frame.show()
// prevent ant from closing the window
while(isAlive) {
sleep(1000)
}
</groovy>
<echo>$${password} = ${password}</echo>
</project>
Finally write your own Inputhandler, see http://sourceforge.net/projects/emaria/files/antdocs/antinput/antinput.pdf/download for details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.