Ejemplo n.º 1
0
  private Artifact XcreateArtifact(
      String groupId,
      String artifactId,
      VersionRange versionRange,
      String type,
      String classifier,
      String scope,
      String inheritedScope,
      boolean optional) {
    String desiredScope = Artifact.SCOPE_RUNTIME;

    if (inheritedScope == null) {
      desiredScope = scope;
    } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
      return null;
    } else if (Artifact.SCOPE_COMPILE.equals(scope)
        && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
      // added to retain compile artifactScope. Remove if you want compile inherited as runtime
      desiredScope = Artifact.SCOPE_COMPILE;
    }

    if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
      desiredScope = Artifact.SCOPE_TEST;
    }

    if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
      desiredScope = Artifact.SCOPE_PROVIDED;
    }

    if (Artifact.SCOPE_SYSTEM.equals(scope)) {
      // system scopes come through unchanged...
      desiredScope = Artifact.SCOPE_SYSTEM;
    }

    ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);

    return new DefaultArtifact(
        groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
  }
  @SuppressWarnings("unchecked")
  public void execute() throws MojoExecutionException {
    ArtifactVersion currentVersion = ri.getApplicationVersion();
    ArtifactVersion maxUsageVersion = new DefaultArtifactVersion("2.2.0");

    if (maxUsageVersion.compareTo(currentVersion) < 0) {
      getLog()
          .debug(
              "This version of Maven does not require injection of custom ArtifactHandlers using this code. Skipping.");
      return;
    }

    Map<String, ?> handlerDescriptors =
        session.getContainer().getComponentDescriptorMap(ArtifactHandler.ROLE);
    if (handlerDescriptors != null) {
      getLog().debug("Registering all unregistered ArtifactHandlers...");

      if (artifactHandlerManager instanceof DefaultArtifactHandlerManager) {
        Set<String> existingHints =
            ((DefaultArtifactHandlerManager) artifactHandlerManager).getHandlerTypes();
        if (existingHints != null) {
          for (String hint : existingHints) {
            handlerDescriptors.remove(hint);
          }
        }
      }

      if (handlerDescriptors.isEmpty()) {
        getLog().debug("All ArtifactHandlers are registered. Continuing...");
      } else {
        Map<String, ArtifactHandler> unregisteredHandlers =
            new HashMap<String, ArtifactHandler>(handlerDescriptors.size());

        for (String hint : handlerDescriptors.keySet()) {
          try {
            unregisteredHandlers.put(
                hint, (ArtifactHandler) session.lookup(ArtifactHandler.ROLE, hint));
            getLog().info("Adding ArtifactHandler for: " + hint);
          } catch (ComponentLookupException e) {
            getLog()
                .warn(
                    "Failed to lookup ArtifactHandler with hint: "
                        + hint
                        + ". Reason: "
                        + e.getMessage(),
                    e);
          }
        }

        artifactHandlerManager.addHandlers(unregisteredHandlers);
      }
    }

    getLog()
        .debug("...done.\nSetting ArtifactHandler on project-artifact: " + project.getId() + "...");

    Set<Artifact> artifacts = new HashSet<Artifact>();
    artifacts.add(project.getArtifact());

    Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    if (dependencyArtifacts != null && !dependencyArtifacts.isEmpty()) {
      artifacts.addAll(dependencyArtifacts);
    }

    for (Artifact artifact : artifacts) {
      String type = artifact.getType();
      ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);

      getLog()
          .debug(
              "Artifact: "
                  + artifact.getId()
                  + "\nType: "
                  + type
                  + "\nArtifactHandler extension: "
                  + handler.getExtension());

      artifact.setArtifactHandler(handler);
    }

    getLog().debug("...done.");
  }