コード例 #1
0
  protected void setUp() throws Exception {
    // prepare plexus environment
    super.setUp();

    ajcMojo.project = project;
    String temp = new File(".").getAbsolutePath();
    basedir = temp.substring(0, temp.length() - 2) + "/src/test/projects/" + getProjectName() + "/";
    project.getBuild().setDirectory(basedir + "/target");
    project.getBuild().setOutputDirectory(basedir + "/target/classes");
    project.getBuild().setTestOutputDirectory(basedir + "/target/test-classes");
    project.getBuild().setSourceDirectory(basedir + "/src/main/java");
    project.getBuild().setTestSourceDirectory(basedir + "/src/test/java");
    project.addCompileSourceRoot(project.getBuild().getSourceDirectory());
    project.addTestCompileSourceRoot(project.getBuild().getTestSourceDirectory());
    ajcMojo.basedir = new File(basedir);

    setVariableValueToObject(
        ajcMojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));

    ArtifactHandler artifactHandler = new MockArtifactHandler();
    Artifact artifact = new MockArtifact("dill", "dall");
    artifact.setArtifactHandler(artifactHandler);
    project.setArtifact(artifact);
    project.setDependencyArtifacts(Collections.EMPTY_SET);
  }
コード例 #2
0
  @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.");
  }