java - Eclipse 中是否有用于 ANT 任务的 SecureInputHandler?

标签 java eclipse ant

在使用 <input .../> 时有没有办法屏蔽密码?来自 Eclipse IDE 的 ANT 任务?

我看到了一种从命令行执行此操作的方法:

<input message="secure-input:" addproperty="the.password">
    <handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>

但这在 eclipse 中不起作用。

最佳答案

编辑: 由于 ant 安全输入处理程序在 eclipse 4.x 中仍然不起作用,这里有一个基于 ant 脚本任务的解决方案,使用内置 javascript 引擎(自 JDK 1.6.0_06 起),所以不需要额外的库:

<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>

JPasswordField.getText() 方法已弃用, 通常您会使用 getPassword(),但它不起作用。

// create ant property
var s = new String(pw.getPassword());
project.setNewProperty("pwd", s);

只接受字符数组,而 :

// create ant property
var s = String.valueOf(pw.getPassword());
project.setNewProperty("pwd", s);

结果:

[echo] ${pwd} => function String() { [native code for String.String, arity=1] }

可能是 javascript 引擎中的错误!? (使用jdk 1.7.0_60)

自版本 1.7.1 Ant 开始支持 Java 1.6 的安全控制台输入功能,请参阅 Ant Manual .您使用什么版本的 Eclipse 和 Java?或者你可以使用:
AntForms它有很多输入对话框,也支持密码
或者
Jera Ant Tasks它有一个带有可选密码屏蔽的查询任务

编辑:使用 groovy 添加一个特定示例..

您可以使用脚本语言打开对话框,这里是一个使用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>

最后写自己的Inputhandler,见http://sourceforge.net/projects/emaria/files/antdocs/antinput/antinput.pdf/download详情

关于java - Eclipse 中是否有用于 ANT 任务的 SecureInputHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485037/

相关文章:

java - Ant工具工作问题

android - Ant 使用ADT的 "build performance improvements"

java - log4j 1.x : detect if no config file given and programmatically set it up

java - 删除后接触处理程序中的 Box2D 空夹具

java - 配置 AppContextListener 类的应用程序监听器时出错

java - eclipse junit 差异窗口

java - 使用 Hibernate 配置 Spring。创建名称为 'usersDao' : Unsatisfied dependency expressed through field 'sessionFactory' 的 bean 时出错

java - 将对象列表转换为字符串数组

java - 如何保存和重用 Eclipse 设置?

ant - 用 ANT 替换基于属性文件的所有 token