Shoaib Mehmood
Shoaib Mehmood

Reputation: 26

Unable to import class in ant javascript

I am writing an inline javascript in ant. This script will scan a directory and output the names of files that are not up to date with respect to a given timestamp.

<script language="javascript"> <![CDATA[

  importPackage(Packages.java.lang);
  importPackage(Packages.org.apache.commons.io.FileUtils);
  importClass(Packages.java.io.File);
  importClass(Packages.java.util.Iterator);


  var path = "D:\DirectoryToScan\";
  var timeToCompare = buildServers.getProperty("buildStartTime");

  var invocationTime = new Date(timeToCompare );
  invocationTime = invocationTime.getTime();

  var directoryToSearch = new File(path );
  //CODE BREAKS HERE. NONE OF THE SUBSEQUENT LINES EXECUTE  
  var fileIterator = FileUtils.iterateFiles(directoryToSearch, new String[]{"pdf","html"} , true);


  //iterate through files and directories in builtDocs folder and return false if any of the files is older than invocation time
  while (fileIterator.hasNext()) {
     var doc = fileIterator.next();
     if ((!FileUtils.isFileNewer(doc, invocationTime))) {
        return false;
     }        
  }
]]> </script>

I have noticed that the first call to FileUtils breaks. I also tried using some simple classes but they were not accessible and looks like import statement is not working for custom classes or belong to packages that apparently are not accessible here (such as apache.tools.ant.util.FileUtils).

Is there something that i need to do here to load the classes correctly before accessing them?

Upvotes: 0

Views: 3688

Answers (2)

Shoaib Mehmood
Shoaib Mehmood

Reputation: 26

The issue was resolved by including the jar file in ant.cmd file.

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122394

If the classes you're trying to use are not in Ant's own lib directory then you need to specify a classpath for the script:

<path id="script.classpath">
  <fileset dir="lib" includes="**/*.jar"/>
</path>

<script language="javascript" classpathref="script.classpath">
  ...

Upvotes: 1

Related Questions