1 19 20 package org.netbeans.modules.java.j2seproject.copylibstask; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.text.MessageFormat ; 27 import java.util.ResourceBundle ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.taskdefs.Jar; 32 import org.apache.tools.ant.types.Path; 33 import org.apache.tools.ant.util.FileUtils; 34 35 39 public class CopyLibs extends Jar { 40 41 private static final String LIB = "lib"; 43 Path runtimePath; 44 45 46 public CopyLibs () { 47 } 48 49 public void setRuntimeClassPath (final Path path) { 50 assert path != null; 51 this.runtimePath = path; 52 } 53 54 public Path getRuntimeClassPath () { 55 return this.runtimePath; 56 } 57 58 public void execute() throws BuildException { 59 if (this.runtimePath == null) { 60 throw new BuildException ("RuntimeClassPath must be set."); 61 } 62 final String [] pathElements = this.runtimePath.list(); 63 File [] filesToCopy = new File [pathElements.length]; 64 for (int i=0; i< pathElements.length; i++) { 65 File f = new File (pathElements[i]); 66 if (f.isDirectory() || !f.canRead()) { 67 filesToCopy = null; 68 break; 69 } 70 else { 71 filesToCopy[i] = f; 72 } 73 } 74 super.execute(); 75 76 final File destFile = this.getDestFile(); 77 final File destFolder = destFile.getParentFile(); 78 assert destFolder != null && destFolder.canWrite(); 79 try { 80 ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.java.j2seproject.copylibstask.Bundle"); assert bundle != null; 82 final File readme = new File (destFolder,bundle.getString("TXT_README_FILE_NAME")); 83 if (!readme.exists()) { 84 readme.createNewFile(); 85 } 86 final PrintWriter out = new PrintWriter (new FileWriter (readme)); 87 try { 88 final String content = bundle.getString("TXT_README_FILE_CONTENT"); 89 out.println (MessageFormat.format(content,new Object [] {destFile.getName()})); 90 } finally { 91 out.close (); 92 } 93 } catch (IOException ioe) { 94 this.log("Cannot generate readme file.",Project.MSG_VERBOSE); 95 } 96 97 if (filesToCopy != null && filesToCopy.length>0) { 98 final File libFolder = new File (destFolder,LIB); 99 if (!libFolder.exists()) { 100 libFolder.mkdir (); 101 } 102 assert libFolder.canWrite(); 103 FileUtils utils = FileUtils.newFileUtils(); 104 for (int i=0; i<filesToCopy.length; i++) { 105 try { 106 File libFile = new File (libFolder,filesToCopy[i].getName()); 107 utils.copyFile(filesToCopy[i],libFile); 108 } catch (IOException ioe) { 109 throw new BuildException (ioe); 110 } 111 } 112 } 113 114 } 115 116 } 117 | Popular Tags |