Ejemplo n.º 1
0
  @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();
      }
    }
  }