Skip to content

Commit

Permalink
Code simplifications in AbstractMojo (#47)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
rhowe committed Aug 14, 2022
1 parent 46c017d commit 4148491
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -243,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
Expand Down Expand Up @@ -328,28 +322,20 @@ 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();
}

/**
* @return true in case where the classifier is not {@code null} and contains something else than white spaces.
*/
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()
Expand Down

0 comments on commit 4148491

Please sign in to comment.