在 Ant 中使用 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16035108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JavaScript in Ant
提问by ah414
I am having issues using the <script>
tag in Ant and I am hoping someone can help. I want to use JavaScript in my Ant build.xml
. Something like the following:
我<script>
在 Ant 中使用标签时遇到问题,我希望有人可以提供帮助。我想在我的 Ant 中使用 JavaScript build.xml
。类似于以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<project name="java" default="main" basedir=".">
<target name="main">
<script language="javascript"> <![CDATA[
println("hello, world")
]]> </script>
</target>
</project>*
Unfortunately this only displays and error:
不幸的是,这只会显示和错误:
build.xml:4: Could not create task or type of type: script.
build.xml:4: 无法创建任务或类型类型:脚本。
I have located the required jar file (js.jar
) for this to work and moved it to ANT_HOME/lib
, but I am still stuck as of how to get this to work.
我已经找到了所需的 jar 文件 ( js.jar
) 使其工作并将其移动到ANT_HOME/lib
,但我仍然不知道如何让它工作。
采纳答案by Chad Nouis
In addition to js.jar
, you need to add bsf.jar
and commons-logging-*.jar
to ANT_HOME/lib. In your Ant distribution, there is a file named docs/manual/install.html
. The Library Dependenciessection of this HTML file documents where you can download these files.
除了 之外js.jar
,您还需要将bsf.jar
和添加commons-logging-*.jar
到 ANT_HOME/lib。在您的 Ant 发行版中,有一个名为docs/manual/install.html
. 此 HTML 文件文档的库依赖项部分可在其中下载这些文件。
println
isn't supported in JavaScript. Instead, use the following:
println
JavaScript 不支持。相反,请使用以下内容:
<project name="jsTest" default="main">
<target name="main">
<script language="javascript"> <![CDATA[
var echo = jsTest.createTask("echo");
echo.setMessage("hello, world");
echo.perform();
]]> </script>
</target>
</project>
回答by Dmitriy Pichugin
You can also instantiate and use Java classes in Javascript as available via Rhino(JRE pre 1.8) or Nashorn(JRE 1.8+) when you need to.
您还可以在需要时通过 Rhino(JRE pre 1.8) 或 Nashorn(JRE 1.8+) 在 Javascript 中实例化和使用 Java 类。
<script language="javascript">
with(new JavaImporter(java.lang, java.io)){
System.out.println("hello, world");// <--!!!
}
</script>
You may create and use JavaScript functions of your own.
您可以创建和使用自己的 JavaScript 函数。
<script language="javascript">
with(new JavaImporter(java.lang, java.io)){
var fun = function(a,b){
System.out.println(a+b);
};
fun(1,2);
}
</script>
Code above prints
上面的代码打印
3.0
Loops, recursion and everything you've been dreaming about... except strong type checking:-)
循环、递归和你梦寐以求的一切......除了强类型检查:-)
回答by Rebse
Beside the two syntax errors - missing ';' after println.. and trailing '*' after closing project tag - you should upgrade your ant installation to a version >= 1.7.x in conjunction with jdk >= 6 to be able to use the builtin javascript engine.
When using jdk >=6 the use of println is no problem, see :
除了两个语法错误 - 缺少 ';' 在 println.. 和关闭项目标记后尾随 '*' - 您应该将 ant 安装升级到版本 >= 1.7.x 并结合 jdk >= 6 以便能够使用内置的 javascript 引擎。
使用jdk>=6时使用println是没有问题的,见:
import javax.script.*;
public class ExecuteJS {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("print('Line1')");
engine.eval("println('Line2')");
engine.eval("print('Line3')");
engine.eval("println('Line4')");
}
}
output :
输出 :
Line1Line2
Line3Line4
and
和
<project>
<script language="javascript">
println("hello, world");
</script>
</project>
But further testing with Ant 1.9.0 / Win7 (my linux box is down right now) / jdk1.7.0_21 revealed some oddities :
但是使用 Ant 1.9.0 / Win7(我的 linux 机器现在已关闭)/ jdk1.7.0_21 进行进一步测试揭示了一些奇怪之处:
<project>
<script language="javascript">
println("hello, world");
</script>
</project>
works
作品
<project default="foo">
<target name="foo">
<script language="javascript">
println("hello, world");
</script>
</target>
</project>
works also
也有效
<project name="whatever" default="foo">
<target name="foo">
<script language="javascript">
println("hello, world");
</script>
</target>
</project>
works also, whereas
也有效,而
<project name="java" default="foo">
<target name="foo">
<script language="javascript">
println("hello, world");
</script>
</target>
</project>
results in
结果是
BUILD FAILED
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot read property "PrintWriter" from undefined (print#8) in print at line number 8
Strange !?
Seems like a bug, so finally upgrade ant >= 1.7.x and jdk >= 1.6 and
don't use 'java' in name attribute of project :-)
奇怪的 !?
似乎是个bug,所以最后升级ant >= 1.7.x 和jdk >= 1.6,
不要在项目的name 属性中使用'java' :-)