/** * Copies files of a particular pattern from one location to another * * @param fromDir * @param pattern * @param toDir */ protected void copy(File fromDir, String pattern, File toDir) { FileSet fs = new FileSet(); fs.setProject(getProject()); fs.setDir(fromDir); fs.setIncludes(pattern); Copy copy = new Copy(); copy.setTaskName(getTaskName()); copy.setProject(getProject()); copy.setTodir(toDir); copy.setVerbose(isVerbose()); copy.add(fs); copy.execute(); // add files to installed artifacts path reference(fs); }
public void execute() throws BuildException { if (_licenseFileName == null) { log.error("License file not specified"); throw new BuildException("License file not specified"); } File f = new File(_licenseFileName); if (!f.exists()) { log.error("License file doesn't exist"); throw new BuildException("License file doesn't exist"); } try { StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new FileReader(f)); String line = null; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } _updater.setText(sb.toString()); } catch (Exception ex) { log.error("Error getting license text: " + ex.getMessage()); throw new BuildException("Error getting license text: " + ex.getMessage(), ex); } try { _updater.validate(); } catch (Exception ex) { log.error("Task not configured correctly: " + ex.getMessage()); throw new BuildException("Task not configured correctly: " + ex.getMessage(), ex); } if (_fileSets.size() == 0) { log.error("A nested fileset element is required"); throw new BuildException("A nested fileset element is required"); } for (Iterator i = _fileSets.iterator(); i.hasNext(); ) { FileSet fileSet = (FileSet) i.next(); DirectoryScanner ds = null; if (_outputDirName == null) { ds = fileSet.getDirectoryScanner(project); } else { File outputDir = new File(_outputDirName); if (!outputDir.exists()) { outputDir.mkdirs(); } Copy copy = new Copy(); copy.setProject(project); copy.addFileset(fileSet); copy.setTodir(outputDir); copy.setVerbose(false); copy.init(); copy.execute(); fileSet.setDir(outputDir); ds = fileSet.getDirectoryScanner(project); } String[] fileNames = ds.getIncludedFiles(); String path = ds.getBasedir().getAbsolutePath(); for (int j = 0; j < fileNames.length; j++) { _updater.getFiles().add(new File(path + "/" + fileNames[j])); } } try { _updater.run(); } catch (Exception ex) { log.error("Error executing license updater: " + ex.getMessage()); ex.printStackTrace(); throw new BuildException("Error executing license updater: " + ex.getMessage(), ex); } }
/** * Renames this item. * Not all the Items need to support this operation, but if you decide to do so, * you can use this method. */ protected void renameTo(String newName) throws IOException { // always synchronize from bigger objects first final ItemGroup parent = getParent(); synchronized (parent) { synchronized (this) { // sanity check if (newName == null) throw new IllegalArgumentException("New name is not given"); // noop? if (this.name.equals(newName)) return; Item existing = parent.getItem(newName); if (existing != null && existing!=this) // the look up is case insensitive, so we need "existing!=this" // to allow people to rename "Foo" to "foo", for example. // see http://www.nabble.com/error-on-renaming-project-tt18061629.html throw new IllegalArgumentException("Job " + newName + " already exists"); String oldName = this.name; File oldRoot = this.getRootDir(); doSetName(newName); File newRoot = this.getRootDir(); boolean success = false; try {// rename data files boolean interrupted = false; boolean renamed = false; // try to rename the job directory. // this may fail on Windows due to some other processes // accessing a file. // so retry few times before we fall back to copy. for (int retry = 0; retry < 5; retry++) { if (oldRoot.renameTo(newRoot)) { renamed = true; break; // succeeded } try { Thread.sleep(500); } catch (InterruptedException e) { // process the interruption later interrupted = true; } } if (interrupted) Thread.currentThread().interrupt(); if (!renamed) { // failed to rename. it must be that some lengthy // process is going on // to prevent a rename operation. So do a copy. Ideally // we'd like to // later delete the old copy, but we can't reliably do // so, as before the VM // shuts down there might be a new job created under the // old name. Copy cp = new Copy(); cp.setProject(new org.apache.tools.ant.Project()); cp.setTodir(newRoot); FileSet src = new FileSet(); src.setDir(oldRoot); cp.addFileset(src); cp.setOverwrite(true); cp.setPreserveLastModified(true); cp.setFailOnError(false); // keep going even if // there's an error cp.execute(); // try to delete as much as possible try { Util.deleteRecursive(oldRoot); } catch (IOException e) { // but ignore the error, since we expect that e.printStackTrace(); } } success = true; } finally { // if failed, back out the rename. if (!success) doSetName(oldName); } callOnRenamed(newName, parent, oldName); for (ItemListener l : ItemListener.all()) l.onRenamed(this, oldName, newName); } } }