public class MathematicaTask extends org.apache.tools.ant.Task implements PacketListener
You can run Mathematica as a processing step in your build, call Mathematica testing functions, or any other operations you desire. Because MathematicaTask lets you call back into Ant from Mathematica (e.g., to execute other tasks, set/read properties, or log messages), you can use Mathematica as a scripting language for Ant.
To use the <mathematica> task, you must include a taskdef line in your build.xml file like the following examples:
<!-- If JLink.jar is on your system CLASSPATH: -->
<taskdef name="mathematica" classname="com.wolfram.jlink.util.MathematicaTask"/>
<!-- If JLink.jar is NOT on your system CLASSPATH: -->
<taskdef name="mathematica" classname="com.wolfram.jlink.util.MathematicaTask"
classpath="/full/path/to/JLink.jar"/>
| Attribute | Description | Required |
| exe | The path to the Mathematica kernel to launch | Either exe or cmdline must be specified |
| cmdline | The full set of MathLink command-line args to use to connect to the kernel | Either exe or cmdline must be specified |
| runfile | The path to a .m file to be run when the kernel is launched. This is convenient when you want to specify some or all of the code in a file instead of directly in the task. | No |
| fresh | Whether to start a fresh kernel for this task (if a previously-used kernel is running, it will be quit first) | No, defaults to false |
| quit | Whether to force the kernel to quit immediately after this task finishes | No, defaults to false |
| timeout | The maximum number of seconds to allow for the computation. If not finished, the kernel will be killed. | No, defaults to Integer.MAX_VALUE |
| timeoutproperty | The name of a property to set if the timeout expires. The named property will be set to true. | No |
| failonerror | Whether a MathLink error (unlikely) or a timeout should cause the build to fail. | No, defaults to true |
You have two ways of specifying how to launch or connect to the Mathematica kernel. Most users will use the exe parameter, which specifies the path to the kernel to launch. In rare cases you might need more control over how the kernel is launched, or you might want to connect to an already-running kernel (not launched by Ant). In such cases, you can use the cmdline parameter, which lets you specify a full set of command-line MathLink arguments (e.g., "-linkmode connect -linkname 1234 -linkprotocol tcpip").
| Function | Description |
| AntTask["taskname", taskattributes] | Execute the given task, with optional attributes set in the form of "attrname"->"value" rules. |
| AntProperty["propertyname"] | Returns the string value of the specified Ant property. |
| AntSetProperty["propertyname", "value"] | Sets the value of the specified Ant property. |
| AntReference["refId"] | Lookup the corresponding object by its id (such as a defined <path> element from its id atribute). Unlike AntProperty, which always returns a string, AntReference returns a JavaObject expression; to use it in your Mathematica code you will have to inspect the Ant API docs for the particular class. |
| AntLog["message"] | Write the given message to the Ant log. |
| AntFail["message"] | Abort the Mathematica code and cause the Ant build to fail with the given message. |
| Ant["objectName"] | General-purpose function for obtaining a reference to a specified Ant object. The allowed object names are "Project", "Target", "Task", and "Location". Use this function when you want to make direct calls into the Ant API. The other Mathematica functions listed above are convenience functions that are implemented using the Ant[] function. |
<!-- Windows -->
<property name="mathExe", value="c:/program files/wolfram research/mathematica/5.2/mathkernel"/>
<!-- Linux, Unix ('math' is typically on PATH) -->
<property name="mathExe", value="math"/>
<!-- Mac OS/X -->
<property name="mathExe", value="/Applications/Mathematica\ 5.2.app/Contents/MacOS/MathKernel"/>
This simple example loads a package and calls a function. It uses AntFail to make the Ant build fail
if a testing function does not succeed.
<target name="runMathTests">
<mathematica exe="${mathExe}">
<![CDATA[
Get["MyTestPackage`"];
result = CallTestFunction[];
If[!TrueQ[result],
AntFail["Mathematica test function failed"]
]
]]>
</mathematica>
</target>
This example demonstrates the use of AntProperty to obtain the value of an Ant property
as a string in your Mathematica code.
<property name="codeDir" value="${basedir}/MyCodeDir">
<target name="propertyExample">
<mathematica exe="${mathExe}">
<![CDATA[
Get[ToFileName[AntProperty["codeDir"], "SuperPackage.m"]];
...
]]>
</mathematica>
</target>
This example demonstrates the use of AntReference to bring a list of directories defined
in a <path> element into your Mathematica code. In this case, AntReference returns an
Ant Path object, the methods for which are described in the Ant API docs:
<path id="mathPath">
... dirsets, pathelements, etc.
</path>
<target name="refExample">
<mathematica exe="${mathExe}">
<![CDATA[
mathPathDirs = AntReference["mathPath"]@list[];
$Path = Join[mathPathDirs, $Path];
...
]]>
</mathematica>
</target>
This example shows how to call the Ant <echo> task from Mathematica code
(this is just a simple task example--there are easier ways to have Mathematica output appear
in the console, including simply calling Print[]):
<target name="scriptingExample">
<mathematica exe="${mathExe}">
<![CDATA[
For[i = 0, i < 10, i++,
AntTask["echo", "message"->ToString[i^2]]
]
]]>
</mathematica>
</target> | Constructor and Description |
|---|
MathematicaTask() |
| Modifier and Type | Method and Description |
|---|---|
void |
addText(java.lang.String code) |
protected void |
closeKernel() |
void |
execute()
Execute
|
protected KernelLink |
initKernel() |
protected void |
killTimeoutThread(com.wolfram.jlink.util.MathematicaTask.TimeoutThread timeoutThread) |
boolean |
packetArrived(PacketArrivedEvent evt)
|
void |
setCmdline(java.lang.String cmdLine) |
void |
setExe(java.lang.String exe)
Attribute Setters
|
void |
setFail(java.lang.String msg) |
void |
setFailonerror(boolean failOnError) |
void |
setFresh(boolean fresh) |
void |
setQuit(boolean quit) |
void |
setRunfile(java.lang.String runFile) |
void |
setTimeout(int seconds) |
void |
setTimeoutproperty(java.lang.String timeoutProperty) |
protected com.wolfram.jlink.util.MathematicaTask.TimeoutThread |
startTimeoutThread() |
getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskTypepublic void setExe(java.lang.String exe)
public void setCmdline(java.lang.String cmdLine)
public void setFresh(boolean fresh)
public void setQuit(boolean quit)
public void setRunfile(java.lang.String runFile)
public void setTimeout(int seconds)
public void setTimeoutproperty(java.lang.String timeoutProperty)
public void setFailonerror(boolean failOnError)
public void addText(java.lang.String code)
public void setFail(java.lang.String msg)
public void execute()
throws org.apache.tools.ant.BuildException
execute in class org.apache.tools.ant.Taskorg.apache.tools.ant.BuildExceptionprotected KernelLink initKernel()
protected void closeKernel()
protected com.wolfram.jlink.util.MathematicaTask.TimeoutThread startTimeoutThread()
protected void killTimeoutThread(com.wolfram.jlink.util.MathematicaTask.TimeoutThread timeoutThread)
public boolean packetArrived(PacketArrivedEvent evt) throws MathLinkException
packetArrived in interface PacketListenerevt - the PacketArrivedEventMathLinkExceptionJ/Link is Copyright (c) 1999-2012, Wolfram Research, Inc. All rights reserved.