Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm using an ant script to install my application from the Workbench (as suggested in this answer). In past I've used two ways of specifying the destination for the build:

  1. Hard-coded directory, like /Users/me/Library/Mathematica/Applications. This would be fine for a single computer, but I store the code in a versioning system, and access it from computers having different filesystems (e.g., MacOSX laptop, linux desktop). It is inconvenient to change the ant build file for each build.

  2. Setting an environment variable on each computer ($MMA_APP_DIR, say) and using <property environment="env"/> within ant to get that variable using ${env.MMA_APP_DIR}. Much better than option #1, since I don't have to change the code itself, but still an inconvenience.

Mathematica's, $BaseDirectory and $UserBaseDirectory are the destinations I'm after. How can I access the values of these variables from the ant script? They are built into the Workbench framework, showing up as destination folders when using the Application View to export to an archive or to a folder. I've yet to discover how to access them.

share|improve this question
    
I added a section answering your specific question on accessing $UserBaseDirectory etc. –  Leonid Shifrin Jan 18 '13 at 18:30
    
@LeonidShifrin, that answers the particular question, yet your property file suggestion is easier to manage, so I'll go with that. –  JxB Jan 20 '13 at 5:33

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks Leonid, +1! –  JxB Jan 20 '13 at 5:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.