Skip to content

Commit 4148491

Browse files
authored
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.
1 parent 46c017d commit 4148491

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,17 @@ protected File getJarFile( File basedir, String resultFinalName, String classifi
196196
throw new IllegalArgumentException( "finalName is not allowed to be null" );
197197
}
198198

199-
StringBuilder fileName = new StringBuilder( resultFinalName );
200-
199+
String fileName;
201200
if ( hasClassifier() )
202201
{
203-
fileName.append( "-" ).append( classifier );
202+
fileName = resultFinalName + "-" + classifier + ".jar";
203+
}
204+
else
205+
{
206+
fileName = resultFinalName + ".jar";
204207
}
205208

206-
fileName.append( ".jar" );
207-
208-
return new File( basedir, fileName.toString() );
209+
return new File( basedir, fileName );
209210
}
210211

211212
/**
@@ -243,18 +244,11 @@ public File createArchive()
243244
}
244245
}
245246

247+
String archiverName = containsModuleDescriptor ? "mjar" : "jar";
248+
246249
MavenArchiver archiver = new MavenArchiver();
247250
archiver.setCreatedBy( "Maven JAR Plugin", "org.apache.maven.plugins", "maven-jar-plugin" );
248-
249-
if ( containsModuleDescriptor )
250-
{
251-
archiver.setArchiver( (JarArchiver) archivers.get( "mjar" ) );
252-
}
253-
else
254-
{
255-
archiver.setArchiver( (JarArchiver) archivers.get( "jar" ) );
256-
}
257-
251+
archiver.setArchiver( (JarArchiver) archivers.get( archiverName ) );
258252
archiver.setOutputFile( jarFile );
259253

260254
// configure for Reproducible Builds based on outputTimestamp value
@@ -328,28 +322,20 @@ public void execute()
328322

329323
private boolean projectHasAlreadySetAnArtifact()
330324
{
331-
if ( getProject().getArtifact().getFile() != null )
332-
{
333-
return getProject().getArtifact().getFile().isFile();
334-
}
335-
else
325+
if ( getProject().getArtifact().getFile() == null )
336326
{
337327
return false;
338328
}
329+
330+
return getProject().getArtifact().getFile().isFile();
339331
}
340332

341333
/**
342334
* @return true in case where the classifier is not {@code null} and contains something else than white spaces.
343335
*/
344336
protected boolean hasClassifier()
345337
{
346-
boolean result = false;
347-
if ( getClassifier() != null && getClassifier().trim().length() > 0 )
348-
{
349-
result = true;
350-
}
351-
352-
return result;
338+
return getClassifier() != null && getClassifier().trim().length() > 0;
353339
}
354340

355341
private String[] getIncludes()

0 commit comments

Comments
 (0)