public ClassRealm getPluginClassRealm(MavenSession session, MojoExecution mojoExecution)
     throws CoreException {
   IMaven mvn = MavenPlugin.getMaven();
   // call for side effect of ensuring that the realm is set in the
   // descriptor.
   mvn.getConfiguredMojo(session, mojoExecution, Mojo.class);
   MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
   return mojoDescriptor.getPluginDescriptor().getClassRealm();
 }
Exemplo n.º 2
0
  /** {@inheritDoc} */
  public boolean equals(Object object) {
    if (this == object) {
      return true;
    }

    if (object instanceof MojoDescriptor) {
      MojoDescriptor other = (MojoDescriptor) object;

      if (!compareObjects(getPluginDescriptor(), other.getPluginDescriptor())) {
        return false;
      }

      if (!compareObjects(getGoal(), other.getGoal())) {
        return false;
      }

      return true;
    }

    return false;
  }
Exemplo n.º 3
0
 /**
  * Find and configure a mojor.
  *
  * @param coords Maven coordinates, e.g. "com.qulice:maven-qulice-plugin:1.0"
  * @param goal Maven plugin goal to execute
  * @param config The configuration to set
  * @throws ValidationException If something is wrong inside
  */
 public void execute(final String coords, final String goal, final Properties config)
     throws ValidationException {
   final Plugin plugin = new Plugin();
   final String[] sectors = StringUtils.split(coords, ':');
   plugin.setGroupId(sectors[0]);
   plugin.setArtifactId(sectors[1]);
   plugin.setVersion(sectors[2]);
   final MojoDescriptor descriptor = this.descriptor(plugin, goal);
   try {
     this.helper.setupPluginRealm(
         descriptor.getPluginDescriptor(),
         this.session,
         Thread.currentThread().getContextClassLoader(),
         new LinkedList<String>(),
         new LinkedList<String>());
   } catch (final PluginResolutionException ex) {
     throw new IllegalStateException("Plugin resolution problem", ex);
   } catch (final PluginContainerException ex) {
     throw new IllegalStateException("Can't setup realm", ex);
   }
   final Xpp3Dom xpp =
       Xpp3Dom.mergeXpp3Dom(
           this.toXppDom(config, "configuration"),
           this.toXppDom(descriptor.getMojoConfiguration()));
   final MojoExecution execution = new MojoExecution(descriptor, xpp);
   final Mojo mojo = this.mojo(execution);
   Logger.info(this, "Calling %s:%s...", coords, goal);
   try {
     mojo.execute();
   } catch (final MojoExecutionException ex) {
     throw new IllegalArgumentException(ex);
   } catch (final MojoFailureException ex) {
     throw new ValidationException(ex);
   }
   this.manager.releaseMojo(mojo, execution);
 }
  public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
    Object value = null;

    if (expr == null) {
      return null;
    }

    String expression = stripTokens(expr);
    if (expression.equals(expr)) {
      int index = expr.indexOf("${");
      if (index >= 0) {
        int lastIndex = expr.indexOf("}", index);
        if (lastIndex >= 0) {
          String retVal = expr.substring(0, index);

          if ((index > 0) && (expr.charAt(index - 1) == '$')) {
            retVal += expr.substring(index + 1, lastIndex + 1);
          } else {
            Object subResult = evaluate(expr.substring(index, lastIndex + 1));

            if (subResult != null) {
              retVal += subResult;
            } else {
              retVal += "$" + expr.substring(index + 1, lastIndex + 1);
            }
          }

          retVal += evaluate(expr.substring(lastIndex + 1));
          return retVal;
        }
      }

      // Was not an expression
      if (expression.contains("$$")) {
        return expression.replaceAll("\\$\\$", "\\$");
      } else {
        return expression;
      }
    }

    MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();

    if ("localRepository".equals(expression)) {
      value = session.getLocalRepository();
    } else if ("session".equals(expression)) {
      value = session;
    } else if (expression.startsWith("session")) {
      try {
        int pathSeparator = expression.indexOf("/");

        if (pathSeparator > 0) {
          String pathExpression = expression.substring(1, pathSeparator);
          value = ReflectionValueExtractor.evaluate(pathExpression, session);
          value = value + expression.substring(pathSeparator);
        } else {
          value = ReflectionValueExtractor.evaluate(expression.substring(1), session);
        }
      } catch (Exception e) {
        // TODO: don't catch exception
        throw new ExpressionEvaluationException(
            "Error evaluating plugin parameter expression: " + expression, e);
      }
    } else if ("reactorProjects".equals(expression)) {
      value = session.getProjects();
    } else if ("mojoExecution".equals(expression)) {
      value = mojoExecution;
    } else if ("project".equals(expression)) {
      value = project;
    } else if ("executedProject".equals(expression)) {
      value = project.getExecutionProject();
    } else if (expression.startsWith("project") || expression.startsWith("pom")) {
      try {
        int pathSeparator = expression.indexOf("/");

        if (pathSeparator > 0) {
          String pathExpression = expression.substring(0, pathSeparator);
          value = ReflectionValueExtractor.evaluate(pathExpression, project);
          value = value + expression.substring(pathSeparator);
        } else {
          value = ReflectionValueExtractor.evaluate(expression.substring(1), project);
        }
      } catch (Exception e) {
        // TODO: don't catch exception
        throw new ExpressionEvaluationException(
            "Error evaluating plugin parameter expression: " + expression, e);
      }
    } else if (expression.equals("repositorySystemSession")) {
      value = session.getRepositorySession();
    } else if (expression.equals("mojo")) {
      value = mojoExecution;
    } else if (expression.startsWith("mojo")) {
      try {
        int pathSeparator = expression.indexOf("/");

        if (pathSeparator > 0) {
          String pathExpression = expression.substring(1, pathSeparator);
          value = ReflectionValueExtractor.evaluate(pathExpression, mojoExecution);
          value = value + expression.substring(pathSeparator);
        } else {
          value = ReflectionValueExtractor.evaluate(expression.substring(1), mojoExecution);
        }
      } catch (Exception e) {
        // TODO: don't catch exception
        throw new ExpressionEvaluationException(
            "Error evaluating plugin parameter expression: " + expression, e);
      }
    } else if (expression.equals("plugin")) {
      value = mojoDescriptor.getPluginDescriptor();
    } else if (expression.startsWith("plugin")) {
      try {
        int pathSeparator = expression.indexOf("/");

        PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();

        if (pathSeparator > 0) {
          String pathExpression = expression.substring(1, pathSeparator);
          value = ReflectionValueExtractor.evaluate(pathExpression, pluginDescriptor);
          value = value + expression.substring(pathSeparator);
        } else {
          value = ReflectionValueExtractor.evaluate(expression.substring(1), pluginDescriptor);
        }
      } catch (Exception e) {
        throw new ExpressionEvaluationException(
            "Error evaluating plugin parameter expression: " + expression, e);
      }
    } else if ("settings".equals(expression)) {
      value = session.getSettings();
    } else if (expression.startsWith("settings")) {
      try {
        int pathSeparator = expression.indexOf("/");

        if (pathSeparator > 0) {
          String pathExpression = expression.substring(1, pathSeparator);
          value = ReflectionValueExtractor.evaluate(pathExpression, session.getSettings());
          value = value + expression.substring(pathSeparator);
        } else {
          value = ReflectionValueExtractor.evaluate(expression.substring(1), session.getSettings());
        }
      } catch (Exception e) {
        // TODO: don't catch exception
        throw new ExpressionEvaluationException(
            "Error evaluating plugin parameter expression: " + expression, e);
      }
    } else if ("basedir".equals(expression)) {
      value = basedir;
    } else if (expression.startsWith("basedir")) {
      int pathSeparator = expression.indexOf("/");

      if (pathSeparator > 0) {
        value = basedir + expression.substring(pathSeparator);
      }
    }

    /*
     * MNG-4312: We neither have reserved all of the above magic expressions nor is their set fixed/well-known (it
     * gets occasionally extended by newer Maven versions). This imposes the risk for existing plugins to
     * unintentionally use such a magic expression for an ordinary system property. So here we check whether we
     * ended up with a magic value that is not compatible with the type of the configured mojo parameter (a string
     * could still be converted by the configurator so we leave those alone). If so, back off to evaluating the
     * expression from properties only.
     */
    if (value != null
        && type != null
        && !(value instanceof String)
        && !isTypeCompatible(type, value)) {
      value = null;
    }

    if (value == null) {
      // The CLI should win for defining properties

      if ((value == null) && (properties != null)) {
        // We will attempt to get nab a system property as a way to specify a
        // parameter to a plugins. My particular case here is allowing the surefire
        // plugin to run a single test so I want to specify that class on the cli
        // as a parameter.

        value = properties.getProperty(expression);
      }

      if ((value == null) && ((project != null) && (project.getProperties() != null))) {
        value = project.getProperties().getProperty(expression);
      }
    }

    if (value instanceof String) {
      // TODO: without #, this could just be an evaluate call...

      String val = (String) value;

      int exprStartDelimiter = val.indexOf("${");

      if (exprStartDelimiter >= 0) {
        if (exprStartDelimiter > 0) {
          value =
              val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
        } else {
          value = evaluate(val.substring(exprStartDelimiter));
        }
      }
    }

    return value;
  }
  public Map<String, List<MojoExecution>> calculateLifecycleMappings(
      MavenSession session, MavenProject project, Lifecycle lifecycle, String lifecyclePhase)
      throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
          MojoNotFoundException, InvalidPluginDescriptorException {
    /*
     * Initialize mapping from lifecycle phase to bound mojos. The key set of this map denotes the phases the caller
     * is interested in, i.e. all phases up to and including the specified phase.
     */

    Map<String, Map<Integer, List<MojoExecution>>> mappings =
        new LinkedHashMap<String, Map<Integer, List<MojoExecution>>>();

    for (String phase : lifecycle.getPhases()) {
      Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<Integer, List<MojoExecution>>();

      mappings.put(phase, phaseBindings);

      if (phase.equals(lifecyclePhase)) {
        break;
      }
    }

    /*
     * Grab plugin executions that are bound to the selected lifecycle phases from project. The effective model of
     * the project already contains the plugin executions induced by the project's packaging type. Remember, all
     * phases of interest and only those are in the lifecyle mapping, if a phase has no value in the map, we are not
     * interested in any of the executions bound to it.
     */

    for (Plugin plugin : project.getBuild().getPlugins()) {
      for (PluginExecution execution : plugin.getExecutions()) {
        // if the phase is specified then I don't have to go fetch the plugin yet and pull it down
        // to examine the phase it is associated to.
        if (execution.getPhase() != null) {
          Map<Integer, List<MojoExecution>> phaseBindings = mappings.get(execution.getPhase());
          if (phaseBindings != null) {
            for (String goal : execution.getGoals()) {
              MojoExecution mojoExecution = new MojoExecution(plugin, goal, execution.getId());
              mojoExecution.setLifecyclePhase(execution.getPhase());
              addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
            }
          }
        }
        // if not then i need to grab the mojo descriptor and look at the phase that is specified
        else {
          for (String goal : execution.getGoals()) {
            MojoDescriptor mojoDescriptor =
                pluginManager.getMojoDescriptor(
                    plugin,
                    goal,
                    project.getRemotePluginRepositories(),
                    session.getRepositorySession());

            Map<Integer, List<MojoExecution>> phaseBindings =
                mappings.get(mojoDescriptor.getPhase());
            if (phaseBindings != null) {
              MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, execution.getId());
              mojoExecution.setLifecyclePhase(mojoDescriptor.getPhase());
              addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
            }
          }
        }
      }
    }

    Map<String, List<MojoExecution>> lifecycleMappings =
        new LinkedHashMap<String, List<MojoExecution>>();

    for (Map.Entry<String, Map<Integer, List<MojoExecution>>> entry : mappings.entrySet()) {
      List<MojoExecution> mojoExecutions = new ArrayList<MojoExecution>();

      for (List<MojoExecution> executions : entry.getValue().values()) {
        mojoExecutions.addAll(executions);
      }

      lifecycleMappings.put(entry.getKey(), mojoExecutions);
    }

    return lifecycleMappings;
  }