Exemple #1
0
  private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite)
      throws Exception {

    Jar jar = new Jar("dot", in);
    try {
      for (Entry<String, Resource> e : jar.getResources().entrySet()) {

        String path = e.getKey();
        bnd.trace("path %s", path);

        if (glob != null && !glob.matcher(path).matches()) continue;

        Resource r = e.getValue();
        File dest = Processor.getFile(workspaceDir, path);
        if (overwrite
            || !dest.isFile()
            || dest.lastModified() < r.lastModified()
            || r.lastModified() <= 0) {

          bnd.trace("copy %s to %s", path, dest);

          File dp = dest.getParentFile();
          if (!dp.exists() && !dp.mkdirs()) {
            throw new IOException("Could not create directory " + dp);
          }

          IO.copy(r.openInputStream(), dest);
        }
      }
    } finally {
      jar.close();
    }
  }
Exemple #2
0
  private void doManifest(
      Jar jar, String[] digestNames, MessageDigest[] algorithms, OutputStream out)
      throws Exception {

    for (Map.Entry<String, Resource> entry : jar.getResources().entrySet()) {
      String name = entry.getKey();
      if (!METAINFDIR.matcher(name).matches()) {
        out.write("\r\n".getBytes("UTF-8"));
        out.write("Name: ".getBytes("UTF-8"));
        out.write(name.getBytes("UTF-8"));
        out.write("\r\n".getBytes("UTF-8"));

        digest(algorithms, entry.getValue());
        for (int a = 0; a < algorithms.length; a++) {
          if (algorithms[a] != null) {
            byte[] digest = algorithms[a].digest();
            String header = digestNames[a] + "-Digest: " + new Base64(digest) + "\r\n";
            out.write(header.getBytes("UTF-8"));
          }
        }
      }
    }
  }
  @Override
  protected void doExecute() throws Exception {
    aQute.bnd.build.Project bndProject = getBndProject();

    Builder builder = new Builder(bndProject);

    builder.setClasspath(_classpathFiles.toArray(new File[_classpathFiles.size()]));
    builder.setPedantic(isPedantic());
    builder.setProperties(_file);
    builder.setSourcepath(new File[] {_sourcePath});

    Jar[] jars = builder.builds();

    // Report both task failures and bnd build failures

    boolean taskFailed = report();
    boolean bndFailed = report(builder);

    // Fail this build if failure is not ok and either the task failed or
    // the bnd build failed

    if (taskFailed || bndFailed) {
      throw new BuildException(
          "bnd failed", new org.apache.tools.ant.Location(_file.getAbsolutePath()));
    }

    for (Jar jar : jars) {
      String bsn = jar.getName();

      File outputFile = _outputPath;

      if (_outputPath.isDirectory()) {
        String path = builder.getProperty("-output");

        if (path != null) {
          outputFile = getFile(_outputPath, path);
        } else {
          outputFile = getFile(_outputPath, bsn + ".jar");
        }
      }

      if (!outputFile.exists() || (outputFile.lastModified() <= jar.lastModified())) {

        jar.write(outputFile);

        Map<String, Resource> resources = jar.getResources();

        log(jar.getName() + " (" + outputFile.getName() + ") " + resources.size());

        doBaselineJar(jar, outputFile, bndProject);
      } else {
        Map<String, Resource> resources = jar.getResources();

        log(
            jar.getName()
                + " ("
                + outputFile.getName()
                + ") "
                + resources.size()
                + " (not modified)");
      }

      report();

      jar.close();
    }

    builder.close();
  }
Exemple #4
0
  @SuppressWarnings("cast")
  private void executeBackwardCompatible() throws BuildException {
    try {
      if (files == null) throw new BuildException("No files set");

      if (eclipse) {
        File project = getProject().getBaseDir();
        EclipseClasspath cp = new EclipseClasspath(this, project.getParentFile(), project);
        classpath.addAll(cp.getClasspath());
        classpath.addAll(cp.getBootclasspath());
        sourcepath.addAll(cp.getSourcepath());
        // classpath.add(cp.getOutput());
        if (report()) throw new BuildException("Errors during Eclipse Path inspection");
      }

      if (output == null) output = getProject().getBaseDir();

      for (Iterator<File> f = files.iterator(); f.hasNext(); ) {
        File file = f.next();
        Builder builder = new Builder();

        builder.setPedantic(isPedantic());
        if (file.exists()) {
          // Do nice property calculations
          // merging includes etc.
          builder.setProperties(file);
        }

        // get them and merge them with the project
        // properties, if the inherit flag is specified
        if (inherit) {
          Properties projectProperties = new UTF8Properties();
          @SuppressWarnings("unchecked")
          Hashtable<Object, Object> antProps = getProject().getProperties();
          projectProperties.putAll(antProps);
          projectProperties.putAll(builder.getProperties());
          builder.setProperties(projectProperties);
        }

        builder.setClasspath(toFiles(classpath, "classpath"));
        builder.setSourcepath(toFiles(sourcepath, "sourcepath"));
        Jar jars[] = builder.builds();

        // Report both task failures and bnd build failures.
        boolean taskFailed = report();
        boolean bndFailed = report(builder);

        // Fail this build if failure is not ok and either the task
        // failed or the bnd build failed.
        if (!failok && (taskFailed || bndFailed)) {
          throw new BuildException(
              "bnd failed", new org.apache.tools.ant.Location(file.getAbsolutePath()));
        }

        for (int i = 0; i < jars.length; i++) {
          Jar jar = jars[i];
          String bsn = jar.getName();

          File base = file.getParentFile();
          File output = this.output;

          String path = builder.getProperty("-output");

          if (output == null) {
            if (path == null) output = getFile(base, bsn + ".jar");
            else {
              output = getFile(base, path);
            }
          } else if (output.isDirectory()) {
            if (path == null) output = getFile(this.output, bsn + ".jar");
            else output = getFile(this.output, path);
          } else if (output.isFile()) {
            if (files.size() > 1) messages.GotFileNeedDir_(output.getAbsoluteFile());
          }

          String msg = "";
          if (!output.exists() || output.lastModified() <= jar.lastModified()) {
            jar.write(output);
          } else {
            msg = "(not modified)";
          }
          trace("%s (%s) %s %s", jar.getName(), output.getName(), jar.getResources().size(), msg);
          report();
          jar.close();
        }
        builder.close();
      }
    } catch (Exception e) {
      // if (exceptions)
      e.printStackTrace();
      if (!failok) throw new BuildException("Failed to build jar file: ", e);
    }
  }