You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2022/08/14 11:26:47 UTC

[maven-jar-plugin] branch master updated: Code simplifications in AbstractMojo (#47)

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

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-jar-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 4148491  Code simplifications in AbstractMojo (#47)
4148491 is described below

commit 414849196259fb34c621b0cdaa0a9e923ca9bc42
Author: Russell Howe <rh...@siksai.co.uk>
AuthorDate: Sun Aug 14 12:26:43 2022 +0100

    Code simplifications in AbstractMojo (#47)
    
    * Replace StringBuilder with string concatenation in AbstractJarMojo#getJarFile
    
    * Simplify archiver selection
    
    * Simplify projectHasAlreadySetAnArtifact in AbstractJarMojo
    
    Inverting this test leads to a more readable flow.
    
    * Simplify hasClassifier
    
    We can remove all branching from this method and return the check
    directly.
---
 .../apache/maven/plugins/jar/AbstractJarMojo.java  | 42 ++++++++--------------
 1 file changed, 14 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
index 588dad8..d54386b 100644
--- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
@@ -196,16 +196,17 @@ public abstract class AbstractJarMojo
             throw new IllegalArgumentException( "finalName is not allowed to be null" );
         }
 
-        StringBuilder fileName = new StringBuilder( resultFinalName );
-
+        String fileName;
         if ( hasClassifier() )
         {
-            fileName.append( "-" ).append( classifier );
+            fileName = resultFinalName + "-" + classifier + ".jar";
+        }
+        else
+        {
+            fileName = resultFinalName + ".jar";
         }
 
-        fileName.append( ".jar" );
-
-        return new File( basedir, fileName.toString() );
+        return new File( basedir, fileName );
     }
 
     /**
@@ -243,18 +244,11 @@ public abstract class AbstractJarMojo
             }
         }
 
+        String archiverName = containsModuleDescriptor ? "mjar" : "jar";
+
         MavenArchiver archiver = new MavenArchiver();
         archiver.setCreatedBy( "Maven JAR Plugin", "org.apache.maven.plugins", "maven-jar-plugin" );
-
-        if ( containsModuleDescriptor )
-        {
-            archiver.setArchiver( (JarArchiver) archivers.get( "mjar" ) );
-        }
-        else
-        {
-            archiver.setArchiver( (JarArchiver) archivers.get( "jar" ) );
-        }
-
+        archiver.setArchiver( (JarArchiver) archivers.get( archiverName ) );
         archiver.setOutputFile( jarFile );
 
         // configure for Reproducible Builds based on outputTimestamp value
@@ -328,14 +322,12 @@ public abstract class AbstractJarMojo
 
     private boolean projectHasAlreadySetAnArtifact()
     {
-        if ( getProject().getArtifact().getFile() != null )
-        {
-            return getProject().getArtifact().getFile().isFile();
-        }
-        else
+        if ( getProject().getArtifact().getFile() == null )
         {
             return false;
         }
+
+        return getProject().getArtifact().getFile().isFile();
     }
 
     /**
@@ -343,13 +335,7 @@ public abstract class AbstractJarMojo
      */
     protected boolean hasClassifier()
     {
-        boolean result = false;
-        if ( getClassifier() != null && getClassifier().trim().length() > 0 )
-        {
-            result = true;
-        }
-
-        return result;
+        return getClassifier() != null && getClassifier().trim().length() > 0;
     }
 
     private String[] getIncludes()