java Ant 中的 Beanshell 产生“无法为 beanshell 创建 javax 脚本引擎”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2716646/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:30:07  来源:igfitidea点击:

Beanshell in Ant yielding, "Unable to create javax script engine for beanshell"

javaantbeanshell

提问by Vimes

Greeting, I'm trying to put some Beanshell script in my Ant build.xml file. I've followed the Ant manual as well as I can but I keep getting "Unable to create javax script engine for beanshell" when I run Ant. Here is the test target I wrote mostly from examples in the Ant manual:

您好,我正在尝试将一些 Beanshell 脚本放入我的 Ant build.xml 文件中。我已经尽可能地遵循了 Ant 手册,但是当我运行 Ant 时,我不断收到“无法为 beanshell 创建 javax 脚本引擎”的消息。这是我主要根据 Ant 手册中的示例编写的测试目标:

<target name="test-target">
    <script language="beanshell" setbeans="true">
        <classpath>
            <fileset dir="c:\TEMP" includes="*.jar" />
        </classpath>
        System.out.println("Hello world");
    </script>
</target>

My beanshell "bsh-2.0b4.jar" file is on the script task's classpath the way the manual recommended. Hope I have the right file. I'm working in c:\TEMP right now. I've been googling and trying for a while now. Any ideas would be greatly appreciated. Thanks.

我的 beanshell“bsh-2.0b4.jar”文件按照手册推荐的方式位于脚本任务的类路径中。希望我有正确的文件。我现在在 c:\TEMP 工作。我一直在谷歌搜索并尝试了一段时间。任何想法将不胜感激。谢谢。

采纳答案by Brett Kail

First, you need jsr-engines.zip from here:

首先,您需要从这里获取 jsr-engines.zip:

https://scripting.dev.java.net/servlets/ProjectDocumentList

https://scripting.dev.java.net/servlets/ProjectDocumentList

Inside, you'll find jsr223/beanshell/build/bsh-engine.jar. Some searching implied that you need to download bsh-2.05b.jar. I found it here:

在里面,你会找到 jsr223/beanshell/build/bsh-engine.jar。一些搜索暗示您需要下载bsh-2.05b.jar。我在这里找到了:

http://beanshell.org/bsh-2.0b5.jar

http://beanshell.org/bsh-2.0b5.jar

The more easily findable bsh-2.0b4.jar also seemed to work, but it printed a message that implied it was experimental.

更容易找到的 bsh-2.0b4.jar 似乎也可以工作,但它打印了一条消息,暗示它是实验性的。

回答by Jarekczek

Currently (2012) you need only 1 jar to fire the script task for BeanShell:

目前(2012 年)您只需要 1 个 jar 即可为 BeanShell 触发脚本任务:

Previously I also thought of the following, as mentioned by Ant Manual, Library Dependencies chapter:

之前我还想到了以下内容,正如 Ant 手册,库依赖项章节中提到的:

But it looks like bsfis not needed for bsh, at least in my environment.

但看起来bsf不需要 for bsh,至少在我的环境中是这样。

Once the jar is given to ant, the script task runs smoothly. There are 2 possible scenarios for getting the jars and making them available to ant.

将jar 交给ant 后,脚本任务就会顺利运行。有 2 种可能的场景来获取 jars 并将它们提供给 ant。

Manual download way

手动下载方式

Download the jars above. I provided the links from maven repository. Once you have all the jars downloaded, make them available to ant. There are at least 3 ways to do it:

下载上面的jar包。我提供了来自 Maven 存储库的链接。下载所有 jar 后,将它们提供给 ant。至少有3种方法可以做到:

  1. Put it in java library path
  2. Put it in ant library directory
  3. Give the correct classpath to scripttask.
  1. 把它放在java库路径中
  2. 放到ant库目录下
  3. script任务提供正确的类路径。

I find the last method the best, because it is most easily ported between different systems. The ant file for the script task could look as follows:

我发现最后一种方法最好,因为它最容易在不同系统之间移植。脚本任务的 ant 文件可能如下所示:

<project default="t1" >
  <property name="bsh.path"
    location="/mnt/q/jarek/lang/java/ant/stackoverflow/bsh-2.0b5.jar" />
  <target name="t1">
    <script language="beanshell" classpath="${bsh.path}">
      javax.swing.JOptionPane.showMessageDialog(null, "Hello, Script!");
    </script>
  </target>
</project>

Automatic download method, employing Ivy

自动下载方式,采用Ivy

The manual method is not perfect when you want to distribute your build script. Then you would like a way to make sure the jars are present in the destination system. For distributing builds there's no better tool than ivy. Ivy will download the jars and put them in classpathfor you. The problem is that there appears another dependency, which is ivy itself. But providing ivy.jar is quite easy and that is the last dependency we need to supply explicitly.

当您想要分发构建脚本时,手动方法并不完美。然后,您需要一种方法来确保目标系统中存在 jar。对于分发构建,没有比ivy更好的工具了。Ivy 会下载这些 jars 并classpath为您放入。问题是出现了另一个依赖项,即ivy本身。但是提供 ivy.jar 非常容易,这是我们需要明确提供的最后一个依赖项。

One may ask why to provide ivy.jar, while we could simply download bsh.jarin the same way. The answer is flexibility. When you have the ivy.jar, you get any jar you wish with a single step being adding it to the ivy.xmlfile. And there is an agreed universal location for the ivy.jarfile, while for other file we would have to think of a suitable directory.

有人可能会问为什么要提供ivy.jar,而我们可以bsh.jar用同样的方式简单地下载。答案是灵活性。当您拥有 . 时ivy.jar,您只需一步将其添加到ivy.xml文件中即可获得您想要的任何 jar 。ivy.jar文件有一个商定的通用位置,而对于其他文件,我们必须考虑一个合适的目录。

Below comes the full example that downloads ivy and then all the necessary dependencies. Ivy download script is based on Installation chapter of Ivy reference. Then a simple ivy.xmlfile is needed, which is given after the sample build.xml.

下面是下载 ivy 和所有必要依赖项的完整示例。Ivy 下载脚本基于Ivy 参考的安装章节。然后ivy.xml需要一个简单的文件,在示例之后给出build.xml

Original auto-download ivy script has a disadvantage of always checking the ivy url, even if ivy.jaris already in the expected location. This may be overriden by specifying -Doffline=true. I prefer to add another target to the build file and to do the http check only if we don't already have the ivy.jar. This is the way the script here works. To observe what ivy actually downloaded, set IVY_HOMEenvironment variable to a directory of your choice. It will be created and filled with ivy stuff.

原始的自动下载 ivy 脚本的缺点是总是检查 ivy url,即使ivy.jar它已经在预期的位置。这可以通过指定来覆盖-Doffline=true。我更喜欢在构建文件中添加另一个目标,并且仅当我们还没有ivy.jar. 这是这里脚本的工作方式。要观察 ivy 实际下载的内容,请将IVY_HOME环境变量设置为您选择的目录。它将被创建并充满常春藤的东西。

build.xml:

build.xml

<project default="t1"
         xmlns:ivy="antlib:org.apache.ivy.ant" >

  <property name="ivy.install.version" value="2.2.0" />
  <property environment="env" />
  <condition property="ivy.home" value="${env.IVY_HOME}">
    <isset property="env.IVY_HOME" />
  </condition>
  <property name="ivy.home" value="${user.home}/.ant" />
  <property name="ivy.jar.dir" value="${ivy.home}/lib" />
  <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />

  <target name="check-ivy">
    <condition property="ivy.present">
      <available file="${ivy.jar.file}" type="file" />
    </condition>
  </target>

  <target name="download-ivy" unless="ivy.present">
      <mkdir dir="${ivy.jar.dir}"/>
      <!-- download Ivy from web site so that it can be used even without any special installation -->
      <get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" 
           dest="${ivy.jar.file}" usetimestamp="true"/>
  </target>

  <target name="init-ivy" depends="check-ivy, download-ivy">
    <!-- try to load ivy here from ivy home, in case the user has not already dropped
            it into ant's lib dir (note that the latter copy will always take precedence).
            We will not fail as long as local lib dir exists (it may be empty) and
            ivy is in at least one of ant's lib dir or the local lib dir. -->
      <path id="ivy.lib.path">
          <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
      </path>
      <taskdef resource="org/apache/ivy/ant/antlib.xml"
               uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
  </target>

  <target name="ivy-libs" depends="init-ivy" >
    <ivy:cachepath pathid="path.from.ivy" log="download-only" />
  </target>

  <target name="t1" depends="ivy-libs" >
    <script language="beanshell" classpathref="path.from.ivy">
      javax.swing.JOptionPane.showMessageDialog(null, "Hello, Script!");
    </script>
  </target>

</project>

ivy.xml:

ivy.xml

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation=
                   "http://ant.apache.org/ivy/schemas/ivy.xsd">

<info organisation="example.com" module="testing-script-task" /> 

<dependencies>
  <dependency org="org.beanshell" name="bsh" rev="2.0b5" />
  <!--  <dependency org="bsf" name="bsf" rev="2.4.0" /> -->
</dependencies>

</ivy-module>

回答by Jarekczek

Don't use beanshelllanguage. Use javascriptinstead, as it runs on jdk6 without any additional jars. Rebsetold me that.

不要使用beanshell语言。改用javascript它,因为它在 jdk6 上运行,没有任何额外的 jars。Rebse告诉我的。

Javascriptis also allowed to use java classes, for example java.lang.System.out.println()

Javascript也允许使用java类,例如 java.lang.System.out.println()

回答by Supun Sameera

The Ant plug-in "org.apache.ant_1.7.0.v200803061910" have all the jar files needed

Ant插件“org.apache.ant_1.7.0.v200803061910”有需要的所有jar文件