Password input using Eclipse, Ant and AntForms
Some Ant tasks such as ftp and scp require the name and password of a user. Certainly you don’t want to have passwords in plaintext form in your build scripts. Ant’s input task can be used to read input such as passwords from the console, which is more safe. Unfortunately it also echoes the entered values back to the console in plaintext form, which might not be wanted. To prevent this, one could use a SecureInputHandler as desribed in a stackoverflow discussion. However, this does not work when envoked from within Eclipse, since it will just hang and keep on listening to your input no matter how often you press Enter. As far as I know, there is no direct solution to this, but there do exist some alternatives. I decided to use AntForms instead of input, since it’s comfortable to use and easy to setup. “AntForm is a java tool designed to add interaction to Ant scripts through graphical forms.” I will show you how to make a simple dialogue that asks the user to enter his user name and password. The input values will be stored in Ant properties, so that they can be used in further tasks such as ftp and scp. Just follow these steps:
- Download the latest AntForms release from sourceforge and extract it to a location of your choice. Let’s suppose the lib directory to be located in E:\Development\antform-bin-2.0\lib\.
Put this simple build.xml in one of your Eclipse projects:
<?xml version="1.0" encoding="UTF-8"?> <project name="name-of-project" default="build"> <taskdef name="antform" classname="com.sardak.antform.AntForm" classpath="E:\\Development\\antform-bin-2.0\\lib\\antform.jar"/> <target name="build"> <antform title="Credentials"> <label>Please enter your user name and password.</label> <textProperty label="User name:" property="userName" required="true" /> <textProperty label="User password:" property="userPass" required="true" password="true" /> </antform> <echo message="The user name is: ${userName}"/> <echo message="The user password is: ${userPass}"/> </target> </project>
Right click build.xml and choose Run As > Ant Build. A simple dialogue appears:
Enter some credentials, click OK and have a look at the Eclipse console:
Buildfile: E:\\Development\\workspace\ est\\build.xml build: [echo] The user name is: bob [echo] The user password is: bobs_password BUILD SUCCESSFUL Total time: 5 seconds
I hope this was helpful to you. Don’t forget to remove the password echo.