Search in sources :

Example 61 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class EvaluateMojo method getEvaluator.

/**
     * @return a lazy loading evaluator object.
     * @throws MojoExecutionException if any
     * @throws MojoFailureException if any reflection exceptions occur or missing components.
     */
private PluginParameterExpressionEvaluator getEvaluator() throws MojoExecutionException, MojoFailureException {
    if (evaluator == null) {
        MojoDescriptor mojoDescriptor;
        try {
            mojoDescriptor = mojoDescriptorCreator.getMojoDescriptor("help:evaluate", session, project);
        } catch (Exception e) {
            throw new MojoFailureException("Failure while evaluating.", e);
        }
        MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
        MavenProject currentProject = session.getCurrentProject();
        // synchronize in case another thread wants to fetch the real current project in between
        synchronized (session) {
            session.setCurrentProject(project);
            evaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
            session.setCurrentProject(currentProject);
        }
    }
    return evaluator;
}
Also used : MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) PluginParameterExpressionEvaluator(org.apache.maven.plugin.PluginParameterExpressionEvaluator) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 62 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class DescribeMojo method describePlugin.

/**
     * Method for retrieving the plugin description
     *
     * @param pd     contains the plugin description
     * @param buffer contains the information to be displayed or printed
     * @throws MojoFailureException   if any reflection exceptions occur.
     * @throws MojoExecutionException if any
     */
private void describePlugin(PluginDescriptor pd, StringBuilder buffer) throws MojoFailureException, MojoExecutionException {
    append(buffer, pd.getId(), 0);
    buffer.append(LS);
    String name = pd.getName();
    if (name == null) {
        // Can be null because of MPLUGIN-137 (and descriptors generated with maven-plugin-tools-api <= 2.4.3)
        ArtifactCoordinate coordinate = toArtifactCoordinate(pd, "jar");
        ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        pbr.setRemoteRepositories(remoteRepositories);
        pbr.setProject(null);
        pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        try {
            Artifact artifact = artifactResolver.resolveArtifact(pbr, coordinate).getArtifact();
            name = projectBuilder.build(artifact, pbr).getProject().getName();
        } catch (Exception e) {
            // oh well, we tried our best.
            getLog().warn("Unable to get the name of the plugin " + pd.getId() + ": " + e.getMessage());
            name = pd.getId();
        }
    }
    append(buffer, "Name", name, 0);
    appendAsParagraph(buffer, "Description", toDescription(pd.getDescription()), 0);
    append(buffer, "Group Id", pd.getGroupId(), 0);
    append(buffer, "Artifact Id", pd.getArtifactId(), 0);
    append(buffer, "Version", pd.getVersion(), 0);
    append(buffer, "Goal Prefix", pd.getGoalPrefix(), 0);
    buffer.append(LS);
    List<MojoDescriptor> mojos = pd.getMojos();
    if (mojos == null) {
        append(buffer, "This plugin has no goals.", 0);
        return;
    }
    if ((detail || medium) && !minimal) {
        append(buffer, "This plugin has " + mojos.size() + " goal" + (mojos.size() > 1 ? "s" : "") + ":", 0);
        buffer.append(LS);
        mojos = new ArrayList<MojoDescriptor>(mojos);
        PluginUtils.sortMojos(mojos);
        for (MojoDescriptor md : mojos) {
            if (detail) {
                describeMojoGuts(md, buffer, true);
            } else {
                describeMojoGuts(md, buffer, false);
            }
            buffer.append(LS);
        }
    }
    if (!detail) {
        buffer.append("For more information, run 'mvn help:describe [...] -Ddetail'");
        buffer.append(LS);
    }
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Artifact(org.apache.maven.artifact.Artifact) PluginVersionResolutionException(org.apache.maven.plugin.version.PluginVersionResolutionException) NoPluginFoundForPrefixException(org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 63 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class DescribeMojo method toLines.

/**
     * Invoke the following private method
     * <code>HelpMojo#toLines(String, int, int, int)</code>
     *
     * @param text       The text to split into lines, must not be <code>null</code>.
     * @param indent     The base indentation level of each line, must not be negative.
     * @param indentSize The size of each indentation, must not be negative.
     * @param lineLength The length of the line, must not be negative.
     * @return The sequence of display lines, never <code>null</code>.
     * @throws MojoFailureException   if any can not invoke the method
     * @throws MojoExecutionException if no line was found for <code>text</code>
     * @see HelpMojo#toLines(String, int, int, int)
     */
private static List<String> toLines(String text, int indent, int indentSize, int lineLength) throws MojoFailureException, MojoExecutionException {
    try {
        Method m = HelpMojo.class.getDeclaredMethod("toLines", new Class[] { String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE });
        m.setAccessible(true);
        @SuppressWarnings("unchecked") List<String> output = (List<String>) m.invoke(HelpMojo.class, text, indent, indentSize, lineLength);
        if (output == null) {
            throw new MojoExecutionException("No output was specified.");
        }
        return output;
    } catch (SecurityException e) {
        throw new MojoFailureException("SecurityException: " + e.getMessage());
    } catch (IllegalArgumentException e) {
        throw new MojoFailureException("IllegalArgumentException: " + e.getMessage());
    } catch (NoSuchMethodException e) {
        throw new MojoFailureException("NoSuchMethodException: " + e.getMessage());
    } catch (IllegalAccessException e) {
        throw new MojoFailureException("IllegalAccessException: " + e.getMessage());
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause instanceof NegativeArraySizeException) {
            throw new MojoFailureException("NegativeArraySizeException: " + cause.getMessage());
        }
        throw new MojoFailureException("InvocationTargetException: " + e.getMessage());
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 64 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class DescribeMojo method execute.

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/**
     * {@inheritDoc}
     */
public void execute() throws MojoExecutionException, MojoFailureException {
    validateParameters();
    StringBuilder descriptionBuffer = new StringBuilder();
    boolean describePlugin = true;
    if (StringUtils.isNotEmpty(cmd)) {
        describePlugin = describeCommand(descriptionBuffer);
    }
    if (describePlugin) {
        PluginInfo pi = parsePluginLookupInfo();
        PluginDescriptor descriptor = lookupPluginDescriptor(pi);
        if (StringUtils.isNotEmpty(goal)) {
            MojoDescriptor mojo = descriptor.getMojo(goal);
            if (mojo == null) {
                throw new MojoFailureException("The mojo '" + goal + "' does not exist in the plugin '" + pi.getPrefix() + "'");
            }
            describeMojo(mojo, descriptionBuffer);
        } else {
            describePlugin(descriptor, descriptionBuffer);
        }
    }
    writeDescription(descriptionBuffer);
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 65 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class AbstractSiteRenderingMojo method createSiteRenderingContext.

protected SiteRenderingContext createSiteRenderingContext(Locale locale) throws MojoExecutionException, IOException, MojoFailureException {
    DecorationModel decorationModel = prepareDecorationModel(locale);
    if (attributes == null) {
        attributes = new HashMap<String, Object>();
    }
    if (attributes.get("project") == null) {
        attributes.put("project", project);
    }
    if (attributes.get("inputEncoding") == null) {
        attributes.put("inputEncoding", getInputEncoding());
    }
    if (attributes.get("outputEncoding") == null) {
        attributes.put("outputEncoding", getOutputEncoding());
    }
    // Put any of the properties in directly into the Velocity context
    for (Map.Entry<Object, Object> entry : project.getProperties().entrySet()) {
        attributes.put((String) entry.getKey(), entry.getValue());
    }
    SiteRenderingContext context;
    if (templateFile != null) {
        getLog().info("Rendering site with " + templateFile + " template file.");
        if (!templateFile.exists()) {
            throw new MojoFailureException("Template file '" + templateFile + "' does not exist");
        }
        context = siteRenderer.createContextForTemplate(templateFile, attributes, decorationModel, project.getName(), locale);
    } else {
        try {
            Artifact skinArtifact = siteTool.getSkinArtifactFromRepository(localRepository, repositories, decorationModel);
            getLog().info("Rendering site with " + skinArtifact.getId() + " skin.");
            context = siteRenderer.createContextForSkin(skinArtifact, attributes, decorationModel, project.getName(), locale);
        } catch (SiteToolException e) {
            throw new MojoExecutionException("SiteToolException while preparing skin: " + e.getMessage(), e);
        } catch (RendererException e) {
            throw new MojoExecutionException("RendererException while preparing context for skin: " + e.getMessage(), e);
        }
    }
    // Generate static site
    if (!locale.getLanguage().equals(Locale.getDefault().getLanguage())) {
        context.addSiteDirectory(new File(siteDirectory, locale.getLanguage()));
        context.addModuleDirectory(new File(xdocDirectory, locale.getLanguage()), "xdoc");
        context.addModuleDirectory(new File(xdocDirectory, locale.getLanguage()), "fml");
    } else {
        context.addSiteDirectory(siteDirectory);
        context.addModuleDirectory(xdocDirectory, "xdoc");
        context.addModuleDirectory(xdocDirectory, "fml");
    }
    if (moduleExcludes != null) {
        context.setModuleExcludes(moduleExcludes);
    }
    if (saveProcessedContent) {
        context.setProcessedContentOutput(new File(generatedSiteDirectory, "processed"));
    }
    return context;
}
Also used : DecorationModel(org.apache.maven.doxia.site.decoration.DecorationModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Artifact(org.apache.maven.artifact.Artifact) SiteToolException(org.apache.maven.doxia.tools.SiteToolException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File)

Aggregations

MojoFailureException (org.apache.maven.plugin.MojoFailureException)157 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)94 File (java.io.File)93 IOException (java.io.IOException)91 ArrayList (java.util.ArrayList)50 TreeSet (java.util.TreeSet)33 FileInputStream (java.io.FileInputStream)32 FileOutputStream (java.io.FileOutputStream)21 List (java.util.List)21 Map (java.util.Map)21 MavenProject (org.apache.maven.project.MavenProject)18 Set (java.util.Set)15 MalformedURLException (java.net.MalformedURLException)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 InputStream (java.io.InputStream)12 URLClassLoader (java.net.URLClassLoader)12 Matcher (java.util.regex.Matcher)12 Artifact (org.apache.maven.artifact.Artifact)12 AbstractMojo (org.apache.maven.plugin.AbstractMojo)12