Search in sources :

Example 26 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class PdfMojo method generateMavenReport.

/**
     * Generate the given Maven report only if it is not an external report and the report could be generated.
     *
     * @param mojoDescriptor not null, to catch linkage error
     * @param report could be null
     * @param locale not null
     * @throws IOException if any
     * @throws MojoExecutionException if any
     * @see #isValidGeneratedReport(MojoDescriptor, File, String)
     * @since 1.1
     */
private void generateMavenReport(MavenReport report, Artifact pluginArtifact, Locale locale) throws IOException, MojoExecutionException {
    if (report == null) {
        return;
    }
    String localReportName = report.getName(locale);
    if (!report.canGenerateReport()) {
        getLog().info("Skipped \"" + localReportName + "\" report.");
        getLog().debug("canGenerateReport() was false.");
        return;
    }
    if (report.isExternalReport()) {
        getLog().info("Skipped external \"" + localReportName + "\" report.");
        getLog().debug("isExternalReport() was false.");
        return;
    }
    for (final MavenReport generatedReport : getGeneratedMavenReports(locale)) {
        if (report.getName(locale).equals(generatedReport.getName(locale))) {
            if (getLog().isDebugEnabled()) {
                getLog().debug(report.getName(locale) + " was already generated.");
            }
            return;
        }
    }
    File outDir = new File(getGeneratedSiteDirectoryTmp(), "xdoc");
    if (!locale.getLanguage().equals(defaultLocale.getLanguage())) {
        outDir = new File(new File(getGeneratedSiteDirectoryTmp(), locale.getLanguage()), "xdoc");
    }
    outDir.mkdirs();
    File generatedReport = new File(outDir, report.getOutputName() + ".xml");
    String excludes = getDefaultExcludesWithLocales(getAvailableLocales(), getDefaultLocale());
    List<String> files = FileUtils.getFileNames(siteDirectory, "*/" + report.getOutputName() + ".*", excludes, false);
    if (!locale.getLanguage().equals(defaultLocale.getLanguage())) {
        files = FileUtils.getFileNames(new File(siteDirectory, locale.getLanguage()), "*/" + report.getOutputName() + ".*", excludes, false);
    }
    if (files.size() != 0) {
        String displayLanguage = locale.getDisplayLanguage(Locale.ENGLISH);
        if (getLog().isInfoEnabled()) {
            getLog().info("Skipped \"" + report.getName(locale) + "\" report, file \"" + report.getOutputName() + "\" already exists for the " + displayLanguage + " version.");
        }
        return;
    }
    if (getLog().isInfoEnabled()) {
        getLog().info("Generating \"" + localReportName + "\" report.");
    }
    StringWriter sw = new StringWriter();
    PdfSink sink = null;
    try {
        sink = new PdfSink(sw);
        org.codehaus.doxia.sink.Sink proxy = (org.codehaus.doxia.sink.Sink) Proxy.newProxyInstance(org.codehaus.doxia.sink.Sink.class.getClassLoader(), new Class[] { org.codehaus.doxia.sink.Sink.class }, new SinkDelegate(sink));
        report.generate(proxy, locale);
    } catch (MavenReportException e) {
        throw new MojoExecutionException("MavenReportException: " + e.getMessage(), e);
    } finally {
        if (sink != null) {
            sink.close();
        }
    }
    writeGeneratedReport(sw.toString(), generatedReport);
    if (isValidGeneratedReport(pluginArtifact, generatedReport, localReportName)) {
        getGeneratedMavenReports(locale).add(report);
    }
}
Also used : MavenReport(org.apache.maven.reporting.MavenReport) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) Sink(org.apache.maven.doxia.sink.Sink) IndexingSink(org.apache.maven.doxia.index.IndexingSink) XdocSink(org.apache.maven.doxia.module.xdoc.XdocSink) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 27 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class LinkcheckReport method executeReport.

// ----------------------------------------------------------------------
// Protected methods
// ----------------------------------------------------------------------
/** {@inheritDoc} */
protected void executeReport(Locale locale) throws MavenReportException {
    if (result == null) {
        getLog().debug("Calling execute()");
        try {
            this.execute();
        } catch (MojoExecutionException e) {
            throw new MavenReportException("MojoExecutionException: " + e.getMessage(), e);
        }
    }
    if (result != null) {
        generateReport(locale, result);
        // free memory
        result = null;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 28 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class DefaultCheckstyleRssGenerator method generateRSS.

@Override
public void generateRSS(CheckstyleResults results, CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest) throws MavenReportException {
    VelocityTemplate vtemplate = new VelocityTemplate(velocityComponent, CheckstyleReport.PLUGIN_RESOURCES);
    vtemplate.setLog(checkstyleRssGeneratorRequest.getLog());
    Context context = new VelocityContext();
    context.put("results", results);
    context.put("project", checkstyleRssGeneratorRequest.getMavenProject());
    context.put("copyright", checkstyleRssGeneratorRequest.getCopyright());
    context.put("levelInfo", SeverityLevel.INFO);
    context.put("levelWarning", SeverityLevel.WARNING);
    context.put("levelError", SeverityLevel.ERROR);
    context.put("stringutils", new StringUtils());
    try {
        vtemplate.generate(checkstyleRssGeneratorRequest.getOutputDirectory().getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context);
    } catch (ResourceNotFoundException e) {
        throw new MavenReportException("Unable to find checkstyle-rss.vm resource.", e);
    } catch (MojoExecutionException | IOException | VelocityException e) {
        throw new MavenReportException("Unable to generate checkstyle.rss.", e);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) StringUtils(org.codehaus.plexus.util.StringUtils) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 29 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class ReportDocumentRenderer method generateMultiPage.

/**
     * Try to generate report with extended multi-page API.
     * 
     * @return <code>true</code> if the report was compatible with the extended API
     */
private boolean generateMultiPage(Locale locale, SinkFactory sf, Sink sink) throws MavenReportException {
    try {
        // MavenMultiPageReport is not in Maven Core, then the class is different in site plugin and in each report
        // plugin: only reflection can let us invoke its method
        Method generate = report.getClass().getMethod("generate", Sink.class, SinkFactory.class, Locale.class);
        generate.invoke(report, sink, sf, locale);
        return true;
    } catch (SecurityException se) {
        return false;
    } catch (NoSuchMethodException nsme) {
        return false;
    } catch (IllegalArgumentException iae) {
        throw new MavenReportException("error while invoking generate", iae);
    } catch (IllegalAccessException iae) {
        throw new MavenReportException("error while invoking generate", iae);
    } catch (InvocationTargetException ite) {
        throw new MavenReportException("error while invoking generate", ite);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 30 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class ReportDocumentRenderer method renderDocument.

public void renderDocument(Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext) throws RendererException, FileNotFoundException {
    Locale locale = siteRenderingContext.getLocale();
    String localReportName = report.getName(locale);
    String msg = "Generating \"" + buffer().strong(localReportName) + "\" report";
    // CHECKSTYLE_OFF: MagicNumber
    log.info(reportMojoInfo == null ? (msg + '.') : (StringUtils.rightPad(msg, 40) + buffer().strong(" --- ").mojo(reportMojoInfo)));
    // CHECKSTYLE_ON: MagicNumber
    // main sink
    SiteRendererSink mainSink = new SiteRendererSink(renderingContext);
    // sink factory, for multi-page reports that need sub-sinks
    MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory(renderingContext);
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (classLoader != null) {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        if (report instanceof MavenMultiPageReport) {
            // extended multi-page API
            ((MavenMultiPageReport) report).generate(mainSink, multiPageSinkFactory, locale);
        } else if (generateMultiPage(locale, multiPageSinkFactory, mainSink)) {
        // extended multi-page API for Maven 2.2, only accessible by reflection API
        } else {
            // old single-page-only API
            report.generate(mainSink, locale);
        }
    } catch (MavenReportException e) {
        String report = (reportMojoInfo == null) ? ('"' + localReportName + "\" report") : reportMojoInfo;
        throw new RendererException("Error generating " + report + ": " + e.getMessage(), e);
    } catch (LinkageError e) {
        String report = (reportMojoInfo == null) ? ('"' + localReportName + "\" report") : reportMojoInfo;
        log.warn("An issue has occurred with " + report + ", skipping LinkageError " + e.getMessage() + ", please report an issue to Maven dev team.", e);
    } finally {
        if (classLoader != null) {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
        mainSink.close();
    }
    if (report.isExternalReport()) {
        // external reports are rendered from their own: no Doxia site rendering needed
        return;
    }
    // render main sink
    renderer.generateDocument(writer, mainSink, siteRenderingContext);
    // render sub-sinks, eventually created by multi-page reports
    try {
        List<MultiPageSubSink> sinks = multiPageSinkFactory.sinks();
        log.debug("Multipage report: " + sinks.size() + " subreports");
        for (MultiPageSubSink mySink : sinks) {
            mySink.enableLogging(new MojoLogWrapper(log));
            log.debug("  Rendering " + mySink.getOutputName());
            File outputFile = new File(mySink.getOutputDir(), mySink.getOutputName());
            Writer out = null;
            try {
                out = WriterFactory.newWriter(outputFile, siteRenderingContext.getOutputEncoding());
                renderer.generateDocument(out, mySink, siteRenderingContext);
                mySink.close();
                mySink = null;
                out.close();
                out = null;
            } finally {
                IOUtil.close(out);
                if (mySink != null) {
                    mySink.close();
                }
            }
        }
    } catch (IOException e) {
        throw new RendererException("Cannot create writer", e);
    }
}
Also used : Locale(java.util.Locale) IOException(java.io.IOException) MavenMultiPageReport(org.apache.maven.reporting.MavenMultiPageReport) RendererException(org.apache.maven.doxia.siterenderer.RendererException) MojoLogWrapper(org.apache.maven.doxia.tools.MojoLogWrapper) SiteRendererSink(org.apache.maven.doxia.siterenderer.sink.SiteRendererSink) File(java.io.File) Writer(java.io.Writer) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

MavenReportException (org.apache.maven.reporting.MavenReportException)48 File (java.io.File)23 IOException (java.io.IOException)23 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)14 ArrayList (java.util.ArrayList)10 Artifact (org.apache.maven.artifact.Artifact)7 FileOutputStream (java.io.FileOutputStream)6 Locale (java.util.Locale)6 BootclasspathArtifact (org.apache.maven.plugin.javadoc.options.BootclasspathArtifact)6 DocletArtifact (org.apache.maven.plugin.javadoc.options.DocletArtifact)6 JavadocPathArtifact (org.apache.maven.plugin.javadoc.options.JavadocPathArtifact)6 ResourcesArtifact (org.apache.maven.plugin.javadoc.options.ResourcesArtifact)6 TagletArtifact (org.apache.maven.plugin.javadoc.options.TagletArtifact)6 FileNotFoundException (java.io.FileNotFoundException)5 Writer (java.io.Writer)5 LinkedHashSet (java.util.LinkedHashSet)5 SiteRendererSink (org.apache.maven.doxia.siterenderer.sink.SiteRendererSink)5 OutputStreamWriter (java.io.OutputStreamWriter)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3