diff --git a/README.md b/README.md index 5eaebdb..22ce8ce 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ To install, please refer to the [extensions documentation](https://github.com/rife2/bld/wiki/Extensions). -To create a Spring Boot executable Java Archive (JAR) from the current project: +To create a [Spring Boot executable Java Archive](https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html) +(JAR) from the current project: ```java @@BuildCommand(summary = "Creates an executable JAR for the project") @@ -25,7 +26,8 @@ public void bootjar() throws Exception { ./bld compile bootjar ``` -To create a Spring Boot executable Web Archive (WAR) from the current project: +To create a [Spring Boot executable Web Archive](https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#appendix.executable-jar.nested-jars.war-structure) +(WAR) from the current project: ```java @BuildCommand(summary = "Create an executable WAR for the project") diff --git a/examples/src/bld/java/com/example/demo/DemoApplicationBuild.java b/examples/src/bld/java/com/example/demo/DemoApplicationBuild.java index fb06087..b591297 100644 --- a/examples/src/bld/java/com/example/demo/DemoApplicationBuild.java +++ b/examples/src/bld/java/com/example/demo/DemoApplicationBuild.java @@ -39,11 +39,12 @@ public class DemoApplicationBuild extends WebProject { } public static void main(String[] args) { + var level = Level.ALL; var logger = Logger.getLogger("rife.bld.extension"); ConsoleHandler consoleHandler = new ConsoleHandler(); - consoleHandler.setLevel(Level.ALL); + consoleHandler.setLevel(level); logger.addHandler(consoleHandler); - logger.setLevel(Level.ALL); + logger.setLevel(level); logger.setUseParentHandlers(false); new DemoApplicationBuild().start(args); diff --git a/src/main/java/rife/bld/extension/AbstractBootOperation.java b/src/main/java/rife/bld/extension/AbstractBootOperation.java index d486f4c..df4bb43 100644 --- a/src/main/java/rife/bld/extension/AbstractBootOperation.java +++ b/src/main/java/rife/bld/extension/AbstractBootOperation.java @@ -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> * * @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> } } + /** + * 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> 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> * * @return {@code true} or an {@link IllegalArgumentException} */ + @SuppressWarnings("SameReturnValue") protected boolean verifyExecute() throws IllegalArgumentException { if (mainClass() == null) { throw new IllegalArgumentException("ERROR: project mainClass required."); diff --git a/src/main/java/rife/bld/extension/BootJarOperation.java b/src/main/java/rife/bld/extension/BootJarOperation.java index ae5aed7..72d6361 100644 --- a/src/main/java/rife/bld/extension/BootJarOperation.java +++ b/src/main/java/rife/bld/extension/BootJarOperation.java @@ -53,7 +53,8 @@ public class BootJarOperation extends AbstractBootOperation { 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); diff --git a/src/main/java/rife/bld/extension/BootWarOperation.java b/src/main/java/rife/bld/extension/BootWarOperation.java index 1a73de0..2b05d0f 100644 --- a/src/main/java/rife/bld/extension/BootWarOperation.java +++ b/src/main/java/rife/bld/extension/BootWarOperation.java @@ -57,7 +57,8 @@ public class BootWarOperation extends AbstractBootOperation { 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);