Ant property files
I would use the Ant property files instead. Getting some Mathematica-related variables by calling Mathematica from Ant is possible (see the second part of this answer), but more complicated and error-prone too.
Ant property files are exactly the mechanism used by Ant to separate the parameters that vary from machine to machine, from those which are universal. These files has the .properties
extension, and are stored separately. Your project may look like
Test
Test
Kernel
init.m
Test.m
Scripts
build.xml
build.properties
Test.nb
Then the build.properties
file may look like
mathematicaInstallDir = C:/Program Files/Wolfram Research/Mathematica/8.0
mathExe = ${mathematicaInstallDir}/MathKernel.exe
userBaseDirectory = C:/Users/Archie/AppData/Roaming/Mathematica
and your build.xml
may look like
<project name="Test" basedir=".." default="build">
<property name="rootdir" value="${basedir}"/>
<property name= "pacletName" value = "Test"/>
<property name= "dist" value = "${rootdir}/Build/${pacletName}"/>
<property file="build.properties"/>
<property name="destination" value="${userBaseDirectory}"/>
<target name="clean" >
<delete dir = "${dist}"/>
</target>
<target name = "build" depends = "clean">
<mkdir dir="${dist}"/>
<mkdir dir = "${dist}/Kernel"/>
<copy todir="${dist}/Kernel">
<fileset dir="Test/Kernel"/>
</copy>
<copy file="Test/Test.m" todir="${dist}"/>
</target>
<target name = "undeploy">
<delete dir = "${destination}"/>
</target>
<!-- Copy the project to the final destination -->
<target name = "deploy" depends = "undeploy, build">
<copy todir="${destination}">
<fileset dir="${dist}"/>
</copy>
</target>
</project>
What happens is that the "destination"
is read in by the build.xml
file from build.properties
file, and used in the targets defined after that. The build.properties
file you don't commit to the version control, it is different for different machines. This is the standard practice with Ant, and I think this will be a better solution than trying to automate things further, call Mathematica from Ant, etc.
How to communicate results from Mathematica to Ant
If you really want it, there is a way to communicate the results from Mathematica to Ant. I will show a simple example - the $UserBaseDirectory
will be assigned to some Ant property. Here are two additional targets to add to the build.xml
script, which illustrate this:
<property name="jlinkpath" value="${mathematicaInstallDir}/SystemFiles/Links/JLink/"/>
<target name="initMathematicaTask" unless="JLinkLoaded">
<path id="jlink.lib">
<fileset dir="${jlinkpath}">
<include name="**/JLink.jar"/>
<include name="**/SystemFiles/*"/>
</fileset>
</path>
<!-- Load JLink -->
<taskdef name="mathematica"
classname="com.wolfram.jlink.util.MathematicaTask" >
<classpath refid="jlink.lib"/>
</taskdef>
<property name="JLinkLoaded" value="true"/>
</target>
<target name = "testUserBaseDirSet" depends = "initMathematicaTask">
<mathematica exe="${mathExe}" fresh="true" quit="true">
<![CDATA[
AntSetProperty["userBaseDir", ToString[$UserBaseDirectory]];
]]>
</mathematica>
<echo message="The vaue of 'userBaseDir' is: ${userBaseDir} "/>
</target>
The property "jlinkpath"
you can set at any place after the "mathematicaInstallDir"
property has been defined (you will need this one in any case, and it has been defined in my example in the build.properties
file). The "initMathematicaTask"
target is an auxiliary target to init Mathematica. With it, one can use Mathematica in the Ant builds. The target "testUserBaseDirSet"
illustrates how you can run Mathematica code by Ant and set Ant variables (properties) from within Mathematica. To see a larger example of this, search for notebook.xml
, which is a part of the documentation build script and should reside somewhere in the Workbench distribution.
$UserBaseDirectory
etc. $\endgroup$