From 61f7352cd342ec62d3a1592eae2cd24d76d66f6c Mon Sep 17 00:00:00 2001 From: Russell Howe Date: Wed, 10 Aug 2022 08:07:42 +0100 Subject: [PATCH 1/4] Replace StringBuilder with string concatenation in AbstractJarMojo#getJarFile --- .../apache/maven/plugins/jar/AbstractJarMojo.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 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..ef1f4f4 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 @@ protected File getJarFile( File basedir, String resultFinalName, String classifi 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 ); } /** From 9e9ab0a2df57d2c9c6f765ecfba21c08578c5288 Mon Sep 17 00:00:00 2001 From: Russell Howe Date: Wed, 10 Aug 2022 08:08:49 +0100 Subject: [PATCH 2/4] Simplify archiver selection --- .../apache/maven/plugins/jar/AbstractJarMojo.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 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 ef1f4f4..b809084 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -244,18 +244,11 @@ public File createArchive() } } + 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 From d26c272963d5fc43ecbd6486a0cc566c4150df1d Mon Sep 17 00:00:00 2001 From: Russell Howe Date: Wed, 10 Aug 2022 08:09:57 +0100 Subject: [PATCH 3/4] Simplify projectHasAlreadySetAnArtifact in AbstractJarMojo Inverting this test leads to a more readable flow. --- .../org/apache/maven/plugins/jar/AbstractJarMojo.java | 8 +++----- 1 file changed, 3 insertions(+), 5 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 b809084..9285e6e 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -322,14 +322,12 @@ public void execute() 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(); } /** From 611bc678a04d23b9dc151937301a73123f814463 Mon Sep 17 00:00:00 2001 From: Russell Howe Date: Wed, 10 Aug 2022 08:10:54 +0100 Subject: [PATCH 4/4] Simplify hasClassifier We can remove all branching from this method and return the check directly. --- .../org/apache/maven/plugins/jar/AbstractJarMojo.java | 8 +------- 1 file changed, 1 insertion(+), 7 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 9285e6e..d54386b 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -335,13 +335,7 @@ private boolean projectHasAlreadySetAnArtifact() */ 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()