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:
<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.
Don't use
beanshell
language. Usejavascript
instead, as it runs on jdk6 without any additional jars. Rebse told me that.Javascript
is also allowed to use java classes, for examplejava.lang.System.out.println()
The Ant plug-in "org.apache.ant_1.7.0.v200803061910" have all the jar files needed
First, you need jsr-engines.zip from here:
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:
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.
Currently (2012) you need only 1 jar to fire the script task for BeanShell:
Previously I also thought of the following, as mentioned by Ant Manual, Library Dependencies chapter:
But it looks like
bsf
is not needed forbsh
, at least in my environment.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.
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:
script
task.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:
Automatic download method, employing 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
classpath
for 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.One may ask why to provide
ivy.jar
, while we could simply downloadbsh.jar
in the same way. The answer is flexibility. When you have theivy.jar
, you get any jar you wish with a single step being adding it to theivy.xml
file. And there is an agreed universal location for theivy.jar
file, while for other file we would have to think of a suitable directory.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.xml
file is needed, which is given after the samplebuild.xml
.Original auto-download ivy script has a disadvantage of always checking the ivy url, even if
ivy.jar
is 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 theivy.jar
. This is the way the script here works. To observe what ivy actually downloaded, setIVY_HOME
environment variable to a directory of your choice. It will be created and filled with ivy stuff.build.xml
:ivy.xml
: