o Added mojo to generate reports

o Added report mojo

git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@712313 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2008-11-07 23:59:37 +00:00
parent efbc2fff9b
commit 642976c2b8
3 changed files with 350 additions and 1 deletions

View File

@ -33,7 +33,7 @@ under the License.
<name>Maven Integration Test Plugin :: Site</name>
<description>
A test plugin that requires reports for its execution.
A test plugin that works with reports.
</description>
<inceptionYear>2008</inceptionYear>
@ -47,5 +47,21 @@ under the License.
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-api</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>doxia</groupId>
<artifactId>doxia-sink-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-sink-api</artifactId>
<version>1.0-alpha-11</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,132 @@
package org.apache.maven.plugin.coreit;
/*
* 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 org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.reporting.MavenReport;
import org.codehaus.doxia.sink.Sink;
import java.io.File;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Locale;
/**
* Generates the available/configured reports.
*
* @goal generate
* @phase site
* @requiresReports true
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class GenerateMojo
extends AbstractMojo
{
/**
* The path to the output directory of the site.
*
* @parameter default-value="${project.reporting.outputDirectory}"
*/
private File outputDirectory;
/**
* The language for the reports.
*
* @parameter default-value="en"
*/
private String language = "en";
/**
* A flag whether to ignore errors from reports and continue the generation.
*
* @parameter default-value="false"
*/
private boolean ignoreErrors;
/**
* The reports configured for the current build.
*
* @parameter expression="${reports}"
* @required
* @readonly
*/
private List reports;
/**
* Runs this mojo.
*
* @throws MojoExecutionException If the output file could not be created.
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
getLog().info( "[MAVEN-CORE-IT-LOG] Using output directory " + outputDirectory );
Locale locale = new Locale( language );
getLog().info( "[MAVEN-CORE-IT-LOG] Using locale " + locale );
InvocationHandler handler = new InvocationHandler()
{
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
return null;
}
};
Sink sink = (Sink) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[] { Sink.class }, handler );
for ( int i = 0; i < reports.size(); i++ )
{
MavenReport report = (MavenReport) reports.get( i );
if ( report.canGenerateReport() )
{
getLog().info( "[MAVEN-CORE-IT-LOG] Generating report " + report );
try
{
report.setReportOutputDirectory( outputDirectory );
report.generate( sink, locale );
}
catch ( Throwable e )
{
getLog().warn( "[MAVEN-CORE-IT-LOG] " + e, e );
if ( !ignoreErrors )
{
throw new MojoExecutionException( "Failed to generate report " + report, e );
}
}
}
else
{
getLog().info( "[MAVEN-CORE-IT-LOG] Skipping report " + report );
}
}
}
}

View File

@ -0,0 +1,201 @@
package org.apache.maven.plugin.coreit;
/*
* 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 org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.doxia.sink.Sink;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Properties;
/**
* Creates a properties file in the site output directory.
*
* @goal info
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class InfoReport
extends AbstractMojo
implements MavenReport
{
/**
* The base directory of the current Maven project.
*
* @parameter default-value="${basedir}"
* @required
* @readonly
*/
private File basedir;
/**
* The path to the properties file, relative to the output directory of the site. The keys
* <code>locale.language</code>, <code>locale.country</code> and <code>locale.variant</code> indicate the report's
* locale.
*
* @parameter default-value="info.properties"
*/
private String infoFile = "info.properties";
/**
* The path to the output directory of the site.
*
* @parameter default-value="${project.reporting.outputDirectory}"
*/
private File outputDirectory;
/**
* The locale for the report.
*/
private Locale locale;
/**
* Runs this mojo.
*
* @throws MojoExecutionException If the output file could not be created.
* @throws MojoFailureException If the output file has not been set.
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + infoFile );
if ( infoFile == null || infoFile.length() <= 0 )
{
throw new MojoFailureException( "Path name for output file has not been specified" );
}
File outputFile = new File( outputDirectory, infoFile );
if ( !outputFile.isAbsolute() )
{
outputFile = new File( new File( basedir, outputDirectory.getPath() ), infoFile ).getAbsoluteFile();
}
Properties props = new Properties();
props.setProperty( "site.output.directory", outputDirectory.getPath() );
if ( locale != null )
{
props.setProperty( "locale.language", locale.getLanguage() );
props.setProperty( "locale.country", locale.getCountry() );
props.setProperty( "locale.variant", locale.getVariant() );
}
getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
OutputStream out = null;
try
{
outputFile.getParentFile().mkdirs();
out = new FileOutputStream( outputFile );
props.store( out, "MAVEN-CORE-IT-LOG" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
}
finally
{
if ( out != null )
{
try
{
out.close();
}
catch ( IOException e )
{
// just ignore
}
}
}
getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
}
/**
* Runs this report.
*
* @throws MavenReportException If the report could not be created.
*/
public void generate( Sink sink, Locale locale )
throws MavenReportException
{
this.locale = locale;
try
{
execute();
}
catch ( Exception e )
{
throw new MavenReportException( "Report could not be created", e );
}
}
public String getOutputName()
{
return "info";
}
public String getCategoryName()
{
return "Project Reports";
}
public String getName( Locale locale )
{
return "name";
}
public String getDescription( Locale locale )
{
return "description";
}
public void setReportOutputDirectory( File outputDirectory )
{
this.outputDirectory = outputDirectory;
}
public File getReportOutputDirectory()
{
return outputDirectory;
}
public boolean isExternalReport()
{
return true;
}
public boolean canGenerateReport()
{
return true;
}
}