Compare commits

...

3 commits

5 changed files with 42 additions and 18 deletions

View file

@ -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")

View file

@ -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);

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);