How can I use Java in an Ant buildfile?

I am trying to write my own script selector, and for this I need to read the contents of each file.

Is there a way to use java and not javascript like a script language? If not, is there a way, dumb as it sounds, to read a File object?

<scriptselector language="javascript"> f = self.getFile(); println(f); //how to read the File? self.setSelected(true); </scriptselector> 
+6
java javascript ant
source share
2 answers

Here is how. Something like:

 <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> 
+5
source share

You can use any BSF-supported language that 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> 
+4
source share

All Articles