Skip to content

Commit dc1ddc4

Browse files
peter277bmarwell
authored andcommitted
Refactor artifact attachment logic into separate method with guard clauses
1 parent 28bb374 commit dc1ddc4

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/it/projects/attach_skip/verify.groovy

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import java.util.*;
2323
import java.util.jar.*;
2424
import org.codehaus.plexus.util.*;
2525

26-
try
27-
{
26+
try {
2827
File target = new File( basedir, "target" )
2928
if ( !target.exists() || !target.isDirectory() ) {
3029
System.err.println( "target file is missing or not a directory." )
@@ -87,9 +86,7 @@ try
8786
}
8887

8988
return true
90-
}
91-
catch( Throwable e )
92-
{
89+
} catch( Throwable e ) {
9390
e.printStackTrace()
9491
return false
9592
}

src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,17 +474,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
474474

475475
File createZipArchiveFromImage = createZipArchiveFromImage(buildDirectory, outputDirectoryImage);
476476

477-
if (attach) {
478-
if (hasClassifier()) {
479-
projectHelper.attachArtifact(getProject(), "jlink", getClassifier(), createZipArchiveFromImage);
480-
} else {
481-
if (projectHasAlreadySetAnArtifact()) {
482-
throw new MojoExecutionException("You have to use a classifier "
483-
+ "to attach supplemental artifacts to the project instead of replacing them.");
484-
}
485-
getProject().getArtifact().setFile(createZipArchiveFromImage);
486-
}
487-
}
477+
attachArtifactUnlessDisabled(createZipArchiveFromImage);
488478
}
489479

490480
/**
@@ -640,6 +630,22 @@ private File createZipArchiveFromImage(File outputDirectory, File outputDirector
640630
return resultArchive;
641631
}
642632

633+
private void attachArtifactUnlessDisabled(File artifactFile) throws MojoExecutionException {
634+
if (!attach) {
635+
return;
636+
}
637+
638+
if (hasClassifier()) {
639+
projectHelper.attachArtifact(getProject(), "jlink", getClassifier(), artifactFile);
640+
} else {
641+
if (projectHasAlreadySetAnArtifact()) {
642+
throw new MojoExecutionException("You have to use a classifier "
643+
+ "to attach supplemental artifacts to the project instead of replacing them.");
644+
}
645+
getProject().getArtifact().setFile(artifactFile);
646+
}
647+
}
648+
643649
private void failIfParametersAreNotInTheirValidValueRanges() throws MojoFailureException {
644650
if (endian != null && (!"big".equals(endian) && !"little".equals(endian))) {
645651
String message = "The given endian parameter " + endian

0 commit comments

Comments
 (0)