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 2023/02/11 15:06:21 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 cf11ede313ce780e7283edeb0a445936163bc5dc
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       | 53 +++++++++++++++++++++-
 6 files changed, 153 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 3ccc898..ae575d9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -112,6 +112,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>
@@ -132,6 +133,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 c2acae8..03c089a 100644
--- a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
+++ b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
@@ -48,6 +48,8 @@ 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.PlexusContainer;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 import org.codehaus.plexus.util.ReaderFactory;
 
 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
@@ -150,6 +152,15 @@ public abstract class AbstractMavenReport extends AbstractMojo implements MavenM
     /** The current report output directory to use */
     private File reportOutputDirectory;
 
+    /**
+     * The report output format: null by default, to represent a site, but can be configured to a Doxia sink id.
+     */
+    @Parameter(property = "output.format")
+    protected String outputFormat;
+
+    @Component
+    private PlexusContainer container;
+
     /**
      * This method is called when the report generation is invoked directly as a standalone Mojo.
      *
@@ -162,6 +173,44 @@ public abstract class AbstractMavenReport extends AbstractMojo implements MavenM
             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 " + new File(outputDirectory, getOutputName() + '.' + outputFormat),
+                        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";
@@ -351,7 +400,9 @@ public abstract class AbstractMavenReport extends AbstractMojo implements MavenM
      * Actions when closing the report.
      */
     protected void closeReport() {
-        getSink().close();
+        if (getSink() != null) {
+            getSink().close();
+        }
     }
 
     /**