2012-08-07 81 views
0

我正在写一个内嵌的JavaScript在蚂蚁。该脚本将扫描目录并输出相对于给定时间戳不最新的文件的名称。无法导入类在蚂蚁javascript

<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> 

我注意到第一次调用FileUtils时打破了。我也试过使用一些简单的类,但它们不可访问,看起来像import语句不适用于自定义类或属于显然无法访问的包(例如apache.tools.ant.util.FileUtils)。

有没有什么,我需要做的是在访问它们之前正确加载类?

+0

你用'importPackage'而不是'importClass'导入'FileUtils',是故意的吗? – 2012-08-07 08:39:56

+0

Nopes,最初尝试使用importClass时出现错误,因此将其更改为importPackage。我想这与蚂​​蚁没有获取正确的FileUtils类有关。一旦jar文件commons-io-1.4.jar被包含在ant.cmd中,ant就会报错importPackage被一个类使用,并且最终必须将其更改为importClass。 – 2012-08-08 07:03:22

回答

0

这个问题是通过在蚂蚁的jar文件来解决。 cmd文件。

1

如果你想使用的类不在蚂蚁自己的lib目录,那么你需要指定一个类路径的脚本:

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

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

我尝试了您的解决方案,但它导致构建失败并显示错误消息: