
org.apache.maven.plugins.site.AbstractSiteRenderingMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-site-plugin Show documentation
Show all versions of maven-site-plugin Show documentation
The Maven Site Plugin is a plugin that generates a site for the current project.
The newest version!
package org.apache.maven.plugins.site;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.doxia.sink.render.RenderingContext;
import org.apache.maven.doxia.site.decoration.DecorationModel;
import org.apache.maven.doxia.site.decoration.Menu;
import org.apache.maven.doxia.site.decoration.MenuItem;
import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
import org.apache.maven.doxia.siterenderer.DocumentRenderer;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.doxia.siterenderer.RendererException;
import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
import org.apache.maven.doxia.tools.SiteToolException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.reporting.exec.MavenReportExecution;
import org.apache.maven.reporting.exec.MavenReportExecutor;
import org.apache.maven.reporting.exec.MavenReportExecutorRequest;
import org.apache.maven.reporting.exec.ReportPlugin;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.configuration.MergePolicy;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfigurationUtils;
import org.codehaus.plexus.configuration.PlexusConfigurationUtils.MergePolicyType;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
/**
* Base class for site rendering mojos.
*
* @author Brett Porter
* @version $Id: AbstractSiteRenderingMojo.java 25140 2011-09-23 19:07:57Z edalquist $
*/
public abstract class AbstractSiteRenderingMojo
extends AbstractSiteMojo implements Contextualizable
{
/**
* Module type exclusion mappings
* ex: fml -> **/*-m1.fml
(excludes fml files ending in '-m1.fml' recursively)
*
* The configuration looks like this:
*
* <moduleExcludes>
* <moduleType>filename1.ext,**/*sample.ext</moduleType>
* <!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. -->
* <!-- The value is a comma separated list of -->
* <!-- filenames or fileset patterns. -->
* <!-- Here's an example: -->
* <xdoc>changes.xml,navigation.xml</xdoc>
* </moduleExcludes>
*
*
* @parameter
*/
private Map moduleExcludes;
/**
* The component for assembling inheritance.
*
* @component
*/
private DecorationModelInheritanceAssembler assembler;
/**
* Remote repositories used for the project.
*
* @todo this is used for site descriptor resolution - it should relate to the actual project but for some reason they are not always filled in
* @parameter default-value="${project.remoteArtifactRepositories}"
* @readonly
*/
private List repositories;
/**
* Directory containing the template page.
*
* @parameter expression="${templateDirectory}" default-value="src/site"
* @deprecated use templateFile or skinning instead
*/
private File templateDirectory;
/**
* Default template page.
*
* @parameter expression="${template}"
* @deprecated use templateFile or skinning instead
*/
private String template;
/**
* The location of a Velocity template file to use. When used, skins and the default templates, CSS and images
* are disabled. It is highly recommended that you package this as a skin instead.
*
* @parameter expression="${templateFile}"
* @since 2.0-beta-5
*/
private File templateFile;
/**
* The template properties for rendering the site.
*
* @parameter
*/
private Map attributes;
/**
* Site renderer.
*
* @component
*/
protected Renderer siteRenderer;
/**
* Reports (Maven 2).
*
* @parameter expression="${reports}"
* @required
* @readonly
*/
protected List reports;
/**
* Alternative directory for xdoc source, useful for m1 to m2 migration
*
* @parameter default-value="${basedir}/xdocs"
* @deprecated use the standard m2 directory layout
*/
private File xdocDirectory;
/**
* Directory containing generated documentation.
* This is used to pick up other source docs that might have been generated at build time.
*
* @parameter alias="workingDirectory" default-value="${project.build.directory}/generated-site"
*
* @todo should we deprecate in favour of reports?
*/
protected File generatedSiteDirectory;
/**
* The current Maven session.
*
* @parameter expression="${session}"
* @required
* @readonly
*/
protected MavenSession mavenSession;
/**
* Report plugins (Maven 3).
*
* @parameter
* @since 3.0-beta-1
*/
private ReportPlugin[] reportPlugins;
/**
* The default merge policy for handling duplicate plugins
*
* @parameter expression="${defaultMergePolicy}"
*/
protected MergePolicy defaultMergePolicy = MergePolicy.BOTH;
private PlexusContainer container;
/**
* Make links in the site descriptor relative to the project URL.
* By default, any absolute links that appear in the site descriptor,
* e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
*
* Links will not be changed if this is set to false, or if the project has no URL defined.
*
* @parameter expression="${relativizeDecorationLinks}" default-value="true"
*
* @since 2.3
*/
private boolean relativizeDecorationLinks;
/**
* Whether to generate the summary page for project reports: project-info.html.
*
* @parameter expression="${generateProjectInfo}" default-value="true"
*
* @since 2.3
*/
private boolean generateProjectInfo;
/** {@inheritDoc} */
public void contextualize( Context context )
throws ContextException
{
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
private String configToString(PlexusConfiguration configuration) {
if (configuration == null) {
return "NO CONFIGURATION";
}
final StringBuilder buf = new StringBuilder().append( '<' ).append( configuration.getName() );
for ( final String a : configuration.getAttributeNames() )
{
buf.append( ' ' ).append( a ).append( "=\"" ).append( configuration.getAttribute( a ) ).append( '"' );
}
if ( configuration.getChildCount() > 0 )
{
buf.append( '>' );
for ( final PlexusConfiguration c : configuration.getChildren() )
{
buf.append( c );
}
buf.append( "" ).append( configuration.getName() ).append( '>' );
}
else if ( null != configuration.getValue() )
{
buf.append( '>' ).append( configuration.getValue() ).append( "" ).append( configuration.getName() ).append( '>' );
}
else
{
buf.append( "/>" );
}
return buf.append( '\n' ).toString();
}
protected List getReports()
throws MojoExecutionException
{
if ( isMaven3OrMore() )
{
ReportPlugin[] filteredReportPlugins = filterReportPlugins(reportPlugins);
MavenReportExecutorRequest mavenReportExecutorRequest = new MavenReportExecutorRequest();
mavenReportExecutorRequest.setLocalRepository( localRepository );
mavenReportExecutorRequest.setMavenSession( mavenSession );
mavenReportExecutorRequest.setProject( project );
mavenReportExecutorRequest.setReportPlugins( filteredReportPlugins );
MavenReportExecutor mavenReportExecutor;
try
{
mavenReportExecutor = (MavenReportExecutor) container.lookup( MavenReportExecutor.class.getName() );
}
catch ( ComponentLookupException e )
{
throw new MojoExecutionException( "could not get MavenReportExecutor component", e );
}
return mavenReportExecutor.buildMavenReports( mavenReportExecutorRequest );
}
List reportExecutions = new ArrayList( reports.size() );
for ( MavenReport report : reports )
{
if ( report.canGenerateReport() )
{
reportExecutions.add( new MavenReportExecution( report ) );
}
}
return reportExecutions;
}
protected ReportPlugin[] filterReportPlugins(ReportPlugin[] reportPlugins) {
final Log log = this.getLog();
//Map of report plugins (to handle merging), linked because order is important!
Map reportPluginFilter = new LinkedHashMap();
for (int pluginIndex = 0; pluginIndex < reportPlugins.length; pluginIndex++) {
ReportPlugin reportPlugin = reportPlugins[pluginIndex];
String pluginKey = reportPlugin.getGroupId() + ":" + reportPlugin.getArtifactId();
if (!reportPluginFilter.containsKey(pluginKey)) {
//First time this plugin has been seen, just add it to the map
reportPluginFilter.put(pluginKey, reportPlugin);
}
else {
final PlexusConfiguration newConfiguration = reportPlugin.getConfiguration();
//Not the first time we've seen this plugin, figure out what to do
final MergePolicy duplicateReportPolicy = MergePolicyType.SELF_MERGE_POLICY.getMergePolicy(newConfiguration, defaultMergePolicy);
switch (duplicateReportPolicy) {
case FIRST: {
//Use first, nothing to do
break;
}
case BOTH: {
//TODO is there a better way? What happens if the same key exists later with a MERGE option?
reportPluginFilter.put(pluginKey + pluginIndex, reportPlugin);
}
case LAST: {
//Use last, replace the existing plugin
reportPluginFilter.put(pluginKey, reportPlugin);
break;
}
case MERGE: {
//Merge the plugin configurations
final ReportPlugin existingReportPlugin = reportPluginFilter.get(pluginKey);
if (reportPlugin.getVersion() != null) {
existingReportPlugin.setVersion(reportPlugin.getVersion());
}
final PlexusConfiguration existingConfiguration = existingReportPlugin.getConfiguration();
final PlexusConfiguration mergedConfiguration = PlexusConfigurationUtils.merge(existingConfiguration, newConfiguration);
existingReportPlugin.setConfiguration(mergedConfiguration);
break;
}
default: {
throw new IllegalArgumentException("Unsupported " + MergePolicy.class.getName() + ": " + duplicateReportPolicy);
}
}
}
}
ReportPlugin[] filteredReportPlugins = reportPluginFilter.values().toArray(new ReportPlugin[reportPluginFilter.size()]);
return filteredReportPlugins;
}
protected SiteRenderingContext createSiteRenderingContext( Locale locale )
throws MojoExecutionException, IOException, MojoFailureException
{
if ( attributes == null )
{
attributes = new HashMap();
}
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy