The Source for Java Technology Collaboration
User: Password:



Rémi Forax

Rémi Forax's Blog

Using jrunscript to create a build script

Posted by forax on September 13, 2006 at 03:00 AM | Comments (5)

The JDK6 provides a new command jrunscript that enables to execute script shell in Java environment. By default, jrunscript uses javascript as scripting language and provides some useful default functions like cp, cd, cat, etc.

These global functions seem designed to ease the creation of build scripts, so i propose to show the basics of how to write such scripts.

First, the script must create the directory that will contain classes :

 // project properties
 srcDir='src'
 classDir='classes'

 // target init
 mkdirs(classDir);

To run the script, just type jrunscript yourScriptFileName.js in your shell
Then the script must compile the sources...
Weark, there is not function named javac that compiles sources to classes !!

But this function is easy to write using the package javax.tools introduced by the JSR199 and available in mustangjdk6.
In addition to javac, we need another function that gather all the source files of some directory, i've named it fileset in reference to the <fileset> of Ant.

So compiling a source tree is done with the following lines in javascript :

 function fileset(path,pattern) {
   var set=new Array()
   function callback(file) {
     set.push(file)
   }
   find(path,pattern,callback)
   return set 
 }

 // target compile
 echo('compile')
 javac(fileset(srcDir,'.*\.java'),classDir)

you can notice that callback is some kind of closure in javascript :)
The function javac is just a copy/paste of one example that you can find in the comment of the class javax.tools.JavaCompilerTool :

function javac(fileset,destDir) {
  compiler=javax.tools.ToolProvider.getSystemJavaCompiler()
  fileManager=compiler.getStandardFileManager(null,null,null)
  fileManager.setLocation(
    javax.tools.StandardLocation.CLASS_OUTPUT,
    java.util.Arrays.asList([new java.io.File(destDir)].valueOf()))

  compilationUnit=fileManager.getJavaFileObjectsFromFiles(
    java.util.Arrays.asList(fileset.valueOf()));
    task=compiler.getTask(null,fileManager,
                          null,null,null,compilationUnit)
  task.call()
  
  fileManager.close()
}

We now have a basic script that compiles a java project, ok, it doesn't have by example a way to declare dependency between target like Ant but that's a good starting point.
The whole script is available here and if you want to know more about scripting and Java, you can read the A. Sundararajan's Weblog.

Rémi Forax

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Good introduction, but I do not really see any future of Javascript to manage project . In fact , a project may need description of a workflow and I think xml is better (Ant, Maven project management tools)

    Nevertheless, it may be used to express algo that it is difficult to express in XML (javascript inside Ant or Maven Script).

    Olivier Parnière
    UMLV Connection

    Posted by: oparnier on September 13, 2006 at 03:35 AM

  • Olivier : You're right that build processes are only workflows for now. But describing a build process in a script language with such easy access to Java seems interesting as tests are written in Java too. Thus such possibility may allow developpers to create some smarter build and test processes that could automate some tasks (e.g. automatically pick up a jar within a database, maybe try different compilation settings depending to test results, ...).

    Posted by: micf on September 13, 2006 at 05:01 AM

  • Very nice. So there is no need to install ant, if I have to compile sources. A simple call of jrunscript does this :-)

    Posted by: gruenewa on September 13, 2006 at 05:43 AM

  • "a project may need description of a workflow and I think xml is better" -oparnier.

    I definitely agree with this. It does look like a neat feature though. I'm excited to play with it, and see if I can find more uses for it than I see at the beginning.
    ~The Scuba Skipper

    Posted by: jeditdog on September 13, 2006 at 12:05 PM

  • There are other advantages to Ant. For example, I am currently building a project to automate the grading of programming assignments. It heavily relies on the ability to listen to the progress of an Ant script. Ant has a listener architecture, so I can attach custom listeners that keep track of the script progress.
    This would be very hard in a completely scripted system. The structure that Ant enforces is useful.
    I think what makes Ant successful is that it is a good middle ground between a fully scripted (infinitely flexible) and fully declarative (if we didn't think if it, you can't do it) approach.

    Posted by: cayhorstmann on September 15, 2006 at 06:46 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds