From 642976c2b832512599348d4a1366672e652fdbe6 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 7 Nov 2008 23:59:37 +0000 Subject: [PATCH] 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 --- .../maven-it-plugin-site/pom.xml | 18 +- .../maven/plugin/coreit/GenerateMojo.java | 132 ++++++++++++ .../maven/plugin/coreit/InfoReport.java | 201 ++++++++++++++++++ 3 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/GenerateMojo.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/InfoReport.java diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-site/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-site/pom.xml index e61e575345..badc63af4c 100644 --- a/its/core-it-support/core-it-plugins/maven-it-plugin-site/pom.xml +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-site/pom.xml @@ -33,7 +33,7 @@ under the License. Maven Integration Test Plugin :: Site - A test plugin that requires reports for its execution. + A test plugin that works with reports. 2008 @@ -47,5 +47,21 @@ under the License. maven-plugin-api 2.0 + + org.apache.maven.reporting + maven-reporting-api + 2.0 + + + doxia + doxia-sink-api + + + + + org.apache.maven.doxia + doxia-sink-api + 1.0-alpha-11 + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/GenerateMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/GenerateMojo.java new file mode 100644 index 0000000000..f6e7878630 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/GenerateMojo.java @@ -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 ); + } + } + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/InfoReport.java b/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/InfoReport.java new file mode 100644 index 0000000000..c89052a422 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-site/src/main/java/org/apache/maven/plugin/coreit/InfoReport.java @@ -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 + * locale.language, locale.country and locale.variant 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; + } + +}