You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2022/11/27 18:42:01 UTC
[maven-reporting-impl] 01/01: [MSHARED-1168] support markup output
This is an automated email from the ASF dual-hosted git repository.
hboutemy pushed a commit to branch MSHARED-1168
in repository https://gitbox.apache.org/repos/asf/maven-reporting-impl.git
commit e6bf452127a5029db0d220066356eaff0537fbe3
Author: Hervé Boutemy <hb...@apache.org>
AuthorDate: Sun Nov 27 19:41:43 2022 +0100
[MSHARED-1168] support markup output
---
pom.xml | 12 +++
.../maven/reporting/its/custom/ExternalReport.java | 2 +
.../use-as-direct-mojo-markup/invoker.properties | 20 +++++
src/it/use-as-direct-mojo-markup/pom.xml | 33 +++++++++
src/it/use-as-direct-mojo-markup/verify.groovy | 34 +++++++++
.../maven/reporting/AbstractMavenReport.java | 85 +++++++++++++++++++++-
6 files changed, 184 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index a3d0fec..0c8d20f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -111,6 +111,7 @@
<artifactId>doxia-sink-api</artifactId>
<version>${doxiaVersion}</version>
</dependency>
+ <!-- Doxia site rendering -->
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-decoration-model</artifactId>
@@ -131,6 +132,17 @@
<artifactId>doxia-site-renderer</artifactId>
<version>${doxiaSitetoolsVersion}</version>
</dependency>
+ <!-- Doxia markup rendering -->
+ <dependency>
+ <groupId>org.apache.maven.doxia</groupId>
+ <artifactId>doxia-module-apt</artifactId>
+ <version>${doxiaVersion}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.doxia</groupId>
+ <artifactId>doxia-module-xdoc</artifactId>
+ <version>${doxiaVersion}</version>
+ </dependency>
<!-- misc -->
<dependency>
diff --git a/src/it/setup-reporting-plugin/src/main/java/org/apache/maven/reporting/its/custom/ExternalReport.java b/src/it/setup-reporting-plugin/src/main/java/org/apache/maven/reporting/its/custom/ExternalReport.java
index 5ded0e7..2fbb6a9 100644
--- a/src/it/setup-reporting-plugin/src/main/java/org/apache/maven/reporting/its/custom/ExternalReport.java
+++ b/src/it/setup-reporting-plugin/src/main/java/org/apache/maven/reporting/its/custom/ExternalReport.java
@@ -85,6 +85,8 @@ public class ExternalReport
private void executeExternalTool( String destination )
throws IOException
{
+ getLog().info( "Running external tool to " + destination );
+
// demo implementation, to be replaced with effective tool
File dest = new File( destination );
diff --git a/src/it/use-as-direct-mojo-markup/invoker.properties b/src/it/use-as-direct-mojo-markup/invoker.properties
new file mode 100644
index 0000000..3381aa4
--- /dev/null
+++ b/src/it/use-as-direct-mojo-markup/invoker.properties
@@ -0,0 +1,20 @@
+# 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.
+
+invoker.goals.1 = org.apache.maven.reporting.its:custom-reporting-plugin:1.0-SNAPSHOT:custom -Doutput.format=xdoc
+invoker.goals.2 = org.apache.maven.reporting.its:custom-reporting-plugin:1.0-SNAPSHOT:custom-renderer -Doutput.format=apt
+invoker.goals.3 = org.apache.maven.reporting.its:custom-reporting-plugin:1.0-SNAPSHOT:external -Doutput.format=anything
diff --git a/src/it/use-as-direct-mojo-markup/pom.xml b/src/it/use-as-direct-mojo-markup/pom.xml
new file mode 100644
index 0000000..09dab3a
--- /dev/null
+++ b/src/it/use-as-direct-mojo-markup/pom.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.reporting.its</groupId>
+ <artifactId>use-as-direct-mojo</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>Use report as direct mojo</name>
+ <description>Use report directly as mojo: "mvn my-plugin:my-report"</description>
+</project>
diff --git a/src/it/use-as-direct-mojo-markup/verify.groovy b/src/it/use-as-direct-mojo-markup/verify.groovy
new file mode 100644
index 0000000..5773ff2
--- /dev/null
+++ b/src/it/use-as-direct-mojo-markup/verify.groovy
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+File site = new File( basedir, 'target/site/' )
+
+File f = new File( site, 'custom-report.xdoc' );
+assert f.exists();
+assert f.text.contains( 'Custom Maven Report content.' );
+
+f = new File( site, 'custom-report-with-renderer.apt' );
+assert f.exists();
+assert f.text.contains( 'Custom Maven Report with Renderer content.' );
+
+f = new File( site, 'external/report.html' );
+assert f.exists();
+assert f.text.contains( '<h1>External Report</h1>' );
+
+return true;
\ No newline at end of file
diff --git a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
index edc8b68..e181454 100644
--- a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
+++ b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
@@ -38,6 +38,12 @@ import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.WriterFactory;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.context.Context;
+import org.codehaus.plexus.context.ContextException;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.ReaderFactory;
import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
@@ -70,7 +76,7 @@ import java.util.Map;
*/
public abstract class AbstractMavenReport
extends AbstractMojo
- implements MavenMultiPageReport
+ implements MavenMultiPageReport, Contextualizable
{
/**
* The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
@@ -148,6 +154,14 @@ public abstract class AbstractMavenReport
/** The current report output directory to use */
private File reportOutputDirectory;
+ /**
+ * The output format: null by default, to represent a site, but can be configured to a Doxia sink.
+ */
+ @Parameter( property = "output.format" )
+ protected String outputFormat;
+
+ private PlexusContainer container;
+
/**
* This method is called when the report generation is invoked directly as a standalone Mojo.
*
@@ -163,6 +177,61 @@ public abstract class AbstractMavenReport
return;
}
+ if ( outputFormat != null )
+ {
+ reportToMarkup();
+ }
+ else
+ {
+ reportToSite();
+ }
+ }
+
+ private void reportToMarkup()
+ throws MojoExecutionException
+ {
+ getLog().info( "rendering to " + outputFormat + " markup" );
+
+ if ( !isExternalReport() )
+ {
+ try
+ {
+ sinkFactory = container.lookup( SinkFactory.class, outputFormat );
+ sink = sinkFactory.createSink( outputDirectory, getOutputName() + '.' + outputFormat );
+ }
+ catch ( ComponentLookupException cle )
+ {
+ throw new MojoExecutionException(
+ "Cannot find SinkFactory for Doxia output format: " + outputFormat, cle );
+ }
+ catch ( IOException ioe )
+ {
+ throw new MojoExecutionException( "Cannot create sink to " + outputDirectory, ioe );
+ }
+ }
+
+ try
+ {
+ Locale locale = Locale.getDefault();
+ generate( sink, sinkFactory, locale );
+ }
+ catch ( MavenReportException mre )
+ {
+ throw new MojoExecutionException(
+ "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", mre );
+ }
+ finally
+ {
+ if ( sink != null )
+ {
+ sink.close();
+ }
+ }
+ }
+
+ private void reportToSite()
+ throws MojoExecutionException
+ {
File outputDirectory = new File( getOutputDirectory() );
String filename = getOutputName() + ".html";
@@ -383,7 +452,10 @@ public abstract class AbstractMavenReport
*/
protected void closeReport()
{
- getSink().close();
+ if ( getSink() != null )
+ {
+ getSink().close();
+ }
}
/**
@@ -426,4 +498,13 @@ public abstract class AbstractMavenReport
*/
protected abstract void executeReport( Locale locale )
throws MavenReportException;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void contextualize( Context context )
+ throws ContextException
+ {
+ container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
+ }
}