@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();
  }
Example #2
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);
    }
  }