/**
  * Fired when a target starts building, this pushes a timed element for the target onto the stack
  * of elements for the current thread, remembering the current time and the name of the target.
  *
  * @param event An event with any relevant extra information. Will not be <code>null</code>.
  */
 public void targetStarted(BuildEvent event) {
   System.out.println("target started in cruise control HeliumCCLogger");
   Target target = event.getTarget();
   TimedElement targetElement = new TimedElement();
   targetElement.startTime = System.currentTimeMillis();
   targetElement.element = doc.createElement(TARGET_TAG);
   targetElement.element.setAttribute(NAME_ATTR, target.getName());
   targets.put(target, targetElement);
   getStack().push(targetElement);
 }
Example #2
0
  private List generateSequence(List projects) {
    DefaultModelFactory factory = new DefaultModelFactory();
    Repository repository = (Repository) getProject().getReference("q.project.repository");
    factory.setRepository(repository);

    // 1st pass: map artifacts generated from each project to a target representing their project
    // files
    Project tempProject = new Project();

    Map targetsByArtifact = new HashMap();
    Map targetsByProject = new HashMap();
    List models = new ArrayList();

    for (Iterator i = projects.iterator(); i.hasNext(); ) {
      File projectFile = (File) i.next();

      // For now, we assume the default profile will contain all dependencies
      ProjectModel model =
          factory.getProjectModel(
              projectFile, Collections.EMPTY_LIST, true, null, new TaskLogger(this), getProject());
      models.add(model);

      // Create a target for each project file
      Target target = new Target();
      target.setName(projectFile.getAbsolutePath());
      tempProject.addTarget(target);
      targetsByProject.put(model.getProject().getProjectFile(), target);

      // Map the artifacts to the target
      for (Iterator j = model.getProject().getArtifacts().iterator(); j.hasNext(); ) {
        Artifact artifact = (Artifact) j.next();
        targetsByArtifact.put(artifact.getId(), target);
      }
    }

    // 2nd pass: Process project dependencies. Where a dependency uses an artifactId found in the
    // 1st pass,
    // set up a dependency between the projects.
    for (Iterator i = models.iterator(); i.hasNext(); ) {
      ProjectModel model = (ProjectModel) i.next();
      Target projectTarget = (Target) targetsByProject.get(model.getProject().getProjectFile());

      Set dependencies = getDependencies(model);

      // Where there is a dependency between projects, mirror it in the targets
      for (Iterator j = dependencies.iterator(); j.hasNext(); ) {
        RepoArtifactId id = (RepoArtifactId) j.next();
        Target dependencyTarget = (Target) targetsByArtifact.get(id);

        if (dependencyTarget != null) {
          projectTarget.addDependency(dependencyTarget.getName());
        }
      }
    }

    // Use ANT's target dependency mechanism to produce a build sequence
    String anyTarget = ((Target) targetsByArtifact.values().iterator().next()).getName();
    Collection sequencedTargets = tempProject.topoSort(anyTarget, tempProject.getTargets());

    // Transform targets into a list of files
    List sequence = new ArrayList();

    for (Iterator i = sequencedTargets.iterator(); i.hasNext(); ) {
      Target seqTarget = (Target) i.next();
      sequence.add(new File(seqTarget.getName()));
    }

    return sequence;
  }
Example #3
0
    /**
     * Initialisation routine called after handler creation with the element name and attributes.
     * The attributes which this handler can deal with are: <code>"name"</code>, <code>"depends"
     * </code>, <code>"if"</code>, <code>"unless"</code>, <code>"id"</code> and <code>"description"
     * </code>.
     *
     * @param uri The namespace URI for this element.
     * @param tag Name of the element which caused this handler to be created. Should not be <code>
     *     null</code>. Ignored in this implementation.
     * @param qname The qualified name for this element.
     * @param attrs Attributes of the element which caused this handler to be created. Must not be
     *     <code>null</code>.
     * @param context The current context.
     * @exception SAXParseException if an unexpected attribute is encountered or if the <code>"name"
     *     </code> attribute is missing.
     */
    public void onStartElement(
        String uri, String tag, String qname, Attributes attrs, AntXMLContext context)
        throws SAXParseException {
      String name = null;
      String depends = "";
      String extensionPoint = null;
      OnMissingExtensionPoint extensionPointMissing = null;

      Project project = context.getProject();
      Target target = "target".equals(tag) ? new Target() : new ExtensionPoint();
      target.setProject(project);
      target.setLocation(new Location(context.getLocator()));
      context.addTarget(target);

      for (int i = 0; i < attrs.getLength(); i++) {
        String attrUri = attrs.getURI(i);
        if (attrUri != null && !attrUri.equals("") && !attrUri.equals(uri)) {
          continue; // Ignore attributes from unknown uris
        }
        String key = attrs.getLocalName(i);
        String value = attrs.getValue(i);

        if (key.equals("name")) {
          name = value;
          if ("".equals(name)) {
            throw new BuildException("name attribute must " + "not be empty");
          }
        } else if (key.equals("depends")) {
          depends = value;
        } else if (key.equals("if")) {
          target.setIf(value);
        } else if (key.equals("unless")) {
          target.setUnless(value);
        } else if (key.equals("id")) {
          if (value != null && !value.equals("")) {
            context.getProject().addReference(value, target);
          }
        } else if (key.equals("description")) {
          target.setDescription(value);
        } else if (key.equals("extensionOf")) {
          extensionPoint = value;
        } else if (key.equals("onMissingExtensionPoint")) {
          try {
            extensionPointMissing = OnMissingExtensionPoint.valueOf(value);
          } catch (IllegalArgumentException e) {
            throw new BuildException("Invalid onMissingExtensionPoint " + value);
          }
        } else {
          throw new SAXParseException("Unexpected attribute \"" + key + "\"", context.getLocator());
        }
      }

      if (name == null) {
        throw new SAXParseException(
            "target element appears without a name attribute", context.getLocator());
      }

      String prefix = null;
      boolean isInIncludeMode = context.isIgnoringProjectTag() && isInIncludeMode();
      String sep = getCurrentPrefixSeparator();

      if (isInIncludeMode) {
        prefix = getTargetPrefix(context);
        if (prefix == null) {
          throw new BuildException(
              "can't include build file "
                  + context.getBuildFileURL()
                  + ", no as attribute has been given"
                  + " and the project tag doesn't"
                  + " specify a name attribute");
        }
        name = prefix + sep + name;
      }

      // Check if this target is in the current build file
      if (context.getCurrentTargets().get(name) != null) {
        throw new BuildException("Duplicate target '" + name + "'", target.getLocation());
      }
      Hashtable<String, Target> projectTargets = project.getTargets();
      boolean usedTarget = false;
      // If the name has not already been defined define it
      if (projectTargets.containsKey(name)) {
        project.log(
            "Already defined in main or a previous import, ignore " + name, Project.MSG_VERBOSE);
      } else {
        target.setName(name);
        context.getCurrentTargets().put(name, target);
        project.addOrReplaceTarget(name, target);
        usedTarget = true;
      }

      if (depends.length() > 0) {
        if (!isInIncludeMode) {
          target.setDepends(depends);
        } else {
          for (String string : Target.parseDepends(depends, name, "depends")) {
            target.addDependency(prefix + sep + string);
          }
        }
      }
      if (!isInIncludeMode
          && context.isIgnoringProjectTag()
          && (prefix = getTargetPrefix(context)) != null) {
        // In an imported file (and not completely
        // ignoring the project tag or having a preconfigured prefix)
        String newName = prefix + sep + name;
        Target newTarget = target;
        if (usedTarget) {
          newTarget = "target".equals(tag) ? new Target(target) : new ExtensionPoint(target);
        }
        newTarget.setName(newName);
        context.getCurrentTargets().put(newName, newTarget);
        project.addOrReplaceTarget(newName, newTarget);
      }
      if (extensionPointMissing != null && extensionPoint == null) {
        throw new BuildException(
            "onMissingExtensionPoint attribute cannot "
                + "be specified unless extensionOf is specified",
            target.getLocation());
      }
      if (extensionPoint != null) {
        ProjectHelper helper =
            (ProjectHelper)
                context.getProject().getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
        for (String extPointName : Target.parseDepends(extensionPoint, name, "extensionOf")) {
          if (extensionPointMissing == null) {
            extensionPointMissing = OnMissingExtensionPoint.FAIL;
          }
          // defer extensionpoint resolution until the full
          // import stack has been processed
          if (isInIncludeMode()) {
            // if in include mode, provide prefix we're including by
            // so that we can try and resolve extension point from
            // the local file first
            helper
                .getExtensionStack()
                .add(
                    new String[] {
                      extPointName, target.getName(), extensionPointMissing.name(), prefix + sep
                    });
          } else {
            helper
                .getExtensionStack()
                .add(new String[] {extPointName, target.getName(), extensionPointMissing.name()});
          }
        }
      }
    }