Added logging of archive file size

This commit is contained in:
Erik C. Thauvin 2023-10-30 21:36:09 -07:00
parent 58a2245840
commit 677994bb67
3 changed files with 35 additions and 14 deletions

View file

@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
@ -57,7 +58,7 @@ public abstract class AbstractBootOperation<T extends AbstractBootOperation<T>>
*
* @param directory the directory to delete
*/
public void deleteDirectories(File... directory) throws FileUtilsErrorException {
public static void deleteDirectories(File... directory) throws FileUtilsErrorException {
for (var d : directory) {
if (d.exists()) {
FileUtils.deleteDirectory(d);
@ -65,6 +66,34 @@ public abstract class AbstractBootOperation<T extends AbstractBootOperation<T>>
}
}
/**
* Return the given file size in bytes, kilobytes, megabytes, gigabytes or terabytes.
*
* @param file the file
* @return the file size in B, KB, MB, GB, or TB.
*/
public static String fileSize(File file) {
var size = file.length();
if (size <= 0) {
return "0 B";
}
var units = new String[]{"B", "KB", "MB", "GB", "TB"};
var digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups))
+ ' ' + units[digitGroups];
}
/**
* Makes a directory for the given path, including any necessary but nonexistent parent directories.
*
* @param path the directory path
*/
public static void mkDirs(File path) throws IOException {
if (!path.exists() && !path.mkdirs()) {
throw new IOException("ERROR: unable to create: " + path.getAbsolutePath());
}
}
/**
* Retrieves the destination directory in which the JAR will be created.
*
@ -376,17 +405,6 @@ public abstract class AbstractBootOperation<T extends AbstractBootOperation<T>>
return (T) this;
}
/**
* Makes a directory for the given path, including any necessary but nonexistent parent directories.
*
* @param path the directory path
*/
protected void mkDirs(File path) throws IOException {
if (!path.exists() && !path.mkdirs()) {
throw new IOException("ERROR: unable to create: " + path.getAbsolutePath());
}
}
/**
* Provides source directories that will be used for the jar archive creation.
*
@ -414,6 +432,7 @@ public abstract class AbstractBootOperation<T extends AbstractBootOperation<T>>
*
* @return {@code true} or an {@link IllegalArgumentException}
*/
@SuppressWarnings("SameReturnValue")
protected boolean verifyExecute() throws IllegalArgumentException {
if (mainClass() == null) {
throw new IllegalArgumentException("ERROR: project mainClass required.");

View file

@ -53,7 +53,8 @@ public class BootJarOperation extends AbstractBootOperation<BootJarOperation> {
var archive = executeCreateArchive(staging_dir);
if (!silent() && LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("The executable JAR was created: " + archive.getAbsolutePath());
LOGGER.info(String.format("The executable JAR was created: %s (%s)", archive.getAbsolutePath(),
fileSize(archive)));
}
} finally {
FileUtils.deleteDirectory(staging_dir);

View file

@ -57,7 +57,8 @@ public class BootWarOperation extends AbstractBootOperation<BootWarOperation> {
var archive = executeCreateArchive(staging_dir);
if (!silent() && LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("The executable WAR was created: " + archive.getAbsolutePath());
LOGGER.info(String.format("The executable WAR was created: %s (%s)", archive.getAbsolutePath(),
fileSize(archive)));
}
} finally {
FileUtils.deleteDirectory(staging_dir);