/** * Copies a single file to a destination file of a different name and/or location. * * @param sourceFile * @param targetFile */ protected void copyFile(File sourceFile, File targetFile) { Copy copy = new Copy(); copy.setTaskName(getTaskName()); copy.setProject(getProject()); copy.setFile(sourceFile); copy.setTofile(targetFile); copy.setVerbose(isVerbose()); copy.execute(); // add target file to installed artifacts path reference(targetFile); }
/** * 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); }
/** * Create project.properties file based on $RDECK_BASE/etc/project.properties * * @param overwrite Overwrite existing properties file */ protected void generateProjectPropertiesFile(final boolean overwrite) { Copy copyTask = new Copy(); Project antProject = new Project(); antProject.setProperty("project.name", getName()); Property propTask = new Property(); propTask.setProject(antProject); propTask.setFile( new File( Constants.getFrameworkProperties( getFrameworkProjectMgr().getFramework().getBaseDir().getAbsolutePath()))); propTask.execute(); copyTask.setProject(antProject); copyTask.setOverwrite(overwrite); final File destfile = new File(getEtcDir(), PROP_FILENAME); copyTask.setTofile(destfile); copyTask.setFile( new File(getFrameworkProjectMgr().getFramework().getConfigDir(), PROP_FILENAME)); copyTask.setFiltering(true); copyTask.createFilterChain().add(new ExpandProperties()); // execute the task copyTask.execute(); getLogger().debug("generated project.properties: " + destfile.getAbsolutePath()); }
@Override public void execute() throws BuildException { if (productDir == null) { throw new BuildException("must set directory of compiled product", getLocation()); } if (file == null) { throw new BuildException("must set file for makenbm", getLocation()); } if (manifest == null && moduleName == null) { throw new BuildException("must set module for makenbm", getLocation()); } if (manifest != null && moduleName != null) { throw new BuildException("cannot set both manifest and module for makenbm", getLocation()); } if (locales == null) { locales = new ArrayList<String>(); } File nbm; String rootDir = getProject().getProperty("nbm.target.dir"); if (rootDir != null && !rootDir.equals("")) { nbm = new File(rootDir, this.file.getName()); } else { nbm = this.file; } // If desired, override the license and/or URL. // overrideURLIfNeeded(); overrideLicenseIfNeeded(); moduleAttributes = new ArrayList<Attributes>(); File module = new File(productDir, moduleName); Attributes attr = getModuleAttributesForLocale(""); if (attr == null) { // #181025: OSGi bundle, copy unmodified. Copy copy = new Copy(); copy.setProject(getProject()); copy.setOwningTarget(getOwningTarget()); copy.setFile(module); copy.setTofile(new File(nbm.getAbsolutePath().replaceFirst("[.]nbm$", ".jar"))); copy.execute(); // XXX possibly sign it // XXX could try to run pack200, though not if it was signed return; } moduleAttributes.add(attr); for (String locale : locales) { Attributes a = getModuleAttributesForLocale(locale); if (a != null) moduleAttributes.add(a); } // Will create a file Info/info.xml to be stored in tmp // The normal case; read attributes from its manifest and maybe bundle. long mMod = module.lastModified(); if (mostRecentInput < mMod) mostRecentInput = mMod; if (mostRecentInput < nbm.lastModified()) { log( "Skipping NBM creation as most recent input is younger: " + mostRecentInput + " than the target file: " + nbm.lastModified(), Project.MSG_VERBOSE); return; } else { log( "Most recent input: " + mostRecentInput + " file: " + nbm.lastModified(), Project.MSG_DEBUG); } ArrayList<ZipFileSet> infoXMLFileSets = new ArrayList<ZipFileSet>(); for (Attributes modAttr : moduleAttributes) { Document infoXmlContents = createInfoXml(modAttr); File infofile; String loc = modAttr.getValue("locale"); if (loc == null) throw new BuildException("Found attributes without assigned locale code", getLocation()); try { infofile = File.createTempFile("info_" + loc, ".xml"); OutputStream infoStream = new FileOutputStream(infofile); try { XMLUtil.write(infoXmlContents, infoStream); } finally { infoStream.close(); } } catch (IOException e) { throw new BuildException( "exception when creating Info/info.xml for locale '" + loc + "'", e, getLocation()); } infofile.deleteOnExit(); ZipFileSet infoXML = new ZipFileSet(); infoXML.setFile(infofile); if (loc.equals("")) { infoXML.setFullpath("Info/info.xml"); } else { infoXML.setFullpath("Info/locale/info_" + loc + ".xml"); log("Adding Info/locale/info_" + loc + ".xml file", Project.MSG_VERBOSE); } infoXMLFileSets.add(infoXML); } String codename = englishAttr.getValue("OpenIDE-Module"); if (codename == null) new BuildException("Can't get codenamebase"); UpdateTracking tracking = new UpdateTracking(productDir.getAbsolutePath()); Set<String> _files = new LinkedHashSet<String>(Arrays.asList(tracking.getListOfNBM(codename))); List<String> __files = new ArrayList<String>(_files); for (String f : _files) { if (f.endsWith(".external")) { // #195041 __files.remove(f.substring(0, f.length() - 9)); } } String[] files = __files.toArray(new String[__files.size()]); ZipFileSet fs = new ZipFileSet(); List<String> moduleFiles = new ArrayList<String>(); fs.setDir(productDir); String[] filesForPackaging = null; if (usePack200 && pack200excludes != null && !pack200excludes.equals("")) { FileSet pack200Files = new FileSet(); pack200Files.setDir(productDir); pack200Files.setExcludes(pack200excludes); pack200Files.setProject(getProject()); for (int i = 0; i < files.length; i++) { pack200Files.createInclude().setName(files[i]); } DirectoryScanner ds = pack200Files.getDirectoryScanner(); ds.scan(); filesForPackaging = ds.getIncludedFiles(); } List<File> packedFiles = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (usePack200) { File sourceFile = new File(productDir, files[i]); if (sourceFile.isFile() && sourceFile.getName().endsWith(".jar")) { boolean doPackage = true; if (filesForPackaging != null) { doPackage = false; for (String f : filesForPackaging) { if (new File(productDir, f).equals(sourceFile)) { doPackage = true; break; } } } if (doPackage) { // if both <filename>.jar and <filename>.jad exist - skip it // if both <filename>.jar and <filename>.jar.pack.gz exist - skip it for (String f : files) { if (f.equals(files[i].substring(0, files[i].lastIndexOf(".jar")) + ".jad") || f.equals(files[i] + ".pack.gz")) { doPackage = false; break; } } } if (doPackage) { File targetFile = new File(productDir, files[i] + ".pack.gz"); try { if (pack200(sourceFile, targetFile)) { packedFiles.add(targetFile); files[i] = files[i] + ".pack.gz"; } } catch (IOException e) { if (targetFile.exists()) { targetFile.delete(); } log("Cannot pack file " + sourceFile, e, Project.MSG_WARN); } } } } fs.createInclude().setName(files[i]); moduleFiles.add(files[i]); } fs.setPrefix("netbeans/"); // JAR it all up together. long jarModified = nbm.lastModified(); // may be 0 // log ("Ensuring existence of NBM file " + file); Jar jar = (Jar) getProject().createTask("jar"); jar.setDestFile(nbm); jar.addZipfileset(fs); for (ZipFileSet zfs : infoXMLFileSets) { jar.addFileset(zfs); } if (main != null) { // Add the main dir main.setPrefix("main"); // use main prefix jar.addZipfileset(main); DirectoryScanner ds = main.getDirectoryScanner(); ds.scan(); String[] mainFiles = ds.getIncludedFiles(); for (String m : mainFiles) { moduleFiles.add(m); } } if (executablesSet != null) { DirectoryScanner eds = executablesSet.getDirectoryScanner(); eds.scan(); String[] executables = eds.getIncludedFiles(); if (executables.length > 0) { ZipFileSet executablesList = new ZipFileSet(); File executablesFile; StringBuilder sb = new StringBuilder(""); String ls = System.getProperty("line.separator"); for (int i = 0; i < executables.length; i++) { if (i != 0) { sb.append(ls); } sb.append(executables[i].replace("\\", "/")); } try { executablesFile = File.createTempFile("executables", ".list"); OutputStream infoStream = new FileOutputStream(executablesFile); try { infoStream.write(sb.toString().getBytes("UTF-8")); } finally { infoStream.close(); } } catch (IOException e) { throw new BuildException( "exception when creating Info/executables.list", e, getLocation()); } executablesFile.deleteOnExit(); executablesList.setFile(executablesFile); executablesList.setFullpath("Info/executables.list"); jar.addZipfileset(executablesList); } } jar.setCompress(true); jar.setLocation(getLocation()); jar.init(); jar.execute(); for (File f : packedFiles) { f.delete(); } // Print messages if we overrode anything. // if (nbm.lastModified() != jarModified) { if (overrideLicense()) { log("Overriding license with: " + getLicenseOverride()); } if (overrideURL()) { log("Overriding homepage URL with: " + getURLOverride()); } } // Maybe sign it. if (signature != null && nbm.lastModified() != jarModified) { if (signature.keystore == null) throw new BuildException("must define keystore attribute on <signature/>"); if (signature.storepass == null) throw new BuildException("must define storepass attribute on <signature/>"); if (signature.alias == null) throw new BuildException("must define alias attribute on <signature/>"); if (signature.storepass.equals("?") || signature.storepass.indexOf("${") != -1 || !signature.keystore.exists()) { log( "Not signing NBM file " + nbm + "; no stored-key password provided or keystore (" + signature.keystore.toString() + ") doesn't exist", Project.MSG_WARN); } else { log("Signing NBM file " + nbm); SignJar signjar = (SignJar) getProject().createTask("signjar"); try { // Signatures changed in various Ant versions. try { SignJar.class.getMethod("setKeystore", File.class).invoke(signjar, signature.keystore); } catch (NoSuchMethodException x) { SignJar.class .getMethod("setKeystore", String.class) .invoke(signjar, signature.keystore.getAbsolutePath()); } try { SignJar.class.getMethod("setJar", File.class).invoke(signjar, nbm); } catch (NoSuchMethodException x) { SignJar.class.getMethod("setJar", String.class).invoke(signjar, nbm.getAbsolutePath()); } } catch (BuildException x) { throw x; } catch (Exception x) { throw new BuildException(x); } signjar.setStorepass(signature.storepass); signjar.setAlias(signature.alias); signjar.setLocation(getLocation()); signjar.setMaxmemory(this.jarSignerMaxMemory); signjar.init(); signjar.execute(); } } }
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); } } }