This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch maven-buildinfo-plugin
in repository https://gitbox.apache.org/repos/asf/maven-studies.git


The following commit(s) were added to refs/heads/maven-buildinfo-plugin by this 
push:
     new 9f0150b  refactoring: extract buildinfo generation from Mojo
9f0150b is described below

commit 9f0150bcba209227c6e1308f6cd39b4206bcc6da
Author: Hervé Boutemy <hbout...@apache.org>
AuthorDate: Sun Feb 23 20:58:51 2020 +0100

    refactoring: extract buildinfo generation from Mojo
---
 .../maven/plugins/buildinfo/BuildInfoWriter.java   | 155 +++++++++++++++++++++
 .../apache/maven/plugins/buildinfo/SaveMojo.java   | 133 ++----------------
 2 files changed, 165 insertions(+), 123 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/plugins/buildinfo/BuildInfoWriter.java 
b/src/main/java/org/apache/maven/plugins/buildinfo/BuildInfoWriter.java
new file mode 100644
index 0000000..3ab6d47
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/buildinfo/BuildInfoWriter.java
@@ -0,0 +1,155 @@
+package org.apache.maven.plugins.buildinfo;
+
+/*
+ * 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.PrintWriter;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Buildinfo content writer.
+ */
+public class BuildInfoWriter
+{
+    private final Log log;
+    private final PrintWriter p;
+    private final boolean mono;
+    private int projectCount = -1;
+
+    BuildInfoWriter( Log log, PrintWriter p, boolean mono )
+    {
+        this.log = log;
+        this.p = p;
+        this.mono = mono;
+    }
+
+    public void printHeader( MavenProject project )
+    {
+        p.println( "# https://reproducible-builds.org/docs/jvm/"; );
+        p.println( "buildinfo.version=1.0-SNAPSHOT" );
+        p.println();
+        p.println( "name=" + project.getName() );
+        p.println( "group-id=" + project.getGroupId() );
+        p.println( "artifact-id=" + project.getArtifactId() );
+        p.println( "version=" + project.getVersion() );
+        p.println();
+        printSourceInformation( project );
+        p.println();
+        p.println( "# build instructions" );
+        p.println( "build-tool=mvn" );
+        //p.println( "# optional build setup url, as plugin parameter" );
+        p.println();
+        p.println( "# effective build environment information" );
+        p.println( "java.version=" + System.getProperty( "java.version" ) );
+        p.println( "java.vendor=" + System.getProperty( "java.vendor" ) );
+        p.println( "os.name=" + System.getProperty( "os.name" ) );
+        p.println();
+        p.println( "# Maven rebuild instructions and effective environment" );
+        //p.println( "mvn.rebuild-args=" + rebuildArgs );
+        p.println( "mvn.version=" + MavenVersion.createMavenVersionString() );
+        if ( ( project.getPrerequisites() != null ) && ( 
project.getPrerequisites().getMaven() != null ) )
+        {
+            // TODO wrong algorithm, should reuse algorithm written in 
versions-maven-plugin
+            p.println( "mvn.minimum.version=" + 
project.getPrerequisites().getMaven() );
+        }
+        p.println();
+        p.println( "# " + ( mono ? "" : "aggregated " ) + "output" );
+    }
+
+    private void printSourceInformation( MavenProject project )
+    {
+        boolean sourceAvailable = false;
+        p.println( "# source information" );
+        //p.println( "# TBD source.* artifact, url should be parameters" );
+        if ( project.getScm() != null )
+        {
+            sourceAvailable = true;
+            p.println( "source.scm.uri=" + project.getScm().getConnection() );
+            p.println( "source.scm.tag=" + project.getScm().getTag() );
+            if ( project.getArtifact().isSnapshot() )
+            {
+                log.warn( "SCM source tag in buildinfo source.scm.tag=" + 
project.getScm().getTag()
+                    + " does not permit rebuilders reproducible source 
checkout" );
+                // TODO is it possible to use Scm API to get SCM version?
+            }
+        }
+        else
+        {
+            p.println( "# no scm configured in pom.xml" );
+        }
+
+        if ( !sourceAvailable )
+        {
+            log.warn( "No source information available in buildinfo for 
rebuilders..." );
+        }
+    }
+
+    public void printArtifacts( MavenProject project )
+        throws MojoExecutionException
+    {
+        if ( project.getArtifact() == null )
+        {
+            return;
+        }
+
+        String prefix = "outputs.";
+        if ( !mono )
+        {
+            // aggregated buildinfo output
+            projectCount++;
+            prefix += projectCount + ".";
+            p.println( prefix + "coordinates=" + project.getGroupId() + ':' + 
project.getArtifactId() );
+        }
+
+        int n = 0;
+        if ( project.getArtifact().getFile() != null )
+        {
+            printArtifact( prefix, n++, project.getArtifact() );
+        }
+
+        for ( Artifact attached : project.getAttachedArtifacts() )
+        {
+            if ( attached.getType().endsWith( ".asc" ) )
+            {
+                // ignore pgp signatures
+                continue;
+            }
+            if ( "javadoc".equals( attached.getClassifier() ) )
+            {
+                // TEMPORARY ignore javadoc, waiting for MJAVADOC-627 in 
m-javadoc-p 3.2.0
+                continue;
+            }
+            printArtifact( prefix, n++, attached );
+        }
+    }
+
+    private void printArtifact( String prefix, int i, Artifact artifact )
+        throws MojoExecutionException
+    {
+        File file = artifact.getFile();
+        p.println( prefix + i + ".filename=" + file.getName() );
+        p.println( prefix + i + ".length=" + file.length() );
+        p.println( prefix + i + ".checksums.sha512=" + 
DigestHelper.calculateSha512( file ) );
+    }
+}
diff --git a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java 
b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
index 16c35b0..87be6b1 100644
--- a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
@@ -20,7 +20,6 @@ package org.apache.maven.plugins.buildinfo;
  */
 
 import org.apache.commons.codec.Charsets;
-import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 
@@ -42,7 +41,7 @@ import java.util.List;
 /**
  * Save buildinfo file, as specified in
  * <a href="https://reproducible-builds.org/docs/jvm/";>Reproducible Builds for 
the JVM</a>
- * for mono-module build, and exended for multi-module build.
+ * for mono-module build, and extended for multi-module build.
  */
 @Mojo( name = "save", defaultPhase = LifecyclePhase.VERIFY )
 public class SaveMojo
@@ -85,12 +84,14 @@ public class SaveMojo
     @Component
     private MavenProjectHelper projectHelper;
 
-    public void execute() throws MojoExecutionException
+    public void execute()
+        throws MojoExecutionException
     {
         boolean mono = reactorProjects.size() == 1;
 
         if ( !mono )
         {
+            // if multi-module build, generate (aggregate) buildinfo only in 
last module
             MavenProject aggregate = reactorProjects.get( 
reactorProjects.size() - 1 );
             if  ( project != aggregate )
             {
@@ -100,6 +101,7 @@ public class SaveMojo
         }
 
         generateBuildinfo( mono );
+        getLog().info( "Saved " + ( mono ? "" : "aggregate " ) + "info on 
build to " + buildinfoFile );
 
         if ( attach )
         {
@@ -121,32 +123,22 @@ public class SaveMojo
         try ( PrintWriter p = new PrintWriter( new BufferedWriter(
                 new OutputStreamWriter( new FileOutputStream( buildinfoFile ), 
Charsets.ISO_8859_1 ) ) ) )
         {
-            printHeader( p, root );
+            BuildInfoWriter bi = new BuildInfoWriter( getLog(), p, mono );
+
+            bi.printHeader( root );
 
             // artifact(s) fingerprints
             if ( mono )
             {
-                if ( project.getArtifact() != null )
-                {
-                    p.println();
-                    p.println( "# output" );
-                    printOutput( p, project, -1 );
-                }
+                bi.printArtifacts( project );
             }
             else
             {
-                int n = 0;
                 for ( MavenProject project : reactorProjects )
                 {
-                    if ( project.getArtifact() != null )
-                    {
-                        p.println();
-                        printOutput( p, project, n++ );
-                    }
+                    bi.printArtifacts( project );
                 }
             }
-
-            getLog().info( "Saved " + ( mono ? "" : "aggregate " ) + "info on 
build to " + buildinfoFile );
         }
         catch ( IOException e )
         {
@@ -154,111 +146,6 @@ public class SaveMojo
         }
     }
 
-    private void printHeader( PrintWriter p, MavenProject project )
-    {
-        p.println( "# https://reproducible-builds.org/docs/jvm/"; );
-        p.println( "buildinfo.version=1.0-SNAPSHOT" );
-        p.println();
-        p.println( "name=" + project.getName() );
-        p.println( "group-id=" + project.getGroupId() );
-        p.println( "artifact-id=" + project.getArtifactId() );
-        p.println( "version=" + project.getVersion() );
-        p.println();
-        printSourceInformation( p, project );
-        p.println();
-        p.println( "# build instructions" );
-        p.println( "build-tool=mvn" );
-        //p.println( "# optional build setup url, as plugin parameter" );
-        p.println();
-        p.println( "# effective build environment information" );
-        p.println( "java.version=" + System.getProperty( "java.version" ) );
-        p.println( "java.vendor=" + System.getProperty( "java.vendor" ) );
-        p.println( "os.name=" + System.getProperty( "os.name" ) );
-        p.println();
-        p.println( "# Maven rebuild instructions and effective environment" );
-        //p.println( "mvn.rebuild-args=" + rebuildArgs );
-        p.println( "mvn.version=" + MavenVersion.createMavenVersionString() );
-        if ( ( project.getPrerequisites() != null ) && ( 
project.getPrerequisites().getMaven() != null ) )
-        {
-            // TODO wrong algorithm, should reuse algorithm written in 
versions-maven-plugin
-            p.println( "mvn.minimum.version=" + 
project.getPrerequisites().getMaven() );
-        }
-    }
-
-    private void printSourceInformation( PrintWriter p, MavenProject project )
-    {
-        boolean sourceAvailable = false;
-        p.println( "# source information" );
-        //p.println( "# TBD source.* artifact, url should be parameters" );
-        if ( project.getScm() != null )
-        {
-            sourceAvailable = true;
-            p.println( "source.scm.uri=" + project.getScm().getConnection() );
-            p.println( "source.scm.tag=" + project.getScm().getTag() );
-            if ( project.getArtifact().isSnapshot() )
-            {
-                getLog().warn( "SCM source tag in buildinfo source.scm.tag=" + 
project.getScm().getTag()
-                    + " does not permit rebuilders reproducible source 
checkout" );
-                // TODO is it possible to use Scm API to get SCM version?
-            }
-        }
-        else
-        {
-            p.println( "# no scm configured in pom.xml" );
-        }
-
-        if ( !sourceAvailable )
-        {
-            getLog().warn( "No source information available in buildinfo for 
rebuilders..." );
-        }
-    }
-
-    private void printOutput( PrintWriter p, MavenProject project, int 
projectIndex )
-            throws MojoExecutionException
-    {
-        String prefix = "outputs.";
-        if ( projectIndex >= 0 )
-        {
-            // aggregated buildinfo output
-            prefix += projectIndex + ".";
-            p.println( prefix + "coordinates=" + project.getGroupId() + ':' + 
project.getArtifactId() );
-        }
-        int n = 0;
-        if ( project.getArtifact().getFile() != null )
-        {
-            printArtifact( p, prefix, n++, project.getArtifact() );
-        }
-
-        for ( Artifact attached : project.getAttachedArtifacts() )
-        {
-            if ( attached.getType().endsWith( ".asc" ) )
-            {
-                // ignore pgp signatures
-                continue;
-            }
-            if ( attached.getType().equals( "buildinfo" ) )
-            {
-                // ignore buildinfo files (during aggregate)
-                continue;
-            }
-            if ( "javadoc".equals( attached.getClassifier() ) )
-            {
-                // TEMPORARY ignore javadoc, waiting for MJAVADOC-627 in 
m-javadoc-p 3.2.0
-                continue;
-            }
-            printArtifact( p, prefix, n++, attached );
-        }
-    }
-
-    private void printArtifact( PrintWriter p, String prefix, int i, Artifact 
artifact )
-            throws MojoExecutionException
-    {
-        File file = artifact.getFile();
-        p.println( prefix + i + ".filename=" + file.getName() );
-        p.println( prefix + i + ".length=" + file.length() );
-        p.println( prefix + i + ".checksums.sha512=" + 
DigestHelper.calculateSha512( file ) );
-    }
-
     private MavenProject getExecutionRoot()
     {
         for ( MavenProject p : reactorProjects )

Reply via email to