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 2020/03/02 06:54:43 UTC

[maven-studies] branch maven-buildinfo-plugin updated (bc61fdc -> cebcd9c)

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

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


    from bc61fdc  added newlines to generated buildinfo
     new 4522f56  download reference buildinfo file
     new cebcd9c  save reference artifacts when no reference buildinfo available

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pom.xml                                            |  12 +-
 .../maven/plugins/buildinfo/BuildInfoWriter.java   |   9 ++
 .../apache/maven/plugins/buildinfo/SaveMojo.java   | 171 +++++++++++++++++++--
 3 files changed, 175 insertions(+), 17 deletions(-)


[maven-studies] 01/02: download reference buildinfo file

Posted by hb...@apache.org.
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

commit 4522f560e24dd4dc88718533be286a0974324246
Author: Hervé Boutemy <hb...@apache.org>
AuthorDate: Sun Mar 1 18:04:24 2020 +0100

    download reference buildinfo file
---
 pom.xml                                            |  12 +-
 .../apache/maven/plugins/buildinfo/SaveMojo.java   | 127 +++++++++++++++++++--
 2 files changed, 124 insertions(+), 15 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0b03588..d827f5e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,6 +93,12 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-compat</artifactId>
+      <version>${mavenVersion}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <version>3.3.0</version>
@@ -103,12 +109,6 @@
       <version>1.13</version>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-compat</artifactId>
-      <version>${mavenVersion}</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven.plugin-tools</groupId>
       <artifactId>maven-plugin-annotations</artifactId>
       <scope>provided</scope>
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 5bbfa77..71056e1 100644
--- a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
@@ -20,6 +20,15 @@ package org.apache.maven.plugins.buildinfo;
  */
 
 import org.apache.commons.codec.Charsets;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 
@@ -29,6 +38,7 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
+import org.codehaus.plexus.util.FileUtils;
 
 import java.io.BufferedWriter;
 import java.io.File;
@@ -36,6 +46,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -79,10 +90,17 @@ public class SaveMojo
     //private String rebuildArgs;
 
     /**
-     * URL for reference build, containing either reference buildinfo file or reference artifacts.
+     * Repository for reference build, containing either reference buildinfo file or reference artifacts.<br/>
+     * Format: <code>id</code> or <code>url</code> or <code>id::url</code>
+     * <dl>
+     * <dt>id</dt>
+     * <dd>The id can be used to pick up the correct credentials from the settings.xml</dd>
+     * <dt>url</dt>
+     * <dd>The location of the repository</dd>
+     * </dl>
      */
-    @Parameter( property = "reference.url" )
-    private String referenceUrl;
+    @Parameter( property = "reference.repo" )
+    private String referenceRepo;
 
     /**
      * Directory of the downloaded reference files.
@@ -91,11 +109,32 @@ public class SaveMojo
     private File referenceDir;
 
     /**
+     * The local repository taken from Maven's runtime. Typically <code>$HOME/.m2/repository</code>.
+     */
+    @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
+    private ArtifactRepository localRepository;
+
+    /**
+     * List of Remote Repositories used by the resolver.
+     */
+    @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
+    private List<ArtifactRepository> remoteArtifactRepositories;
+
+    /**
      * Used for attaching the buildinfo file in the project.
      */
     @Component
     private MavenProjectHelper projectHelper;
 
+    /**
+     * Artifact Resolver, needed to resolve and download the {@code resourceBundles}.
+     */
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private ArtifactFactory artifactFactory;
+
     public void execute()
         throws MojoExecutionException
     {
@@ -124,10 +163,12 @@ public class SaveMojo
             getLog().info( "NOT adding buildinfo to the list of attached artifacts." );
         }
 
-        if ( referenceUrl != null )
+        if ( referenceRepo != null )
         {
-            getLog().info( "Checking against reference build from " + referenceUrl );
-            checkAgainstReference();
+            ArtifactRepository repo = createReferenceRepo();
+
+            getLog().info( "Checking against reference build from " + referenceRepo + "..." );
+            checkAgainstReference( repo );
         }
     }
 
@@ -176,10 +217,78 @@ public class SaveMojo
         return null;
     }
 
-    private void checkAgainstReference()
+    private void checkAgainstReference( ArtifactRepository repo )
+        throws MojoExecutionException
     {
         referenceDir.mkdirs();
-        getLog().warn( "TODO try to download reference buildinfo and compare..." );
-        getLog().warn( "TODO if no reference buildinfo, try to download reference artifacts and compare..." );
+
+        File referenceBuildinfo = downloadReferenceBuildinfo( repo );
+    }
+
+    private File downloadReferenceBuildinfo( ArtifactRepository repo )
+        throws MojoExecutionException
+    {
+        File referenceBuildinfo = new File( referenceDir, buildinfoFile.getName() );
+
+        Artifact buildinfo =
+                        artifactFactory.createArtifactWithClassifier( project.getGroupId(), project.getArtifactId(),
+                                                                      project.getVersion(), "buildinfo", "" );
+        try
+        {
+            artifactResolver.resolve( buildinfo, Collections.singletonList( repo ), localRepository );
+
+            FileUtils.copyFile( buildinfo.getFile(), referenceBuildinfo );
+            getLog().info( "Reference buildinfo file found, copied to " + referenceBuildinfo );
+        }
+        catch ( ArtifactResolutionException are )
+        {
+            throw new MojoExecutionException( "Error resolving buildinfo artifact " + buildinfo, are );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            getLog().warn( "Reference buildinfo file not found: "
+                + "it will be generated from downloaded reference artifacts" );
+        }
+        catch ( IOException ioe )
+        {
+            throw new MojoExecutionException( "Error copying buildinfo artifact " + buildinfo, ioe );
+        }
+
+        return referenceBuildinfo;
+    }
+
+    private ArtifactRepository createReferenceRepo()
+        throws MojoExecutionException
+    {
+        if ( referenceRepo.contains( "::" ) )
+        {
+            // id::url
+            int index = referenceRepo.indexOf( "::" );
+            String id = referenceRepo.substring( 0, index );
+            String url = referenceRepo.substring( index + 2 );
+            return createDeploymentArtifactRepository( id, url );
+        }
+        else if ( referenceRepo.contains( ":" ) )
+        {
+            // url, will use default "reference" id
+            return createDeploymentArtifactRepository( "reference", referenceRepo );
+        }
+
+        // id
+        for ( ArtifactRepository repo : remoteArtifactRepositories )
+        {
+            if ( referenceRepo.equals( repo.getId() ) )
+            {
+                return repo;
+            }
+        }
+        throw new MojoExecutionException( "Could not find repository with id = " + referenceRepo );
+    }
+
+    protected ArtifactRepository createDeploymentArtifactRepository( String id, String url )
+    {
+        return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
+                                            new ArtifactRepositoryPolicy() );
     }
+    
 }


[maven-studies] 02/02: save reference artifacts when no reference buildinfo available

Posted by hb...@apache.org.
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

commit cebcd9ca3a1a9aa10be0173e71e27c99e3a201bf
Author: Hervé Boutemy <hb...@apache.org>
AuthorDate: Mon Mar 2 07:53:38 2020 +0100

    save reference artifacts when no reference buildinfo available
---
 .../maven/plugins/buildinfo/BuildInfoWriter.java   |  9 +++
 .../apache/maven/plugins/buildinfo/SaveMojo.java   | 76 +++++++++++++++++-----
 2 files changed, 67 insertions(+), 18 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
index c4768f5..33ec2e9 100644
--- a/src/main/java/org/apache/maven/plugins/buildinfo/BuildInfoWriter.java
+++ b/src/main/java/org/apache/maven/plugins/buildinfo/BuildInfoWriter.java
@@ -21,6 +21,8 @@ package org.apache.maven.plugins.buildinfo;
 
 import java.io.File;
 import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -35,6 +37,7 @@ public class BuildInfoWriter
     private final Log log;
     private final PrintWriter p;
     private final boolean mono;
+    private final List<Artifact> artifacts = new ArrayList<>();
     private int projectCount = -1;
 
     BuildInfoWriter( Log log, PrintWriter p, boolean mono )
@@ -153,5 +156,11 @@ public class BuildInfoWriter
         p.println( prefix + i + ".filename=" + file.getName() );
         p.println( prefix + i + ".length=" + file.length() );
         p.println( prefix + i + ".checksums.sha512=" + DigestHelper.calculateSha512( file ) );
+        artifacts.add( artifact );
+    }
+
+    public List<Artifact> getArtifacts()
+    {
+        return artifacts;
     }
 }
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 71056e1..33726b0 100644
--- a/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/buildinfo/SaveMojo.java
@@ -151,7 +151,7 @@ public class SaveMojo
             }
         }
 
-        generateBuildinfo( mono );
+        List<Artifact> artifacts = generateBuildinfo( mono );
         getLog().info( "Saved " + ( mono ? "" : "aggregate " ) + "info on build to " + buildinfoFile );
 
         if ( attach )
@@ -165,14 +165,12 @@ public class SaveMojo
 
         if ( referenceRepo != null )
         {
-            ArtifactRepository repo = createReferenceRepo();
-
             getLog().info( "Checking against reference build from " + referenceRepo + "..." );
-            checkAgainstReference( repo );
+            checkAgainstReference( mono, artifacts );
         }
     }
 
-    private void generateBuildinfo( boolean mono )
+    private List<Artifact> generateBuildinfo( boolean mono )
             throws MojoExecutionException
     {
         MavenProject root = mono ? project : getExecutionRoot();
@@ -198,6 +196,7 @@ public class SaveMojo
                     bi.printArtifacts( project );
                 }
             }
+            return bi.getArtifacts();
         }
         catch ( IOException e )
         {
@@ -217,44 +216,85 @@ public class SaveMojo
         return null;
     }
 
-    private void checkAgainstReference( ArtifactRepository repo )
+    private void checkAgainstReference( boolean mono, List<Artifact> artifacts )
         throws MojoExecutionException
     {
+        ArtifactRepository repo = createReferenceRepo();
         referenceDir.mkdirs();
 
         File referenceBuildinfo = downloadReferenceBuildinfo( repo );
+
+        if ( !referenceBuildinfo.canRead() )
+        {
+            // download reference artifacts
+            for ( Artifact artifact : artifacts )
+            {
+                try
+                {
+                    downloadReference( repo, artifact );
+                }
+                catch ( ArtifactNotFoundException e )
+                {
+                    getLog().warn( "Reference artifact not found " + artifact );
+                }
+            }
+
+            // generate buildinfo from reference artifacts
+            try ( PrintWriter p =
+                new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream( referenceBuildinfo ),
+                                                                             Charsets.ISO_8859_1 ) ) ) )
+            {
+                BuildInfoWriter bi = new BuildInfoWriter( getLog(), p, mono );
+
+                // TODO artifact(s) fingerprints
+            }
+            catch ( IOException e )
+            {
+                throw new MojoExecutionException( "Error creating file " + referenceBuildinfo, e );
+            }
+        }
+
+        // TODO compare reference buildinfo vs actual
     }
 
     private File downloadReferenceBuildinfo( ArtifactRepository repo )
         throws MojoExecutionException
     {
-        File referenceBuildinfo = new File( referenceDir, buildinfoFile.getName() );
-
         Artifact buildinfo =
                         artifactFactory.createArtifactWithClassifier( project.getGroupId(), project.getArtifactId(),
                                                                       project.getVersion(), "buildinfo", "" );
         try
         {
-            artifactResolver.resolve( buildinfo, Collections.singletonList( repo ), localRepository );
+            downloadReference( repo, buildinfo );
 
-            FileUtils.copyFile( buildinfo.getFile(), referenceBuildinfo );
-            getLog().info( "Reference buildinfo file found, copied to " + referenceBuildinfo );
-        }
-        catch ( ArtifactResolutionException are )
-        {
-            throw new MojoExecutionException( "Error resolving buildinfo artifact " + buildinfo, are );
+            getLog().info( "Reference buildinfo file found, copied to " + buildinfo.getFile() );
         }
         catch ( ArtifactNotFoundException e )
         {
             getLog().warn( "Reference buildinfo file not found: "
                 + "it will be generated from downloaded reference artifacts" );
         }
+
+        return buildinfo.getFile();
+    }
+
+    private void downloadReference( ArtifactRepository repo, Artifact artifact )
+        throws MojoExecutionException, ArtifactNotFoundException
+    {
+        try
+        {
+            artifactResolver.resolve( artifact, Collections.singletonList( repo ), localRepository );
+
+            FileUtils.copyFile( artifact.getFile(), new File( referenceDir, artifact.getFile().getName() ) );
+        }
+        catch ( ArtifactResolutionException are )
+        {
+            throw new MojoExecutionException( "Error resolving reference artifact " + artifact, are );
+        }
         catch ( IOException ioe )
         {
-            throw new MojoExecutionException( "Error copying buildinfo artifact " + buildinfo, ioe );
+            throw new MojoExecutionException( "Error copying reference artifact " + artifact, ioe );
         }
-
-        return referenceBuildinfo;
     }
 
     private ArtifactRepository createReferenceRepo()