Search in sources :

Example 1 with MavenReport

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

the class PdfMojo method generateMavenReports.

/**
     * Generate all Maven reports defined in <code>${project.reporting}</code> part
     * only if <code>generateReports</code> is enabled.
     *
     * @param locale not null
     * @throws MojoExecutionException if any
     * @throws IOException if any
     * @since 1.1
     */
private void generateMavenReports(Locale locale) throws MojoExecutionException, IOException {
    if (!includeReports) {
        getLog().info("Skipped report generation.");
        return;
    }
    if (project.getReporting() == null) {
        getLog().info("No report was specified.");
        return;
    }
    for (final ReportPlugin reportPlugin : project.getReporting().getPlugins()) {
        final PluginDescriptor pluginDescriptor = getPluginDescriptor(reportPlugin);
        if (pluginDescriptor != null) {
            List<String> goals = new ArrayList<String>(8);
            for (final ReportSet reportSet : reportPlugin.getReportSets()) {
                for (String goal : reportSet.getReports()) {
                    goals.add(goal);
                }
            }
            List mojoDescriptors = pluginDescriptor.getMojos();
            for (Object mojoDescriptor1 : mojoDescriptors) {
                final MojoDescriptor mojoDescriptor = (MojoDescriptor) mojoDescriptor1;
                if (goals.isEmpty() || (!goals.isEmpty() && goals.contains(mojoDescriptor.getGoal()))) {
                    MavenReport report = getMavenReport(mojoDescriptor);
                    generateMavenReport(report, mojoDescriptor.getPluginDescriptor().getPluginArtifact(), locale);
                }
            }
        }
    }
    // generate project-info report
    if (!getGeneratedMavenReports(locale).isEmpty()) {
        File outDir = new File(getGeneratedSiteDirectoryTmp(), "xdoc");
        if (!locale.getLanguage().equals(defaultLocale.getLanguage())) {
            outDir = new File(new File(getGeneratedSiteDirectoryTmp(), locale.getLanguage()), "xdoc");
        }
        outDir.mkdirs();
        File piReport = new File(outDir, "project-info.xml");
        StringWriter sw = new StringWriter();
        PdfSink sink = new PdfSink(sw);
        ProjectInfoRenderer r = new ProjectInfoRenderer(sink, getGeneratedMavenReports(locale), i18n, locale);
        r.render();
        writeGeneratedReport(sw.toString(), piReport);
    }
    // copy generated site
    copySiteDir(getGeneratedSiteDirectoryTmp(), getSiteDirectoryTmp());
    copySiteDir(generatedSiteDirectory, getSiteDirectoryTmp());
}
Also used : MavenReport(org.apache.maven.reporting.MavenReport) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) ArrayList(java.util.ArrayList) ReportSet(org.apache.maven.model.ReportSet) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ReportPlugin(org.apache.maven.model.ReportPlugin) StringWriter(java.io.StringWriter) List(java.util.List) ArrayList(java.util.ArrayList) MailingList(org.apache.maven.model.MailingList) File(java.io.File)

Example 2 with MavenReport

use of org.apache.maven.reporting.MavenReport 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 3 with MavenReport

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

the class CheckstyleReportTest method generateReport.

private File generateReport(String pluginXml) throws Exception {
    File pluginXmlFile = new File(getBasedir(), "src/test/plugin-configs/" + pluginXml);
    ResourceBundle bundle = ResourceBundle.getBundle("checkstyle-report", Locale.getDefault(), this.getClassLoader());
    CheckstyleReport mojo = (CheckstyleReport) lookupMojo("checkstyle", pluginXmlFile);
    assertNotNull("Mojo found.", mojo);
    PluginDescriptor descriptorStub = new PluginDescriptor();
    descriptorStub.setGroupId("org.apache.maven.plugins");
    descriptorStub.setArtifactId("maven-checkstyle-plugin");
    setVariableValueToObject(mojo, "plugin", descriptorStub);
    mojo.execute();
    File outputFile = (File) getVariableValueFromObject(mojo, "outputFile");
    assertNotNull("Test output file", outputFile);
    assertTrue("Test output file exists", outputFile.exists());
    String cacheFile = (String) getVariableValueFromObject(mojo, "cacheFile");
    if (cacheFile != null) {
        assertTrue("Test cache file exists", new File(cacheFile).exists());
    }
    MavenReport reportMojo = mojo;
    File outputDir = reportMojo.getReportOutputDirectory();
    Boolean rss = (Boolean) getVariableValueFromObject(mojo, "enableRSS");
    if (rss) {
        File rssFile = new File(outputDir, "checkstyle.rss");
        assertTrue("Test rss file exists", rssFile.exists());
    }
    File useFile = (File) getVariableValueFromObject(mojo, "useFile");
    if (useFile != null) {
        assertTrue("Test useFile exists", useFile.exists());
    }
    String filename = reportMojo.getOutputName() + ".html";
    File outputHtml = new File(outputDir, filename);
    renderer(mojo, outputHtml);
    assertTrue(outputHtml.getAbsolutePath() + " not generated!", outputHtml.exists());
    assertTrue(outputHtml.getAbsolutePath() + " is empty!", outputHtml.length() > 0);
    String htmlString = FileUtils.fileRead(outputHtml);
    boolean searchHeaderFound = (htmlString.indexOf("<h2>" + bundle.getString("report.checkstyle.rules")) > 0);
    Boolean rules = (Boolean) getVariableValueFromObject(mojo, "enableRulesSummary");
    if (rules) {
        assertTrue("Test for Rules Summary", searchHeaderFound);
    } else {
        assertFalse("Test for Rules Summary", searchHeaderFound);
    }
    searchHeaderFound = (htmlString.indexOf("<h2>" + bundle.getString("report.checkstyle.summary")) > 0);
    Boolean severity = (Boolean) getVariableValueFromObject(mojo, "enableSeveritySummary");
    if (severity) {
        assertTrue("Test for Severity Summary", searchHeaderFound);
    } else {
        assertFalse("Test for Severity Summary", searchHeaderFound);
    }
    searchHeaderFound = (htmlString.indexOf("<h2>" + bundle.getString("report.checkstyle.files")) > 0);
    Boolean files = (Boolean) getVariableValueFromObject(mojo, "enableFilesSummary");
    if (files) {
        assertTrue("Test for Files Summary", searchHeaderFound);
    } else {
        assertFalse("Test for Files Summary", searchHeaderFound);
    }
    return outputHtml;
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) MavenReport(org.apache.maven.reporting.MavenReport) CheckstyleReport(org.apache.maven.plugins.checkstyle.CheckstyleReport) ResourceBundle(java.util.ResourceBundle) File(java.io.File)

Example 4 with MavenReport

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

the class AbstractSiteRenderingMojo method locateDocuments.

/**
     * Locate every document to be rendered for given locale:<ul>
     * <li>handwritten content, ie Doxia files,</li>
     * <li>reports,</li>
     * <li>"Project Information" and "Project Reports" category summaries.</li>
     * </ul>
     * @see CategorySummaryDocumentRenderer
     */
protected Map<String, DocumentRenderer> locateDocuments(SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale) throws IOException, RendererException {
    Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context);
    Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);
    // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
    Map<String, List<MavenReport>> categories = categoriseReports(reportsByOutputName.values());
    siteTool.populateReportsMenu(context.getDecoration(), locale, categories);
    populateReportItems(context.getDecoration(), locale, reportsByOutputName);
    if (categories.containsKey(MavenReport.CATEGORY_PROJECT_INFORMATION) && generateProjectInfo) {
        // add "Project Information" category summary document
        List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_INFORMATION);
        RenderingContext renderingContext = new RenderingContext(siteDirectory, "project-info.html");
        String title = i18n.getString("site-plugin", locale, "report.information.title");
        String desc1 = i18n.getString("site-plugin", locale, "report.information.description1");
        String desc2 = i18n.getString("site-plugin", locale, "report.information.description2");
        DocumentRenderer renderer = new CategorySummaryDocumentRenderer(renderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
        if (!documents.containsKey(renderer.getOutputName())) {
            documents.put(renderer.getOutputName(), renderer);
        } else {
            getLog().info("Category summary '" + renderer.getOutputName() + "' skipped; already exists");
        }
    }
    if (categories.containsKey(MavenReport.CATEGORY_PROJECT_REPORTS)) {
        // add "Project Reports" category summary document
        List<MavenReport> categoryReports = categories.get(MavenReport.CATEGORY_PROJECT_REPORTS);
        RenderingContext renderingContext = new RenderingContext(siteDirectory, "project-reports.html");
        String title = i18n.getString("site-plugin", locale, "report.project.title");
        String desc1 = i18n.getString("site-plugin", locale, "report.project.description1");
        String desc2 = i18n.getString("site-plugin", locale, "report.project.description2");
        DocumentRenderer renderer = new CategorySummaryDocumentRenderer(renderingContext, title, desc1, desc2, i18n, categoryReports, getLog());
        if (!documents.containsKey(renderer.getOutputName())) {
            documents.put(renderer.getOutputName(), renderer);
        } else {
            getLog().info("Category summary '" + renderer.getOutputName() + "' skipped; already exists");
        }
    }
    return documents;
}
Also used : MavenReport(org.apache.maven.reporting.MavenReport) RenderingContext(org.apache.maven.doxia.siterenderer.RenderingContext) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) ArrayList(java.util.ArrayList) List(java.util.List) DocumentRenderer(org.apache.maven.doxia.siterenderer.DocumentRenderer)

Example 5 with MavenReport

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

the class AbstractSiteRenderingMojo method categoriseReports.

/**
     * Go through the collection of reports and put each report into a list for the appropriate category. The list is
     * put into a map keyed by the name of the category.
     *
     * @param reports A Collection of MavenReports
     * @return A map keyed category having the report itself as value
     */
protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenReport> reports) {
    Map<String, List<MavenReport>> categories = new LinkedHashMap<String, List<MavenReport>>();
    for (MavenReport report : reports) {
        List<MavenReport> categoryReports = categories.get(report.getCategoryName());
        if (categoryReports == null) {
            categoryReports = new ArrayList<MavenReport>();
            categories.put(report.getCategoryName(), categoryReports);
        }
        categoryReports.add(report);
    }
    return categories;
}
Also used : MavenReport(org.apache.maven.reporting.MavenReport) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MavenReport (org.apache.maven.reporting.MavenReport)12 ArrayList (java.util.ArrayList)7 File (java.io.File)5 MavenReportExecution (org.apache.maven.reporting.exec.MavenReportExecution)4 List (java.util.List)3 DocumentRenderer (org.apache.maven.doxia.siterenderer.DocumentRenderer)3 SiteRenderingContext (org.apache.maven.doxia.siterenderer.SiteRenderingContext)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 StringWriter (java.io.StringWriter)2 LinkedHashMap (java.util.LinkedHashMap)2 RenderingContext (org.apache.maven.doxia.siterenderer.RenderingContext)2 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)2 MavenReportExecutor (org.apache.maven.reporting.exec.MavenReportExecutor)2 MavenReportExecutorRequest (org.apache.maven.reporting.exec.MavenReportExecutorRequest)2 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)2 IOException (java.io.IOException)1 Locale (java.util.Locale)1 ResourceBundle (java.util.ResourceBundle)1 DocumentTOCItem (org.apache.maven.doxia.document.DocumentTOCItem)1 IndexingSink (org.apache.maven.doxia.index.IndexingSink)1