Skip to content

Commit

Permalink
[MJLINK-90] Make class more direct and simpler (#229)
Browse files Browse the repository at this point in the history
* simplify
  • Loading branch information
elharo authored Dec 8, 2024
1 parent 235a5de commit b57848a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,54 +124,6 @@ protected MavenSession getSession() {
return session;
}

/**
* Returns the archive file to generate, based on an optional classifier.
*
* @param basedir the output directory
* @param finalName the name of the ear file
* @param classifier an optional classifier
* @param archiveExt The extension of the file.
* @return the file to generate
*/
protected File getArchiveFile(File basedir, String finalName, String classifier, String archiveExt) {
if (basedir == null) {
throw new IllegalArgumentException("basedir is not allowed to be null");
}
if (finalName == null) {
throw new IllegalArgumentException("finalName is not allowed to be null");
}
if (archiveExt == null) {
throw new IllegalArgumentException("archiveExt is not allowed to be null");
}

if (finalName.isEmpty()) {
throw new IllegalArgumentException("finalName is not allowed to be empty.");
}
if (archiveExt.isEmpty()) {
throw new IllegalArgumentException("archiveExt is not allowed to be empty.");
}

StringBuilder fileName = new StringBuilder(finalName);

if (hasClassifier(classifier)) {
fileName.append("-").append(classifier);
}

fileName.append('.');
fileName.append(archiveExt);

return new File(basedir, fileName.toString());
}

protected boolean hasClassifier(String classifier) {
boolean result = false;
if (classifier != null && !classifier.isEmpty()) {
result = true;
}

return result;
}

/**
* This will convert a module path separated by either {@code :} or {@code ;} into a string which uses the platform
* path separator uniformly.
Expand Down
63 changes: 44 additions & 19 deletions src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public class JLinkMojo extends AbstractJLinkMojo {
* The byte order of the generated Java Run Time image. <code>--endian &lt;little|big&gt;</code>. If the endian is
* not given the default is: <code>native</code>.
*/
// TODO: Should we define either little or big as default? or should we left as it.
// TODO: Should we define either little or big as default? or should we leave it as is?
@Parameter
private String endian;

Expand Down Expand Up @@ -273,14 +273,14 @@ public class JLinkMojo extends AbstractJLinkMojo {
private boolean ignoreSigningInformation;

/**
* This will suppress to have an <code>includes</code> directory in the resulting Java Run Time Image. The JLink
* This will omit an <code>includes</code> directory from the resulting Java Run Time Image. The JLink
* command line equivalent is: <code>--no-header-files</code>
*/
@Parameter(defaultValue = "false")
private boolean noHeaderFiles;

/**
* This will suppress to have the <code>man</code> directory in the resulting Java Run Time Image. The JLink command
* This will omit the <code>man</code> directory from the resulting Java Run Time Image. The JLink command
* line equivalent is: <code>--no-man-pages</code>
*/
@Parameter(defaultValue = "false")
Expand Down Expand Up @@ -345,8 +345,8 @@ public class JLinkMojo extends AbstractJLinkMojo {
/**
* Classifier to add to the artifact generated. If given, the artifact will be attached
* as a supplemental artifact.
* If not given this will create the main artifact which is the default behavior.
* If you try to do that a second time without using a classifier the build will fail.
* If not given, this will create the main artifact which is the default behavior.
* If you try to do that a second time without using a classifier, the build will fail.
*/
@Parameter
private String classifier;
Expand Down Expand Up @@ -405,7 +405,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

ifOutputDirectoryExistsDelteIt();

JLinkExecutor jLinkExec = getExecutor();
JLinkExecutor jLinkExec = getJlinkExecutor();
Collection<String> modulesToAdd = new ArrayList<>();
if (addModules != null) {
modulesToAdd.addAll(addModules);
Expand Down Expand Up @@ -548,10 +548,6 @@ private Map<String, File> getModulePathElements() throws MojoFailureException {
return modulepathElements;
}

private JLinkExecutor getExecutor() {
return getJlinkExecutor();
}

private boolean projectHasAlreadySetAnArtifact() {
if (getProject().getArtifact().getFile() != null) {
return getProject().getArtifact().getFile().isFile();
Expand All @@ -561,15 +557,10 @@ private boolean projectHasAlreadySetAnArtifact() {
}

/**
* @return true in case where the classifier is not {@code null} and contains something else than white spaces.
* @return true when the classifier is not {@code null} and not empty
*/
protected boolean hasClassifier() {
boolean result = false;
if (getClassifier() != null && !getClassifier().isEmpty()) {
result = true;
}

return result;
private boolean hasClassifier() {
return hasClassifier(getClassifier());
}

private File createZipArchiveFromImage(File outputDirectory, File outputDirectoryImage)
Expand All @@ -582,7 +573,7 @@ private File createZipArchiveFromImage(File outputDirectory, File outputDirector
zipArchiver.configureReproducibleBuild(FileTime.from(lastModified.get()));
}

File resultArchive = getArchiveFile(outputDirectory, finalName, getClassifier(), "zip");
File resultArchive = getZipFile(outputDirectory, finalName, getClassifier());

zipArchiver.setDestFile(resultArchive);
try {
Expand Down Expand Up @@ -780,7 +771,41 @@ private boolean hasLimitModules() {
/**
* {@inheritDoc}
*/
@Override
protected String getClassifier() {
return classifier;
}

/**
* Returns the archive file to generate, based on an optional classifier.
*
* @param basedir the output directory
* @param finalName the name of the zip file
* @param classifier an optional classifier
* @return the file to generate
*/
private static File getZipFile(File basedir, String finalName, String classifier) {
if (finalName.isEmpty()) {
throw new IllegalArgumentException("finalName is not allowed to be empty.");
}

StringBuilder fileName = new StringBuilder(finalName);

if (hasClassifier(classifier)) {
fileName.append("-").append(classifier);
}

fileName.append(".zip");

return new File(basedir, fileName.toString());
}

private static boolean hasClassifier(String classifier) {
boolean result = false;
if (classifier != null && !classifier.isEmpty()) {
result = true;
}

return result;
}
}

0 comments on commit b57848a

Please sign in to comment.