开发者

How can I use Java in an Ant buildfile?

开发者 https://www.devze.com 2023-01-31 11:04 出处:网络
I\'m trying to write a custom scriptselector, and to do so I need to read the contents of each file. Is there a way to use java, and not javascript, as the language of a scriptselector? I开发者_JS百科

I'm trying to write a custom scriptselector, and to do so I need to read the contents of each file.

Is there a way to use java, and not javascript, as the language of a scriptselector? I开发者_JS百科f not, is there a way, silly as it sounds, to read the File object?

<scriptselector language="javascript">
    f = self.getFile();
    println(f);
    //how to read the File?
    self.setSelected(true);
</scriptselector>


Here's how. Something along the lines of:

<scriptselector language="javascript">
    importPackage(java.io);
    importPackage(org.apache.tools.ant.util);

    fileUtils = FileUtils.getFileUtils();
    f = self.getFile();
    println(f);
    if( f.getAbsolutePath().endsWith(".xyz") ){
        fis = new FileInputStream(f.getAbsolutePath());
        isr = new InputStreamReader(fis);
        println('reading it!');
        fileContents = fileUtils.readFully(isr);
        println(fileContents);
        self.setSelected(true);
    }
</scriptselector>


You should be able to use any BSF-supported language, which includes BeanShell (Java). For example:

<script language="beanshell">
    String file = "foo.txt";
    InputStream is = null;
    try {
        is = new FileInputStream(file);
        // read from stream as required
    } finally {
        if (is != null) {
            try {
            is.close();
            } catch (IOException e) { /* ignore */ }
         }
    }
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号