You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2023/02/03 21:29:13 UTC

[maven-artifact-plugin] 01/01: [MARTIFACT-39] Replace MavenVersion with managed component RuntimeInformation

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

michaelo pushed a commit to branch MARTIFACT-39
in repository https://gitbox.apache.org/repos/asf/maven-artifact-plugin.git

commit 4e89e8d4abe349a1ed1703785bb77e4e46795674
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Fri Feb 3 22:26:53 2023 +0100

    [MARTIFACT-39] Replace MavenVersion with managed component RuntimeInformation
    
    This closes #13
---
 .../artifact/buildinfo/AbstractBuildinfoMojo.java  |   6 +-
 .../artifact/buildinfo/BuildInfoWriter.java        |   8 +-
 .../plugins/artifact/buildinfo/CompareMojo.java    |   3 +-
 .../plugins/artifact/buildinfo/MavenVersion.java   | 104 ---------------------
 .../artifact/buildinfo/ReferenceBuildinfoUtil.java |   9 +-
 5 files changed, 20 insertions(+), 110 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java
index ab29a35..26a8e34 100644
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java
+++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java
@@ -28,6 +28,7 @@ import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.rtinfo.RuntimeInformation;
 import org.apache.maven.shared.utils.io.FileUtils;
 import org.apache.maven.toolchain.Toolchain;
 import org.apache.maven.toolchain.ToolchainManager;
@@ -124,6 +125,9 @@ public abstract class AbstractBuildinfoMojo
     @Component
     protected ArtifactHandlerManager artifactHandlerManager;
 
+    @Component
+    protected RuntimeInformation rtInformation;
+
     @Override
     public void execute()
         throws MojoExecutionException
@@ -246,7 +250,7 @@ public abstract class AbstractBuildinfoMojo
         try ( PrintWriter p = new PrintWriter( new BufferedWriter(
                 new OutputStreamWriter( new FileOutputStream( buildinfoFile ), StandardCharsets.UTF_8 ) ) ) )
         {
-            BuildInfoWriter bi = new BuildInfoWriter( getLog(), p, mono, artifactHandlerManager );
+            BuildInfoWriter bi = new BuildInfoWriter( getLog(), p, mono, artifactHandlerManager, rtInformation );
             bi.setIgnoreJavadoc( ignoreJavadoc );
             bi.setIgnore( ignore );
             bi.setToolchain( getToolchain() );
diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
index 6169418..3c85e51 100644
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
+++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java
@@ -33,6 +33,7 @@ import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.rtinfo.RuntimeInformation;
 import org.apache.maven.shared.utils.PropertyUtils;
 import org.apache.maven.toolchain.Toolchain;
 
@@ -45,18 +46,21 @@ class BuildInfoWriter
     private final PrintWriter p;
     private final boolean mono;
     private final ArtifactHandlerManager artifactHandlerManager;
+    private final RuntimeInformation rtInformation;
     private final Map<Artifact, String> artifacts = new LinkedHashMap<>();
     private int projectCount = -1;
     private boolean ignoreJavadoc = true;
     private Set<String> ignore;
     private Toolchain toolchain;
 
-    BuildInfoWriter( Log log, PrintWriter p, boolean mono, ArtifactHandlerManager artifactHandlerManager )
+    BuildInfoWriter( Log log, PrintWriter p, boolean mono, ArtifactHandlerManager artifactHandlerManager,
+                     RuntimeInformation rtInformation )
     {
         this.log = log;
         this.p = p;
         this.mono = mono;
         this.artifactHandlerManager = artifactHandlerManager;
+        this.rtInformation = rtInformation;
     }
 
     void printHeader( MavenProject project, MavenProject aggregate, boolean reproducible )
@@ -93,7 +97,7 @@ class BuildInfoWriter
         p.println( "# Maven rebuild instructions and effective environment" );
         if ( !reproducible )
         {
-            p.println( "mvn.version=" + MavenVersion.createMavenVersionString() );
+            p.println( "mvn.version=" + rtInformation.getMavenVersion() );
         }
         if ( ( project.getPrerequisites() != null ) && ( project.getPrerequisites().getMaven() != null ) )
         {
diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java
index f6d95b6..4c05e1b 100644
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java
+++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java
@@ -150,7 +150,8 @@ public class CompareMojo
         RemoteRepository repo = createReferenceRepo();
 
         ReferenceBuildinfoUtil rmb = new ReferenceBuildinfoUtil( getLog(), referenceDir, artifacts, artifactFactory,
-                                                                 repoSystem, repoSession, artifactHandlerManager );
+                                                                 repoSystem, repoSession, artifactHandlerManager,
+                                                                 rtInformation );
 
         return rmb.downloadOrCreateReferenceBuildinfo( repo, project, buildinfoFile, mono );
     }
diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/MavenVersion.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/MavenVersion.java
deleted file mode 100644
index 051eb62..0000000
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/MavenVersion.java
+++ /dev/null
@@ -1,104 +0,0 @@
-package org.apache.maven.plugins.artifact.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 org.apache.maven.cli.MavenCli;
-import org.apache.maven.shared.utils.StringUtils;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Properties;
-
-/**
- * Helper to calculate the full Maven version string, as done in Maven Core
- * in CLIReportingUtils.
- * @see org.apache.maven.cli.CLIReportingUtils
- */
-
-class MavenVersion
-{
-    static String createMavenVersionString()
-    {
-        return createMavenVersionString( getBuildProperties() );
-    }
-
-    /**
-     * Create a human readable string containing the Maven version, buildnumber, and time of build
-     *
-     * @param buildProperties The build properties
-     * @return Readable build info
-     */
-    private static String createMavenVersionString( Properties buildProperties )
-    {
-        String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
-        String version = reduce( buildProperties.getProperty( "version" ) );
-        String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
-        String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
-
-        String msg = distributionName + " ";
-        msg += ( version != null ? version : "<version unknown>" );
-        if ( rev != null || timestamp != null )
-        {
-            msg += " (";
-            msg += ( rev != null ? rev : "" );
-            if ( StringUtils.isNotBlank( timestamp ) )
-            {
-                String ts = formatTimestamp( Long.valueOf( timestamp ) );
-                msg += ( rev != null ? "; " : "" ) + ts;
-            }
-            msg += ")";
-        }
-        return msg;
-    }
-
-    private static String reduce( String s )
-    {
-        return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
-    }
-
-    private static Properties getBuildProperties()
-    {
-        Properties properties = new Properties();
-
-        try ( InputStream resourceAsStream = MavenCli.class.getResourceAsStream(
-                "/org/apache/maven/messages/build.properties" ) )
-        {
-
-            if ( resourceAsStream != null )
-            {
-                properties.load( resourceAsStream );
-            }
-        }
-        catch ( IOException e )
-        {
-            System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
-        }
-
-        return properties;
-    }
-
-    private static String formatTimestamp( long timestamp )
-    {
-        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" );
-        return sdf.format( new Date( timestamp ) );
-    }
-}
diff --git a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/ReferenceBuildinfoUtil.java b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/ReferenceBuildinfoUtil.java
index 00516af..92c7d2e 100644
--- a/src/main/java/org/apache/maven/plugins/artifact/buildinfo/ReferenceBuildinfoUtil.java
+++ b/src/main/java/org/apache/maven/plugins/artifact/buildinfo/ReferenceBuildinfoUtil.java
@@ -26,6 +26,7 @@ import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.rtinfo.RuntimeInformation;
 import org.apache.maven.shared.utils.io.IOUtil;
 import org.apache.maven.shared.utils.io.FileUtils;
 import org.eclipse.aether.AbstractForwardingRepositorySystemSession;
@@ -91,10 +92,13 @@ class ReferenceBuildinfoUtil
 
     private final ArtifactHandlerManager artifactHandlerManager;
 
+    private final RuntimeInformation rtInformation;
+
     ReferenceBuildinfoUtil( Log log, File referenceDir, Map<Artifact, String> artifacts,
                                       ArtifactFactory artifactFactory, RepositorySystem repoSystem,
                                       RepositorySystemSession repoSession,
-                                      ArtifactHandlerManager artifactHandlerManager )
+                                      ArtifactHandlerManager artifactHandlerManager,
+                                      RuntimeInformation rtInformation )
     {
         this.log = log;
         this.referenceDir = referenceDir;
@@ -103,6 +107,7 @@ class ReferenceBuildinfoUtil
         this.repoSystem = repoSystem;
         this.repoSession = repoSession;
         this.artifactHandlerManager = artifactHandlerManager;
+        this.rtInformation = rtInformation;
     }
 
     File downloadOrCreateReferenceBuildinfo( RemoteRepository repo, MavenProject project, File buildinfoFile,
@@ -162,7 +167,7 @@ class ReferenceBuildinfoUtil
                 new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream( referenceBuildinfo ),
                                                                              StandardCharsets.UTF_8 ) ) ) )
             {
-                BuildInfoWriter bi = new BuildInfoWriter( log, p, mono, artifactHandlerManager );
+                BuildInfoWriter bi = new BuildInfoWriter( log, p, mono, artifactHandlerManager, rtInformation );
 
                 if ( javaVersion != null || osName != null )
                 {