Cleaned up abstraction, etc.

This commit is contained in:
Erik C. Thauvin 2023-10-28 11:46:42 -07:00
parent 287064dec4
commit f529b957f1
3 changed files with 421 additions and 147 deletions

View file

@ -42,13 +42,15 @@ import java.util.spi.ToolProvider;
* @since 1.0
*/
public abstract class AbstractBootOperation extends AbstractOperation<AbstractBootOperation> {
private final List<File> infLibs_ = new ArrayList<>();
private final List<File> launcherJars_ = new ArrayList<>();
private final List<BootManifestAttribute> manifestAttributes_ = new ArrayList<>();
private final List<File> sourceDirectories_ = new ArrayList<>();
protected
Project project_;
private String destinationArchiveFileName;
private File destinationDirectory_;
private String launcherClass_;
private String mainClass_;
private Project project_;
public void deleteDirectories(File... directory) throws FileUtilsErrorException {
for (var d : directory) {
@ -92,7 +94,6 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
*
* @param directory the war destination directory
* @return this operation instance
* @since 1.5
*/
public AbstractBootOperation destinationDirectory(File directory) throws IOException {
destinationDirectory_ = directory;
@ -100,29 +101,15 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
return this;
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} classes.
*/
protected void executeCopyBootInfClassesFiles(File stagingBootInfDirectory) throws IOException {
var boot_inf_classes_dir = new File(stagingBootInfDirectory, "classes");
mkDirs(boot_inf_classes_dir);
for (var dir : sourceDirectories_) {
FileUtils.copyDirectory(dir, boot_inf_classes_dir);
}
deleteDirectories(new File(boot_inf_classes_dir, "resources"), new File(boot_inf_classes_dir, "templates"));
}
/**
* Part of the {@link #execute} operation, copy the {@code spring-boot-loader} archive content to the staging directory.
*/
protected void executeCopyBootLoader(File stagingDirectory) throws FileUtilsErrorException {
if (project_.standaloneClasspathJars().isEmpty()) {
if (launcherJars_.isEmpty()) {
throw new IllegalArgumentException("ERROR: Spring Boot Loader required.");
} else {
var meta_inf_dir = new File(stagingDirectory, "META-INF");
for (var jar : project_.standaloneClasspathJars()) {
for (var jar : launcherJars()) {
FileUtils.unzipFile(jar, stagingDirectory);
if (meta_inf_dir.exists()) {
FileUtils.deleteDirectory(meta_inf_dir);
@ -131,6 +118,32 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
}
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} or {@code WEB-INF} classes.
*/
protected void executeCopyInfClassesFiles(File stagingInfDirectory) throws IOException {
var inf_classes_dir = new File(stagingInfDirectory, "classes");
mkDirs(inf_classes_dir);
for (var dir : sourceDirectories()) {
FileUtils.copyDirectory(dir, inf_classes_dir);
}
deleteDirectories(new File(inf_classes_dir, "resources"), new File(inf_classes_dir, "templates"));
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} or (@code WEB-INF) libs.
*/
protected void executeCopyInfLibs(File stagingInfDirectory) throws IOException {
var inf_lib_dir = new File(stagingInfDirectory, "lib");
mkDirs(inf_lib_dir);
for (var jar : infLibs_) {
Files.copy(jar.toPath(), inf_lib_dir.toPath().resolve(jar.getName()));
}
}
/**
* Part of the {@link #execute} operation, create the archive from the staging directory.
*/
@ -190,32 +203,90 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
}
/**
* Part of the {@link #execute} operation, creates the {@code WEB-INF} staging directory.
* Provides library JARs that will be used for the archive creation.
*
* @param jars Java archive files
* @return this operation instance
*/
protected File executeCreateWebInfDirectory(File stagingDirectory) throws IOException {
var boot_inf = new File(stagingDirectory, "WEB-INF");
mkDirs(boot_inf);
return boot_inf;
public AbstractBootOperation infLibs(List<File> jars) {
infLibs_.addAll(jars);
return this;
}
/**
* Part of the {@link #execute} operation, configure the JAR launcher class.
* Provides library JARs that will be used for the archive creation.
*
* @param jar Java archive file
* @return this operation instance
*/
protected AbstractBootOperation launcherClass(String className) {
public AbstractBootOperation infLibs(File... jar) {
infLibs_.addAll(List.of(jar));
return this;
}
/**
* Retrieves the library JARs in {@code BOOT-INF} or {@code WEB-INF}.
*/
public List<File> infLibs() {
return infLibs_;
}
/**
* Part of the {@link #execute} operation, configure the JAR launcher ({@code spring-boot-loader}) class name.
*/
public AbstractBootOperation launcherClass(String className) {
launcherClass_ = className;
return this;
}
/**
* Retrieves the JAR launcher class fully-qualified name.
* Retrieves the JAR launcher ({@code spring-boot-loader}) class fully-qualified name.
*/
protected String launcherClass() {
if (launcherClass_ == null) {
throw new IllegalArgumentException("ERROR: Spring boot launcher class required.");
throw new IllegalArgumentException("ERROR: Spring boot launcher (spring-boot-loader) class " +
"required.");
}
return launcherClass_;
}
/**
* Retrieves the launcher ({@code spring-boot-loader}) JARs.
*/
public List<File> launcherJars() {
return launcherJars_;
}
/**
* Part of the {@link #execute} operation, configure the launcher ({@code spring-boot-loader}) JAR location.
*/
public AbstractBootOperation launcherJars(List<File> jars) throws IOException {
if (!jars.isEmpty()) {
for (var j : jars) {
if (!j.exists()) {
throw new IOException("ERROR: launcher (spring-boot-loader) JAR(s) not found: " + j);
}
}
launcherJars_.addAll(jars);
}
return this;
}
/**
* Provides the fully-qualified main class name.
*/
protected AbstractBootOperation mainClass(String className) {
mainClass_ = className;
return this;
}
/**
* Retrieves the main class name.
*/
public String mainClass() {
return mainClass_;
}
/**
* Provides an attribute to put in the JAR manifest.
*
@ -242,7 +313,6 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
*
* @param attributes the attributes to put in the manifest
* @return this operation instance
* \
*/
public AbstractBootOperation manifestAttributes(Collection<BootManifestAttribute> attributes) {
manifestAttributes_.addAll(attributes);
@ -258,6 +328,21 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
}
}
/**
* Provides the bld project.
*/
public AbstractBootOperation project(Project project) {
project_ = project;
return this;
}
/**
* Retrieves the bld project.
*/
public Project project() {
return project_;
}
/**
* Provides source directories that will be used for the jar archive creation.
*
@ -268,4 +353,26 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
sourceDirectories_.addAll(List.of(directories));
return this;
}
/**
* Retrieves the source directories that will be used for the jar archive creation.
*/
public List<File> sourceDirectories() {
return sourceDirectories_;
}
/**
* Verifies that all the elements required to create the archived have been provided, throws an
* {@link IllegalArgumentException} otherwise.
*/
protected boolean verifyExecute() throws IllegalArgumentException {
if (mainClass() == null) {
throw new IllegalArgumentException("ERROR: project mainClass required.");
} else if (launcherClass().isEmpty()) {
throw new IllegalArgumentException(("ERROR: launcher (spring-boot-loader) class required"));
} else if (launcherJars().isEmpty()) {
throw new IllegalArgumentException(("ERROR: launcher (spring-boot-loader) JAR(s) required"));
}
return true;
}
}

View file

@ -22,81 +22,40 @@ import rife.tools.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Builds and creates a Sprint Boot executable java archive (JAR).
* Builds and creates a Spring Boot executable Java archive (JAR).
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class BootJarOperation extends AbstractBootOperation {
private static final Logger LOGGER = Logger.getLogger(BootJarOperation.class.getName());
private final List<File> libJars_ = new ArrayList<>();
/**
* Performs the BootJar operation.
* Provides the destination file name that will be used for the archive creation.
*
* @param name the war archive destination file name
* @return this operation instance
*/
@Override
public void execute() throws Exception {
if (project_ == null) {
throw new IllegalArgumentException("ERROR: project required.");
} else if (project_.mainClass() == null) {
throw new IllegalArgumentException("ERROR: project mainClass required.");
}
var staging_dir = Files.createTempDirectory("bootjar").toFile();
try {
var boot_inf_dir = executeCreateWebInfDirectory(staging_dir);
executeCopyBootInfClassesFiles(boot_inf_dir);
executeCopyBootInfLibs(boot_inf_dir);
executeCopyBootLoader(staging_dir);
executeCreateArchive(staging_dir, LOGGER);
if (!silent()) {
System.out.printf("The executable JAR (%s) was created in: %s%n", destinationArchiveFileName(),
destinationDirectory());
}
} finally {
FileUtils.deleteDirectory(staging_dir);
}
public BootJarOperation destinationArchiveFileName(String name) {
return (BootJarOperation) super.destinationArchiveFileName(name);
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} libs.
* Provides the destination directory in which the archive will be created.
*
* @param directory the war destination directory
* @return this operation instance
*/
protected void executeCopyBootInfLibs(File stagingBootInfDirectory) throws IOException {
var boot_inf_lib_dir = new File(stagingBootInfDirectory, "lib");
mkDirs(boot_inf_lib_dir);
for (var jar : libJars_) {
Files.copy(jar.toPath(), boot_inf_lib_dir.toPath().resolve(jar.getName()));
}
}
/**
* Configures the operation from a {@link Project}.
*/
public AbstractBootOperation fromProject(Project project) throws IOException {
project_ = project;
return bootInfLibs(project_.compileClasspathJars())
.bootInfLibs(project_.runtimeClasspathJars())
.launcherClass("org.springframework.boot.loader.JarLauncher")
.manifestAttributes(
List.of(
new BootManifestAttribute("Manifest-Version", "1.0"),
new BootManifestAttribute("Main-Class", launcherClass()),
new BootManifestAttribute("Start-Class", project_.mainClass()))
)
.destinationDirectory(project.buildDistDirectory())
.destinationArchiveFileName(project_.archiveBaseName() + "-" + project_.version() + "-boot.jar")
.sourceDirectories(project_.buildMainDirectory(), project_.srcMainResourcesDirectory());
@Override
public BootJarOperation destinationDirectory(File directory) throws IOException {
return (BootJarOperation) super.destinationDirectory(directory);
}
/**
@ -105,9 +64,9 @@ public class BootJarOperation extends AbstractBootOperation {
* @param jars Java archive files
* @return this operation instance
*/
public BootJarOperation bootInfLibs(Collection<File> jars) {
libJars_.addAll(jars);
return this;
@Override
public BootJarOperation infLibs(List<File> jars) {
return (BootJarOperation) super.infLibs(jars);
}
/**
@ -116,8 +75,132 @@ public class BootJarOperation extends AbstractBootOperation {
* @param jar Java archive file
* @return this operation instance
*/
public BootJarOperation bootInfLibs(File... jar) {
libJars_.addAll(List.of(jar));
return this;
@Override
public AbstractBootOperation infLibs(File... jar) {
return super.infLibs(jar);
}
/**
* Part of the {@link #execute} operation, configure the JAR launcher ({@code spring-boot-loader}) class.
*/
@Override
public BootJarOperation launcherClass(String className) {
return (BootJarOperation) super.launcherClass(className);
}
/**
* Part of the {@link #execute} operation, configure the launcher ({@code spring-boot-loader}) JAR(s).
*/
public BootJarOperation launcherJars(List<File> jars) throws IOException {
return (BootJarOperation) super.launcherJars(jars);
}
/**
* Provides the fully-qualified main class name.
*/
public BootJarOperation mainClass(String className) {
return (BootJarOperation) super.mainClass(className);
}
/**
* Provides an attribute to put in the JAR manifest.
*
* @param name the attribute name to put in the manifest
* @param value the attribute value to put in the manifest
* @return this operation instance
*/
@Override
public BootJarOperation manifestAttribute(String name, String value) {
return (BootJarOperation) super.manifestAttribute(name, value);
}
/**
* Provides a map of attributes to put in the jar manifest.
* <p>
* A copy will be created to allow this map to be independently modifiable.
*
* @param attributes the attributes to put in the manifest
* @return this operation instance
*/
@Override
public BootJarOperation manifestAttributes(Collection<BootManifestAttribute> attributes) {
return (BootJarOperation) super.manifestAttributes(attributes);
}
/**
* Provides the bld project.
*/
@Override
public BootJarOperation project(Project project) {
return (BootJarOperation) super.project(project);
}
/**
* Provides source directories that will be used for the jar archive creation.
*
* @param directories source directories
* @return this operation instance
*/
@Override
public BootJarOperation sourceDirectories(File... directories) {
return (BootJarOperation) super.sourceDirectories(directories);
}
/**
* Performs the BootJar operation.
*/
@Override
public void execute() throws Exception {
verifyExecute();
var staging_dir = Files.createTempDirectory("bootjar").toFile();
try {
var boot_inf_dir = executeCreateBootInfDirectory(staging_dir);
executeCopyInfClassesFiles(boot_inf_dir);
executeCopyInfLibs(boot_inf_dir);
executeCopyBootLoader(staging_dir);
executeCreateArchive(staging_dir, LOGGER);
if (!silent() && LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(String.format("The executable JAR (%s) was created in: %s%n", destinationArchiveFileName(),
destinationDirectory()));
}
} finally {
FileUtils.deleteDirectory(staging_dir);
}
}
/**
* Part of the {@link #execute} operation, creates the {@code BOOT-INF} staging directory.
*/
protected File executeCreateBootInfDirectory(File stagingDirectory) throws IOException {
var boot_inf = new File(stagingDirectory, "BOOT-INF");
mkDirs(boot_inf);
return boot_inf;
}
/**
* Configures the operation from a {@link Project}.
*/
public BootJarOperation fromProject(Project project) throws IOException {
project(project);
mainClass(project.mainClass());
return destinationDirectory(project.buildDistDirectory())
.destinationArchiveFileName(project.archiveBaseName() + "-" + project.version() + "-boot.jar")
.infLibs(project.compileClasspathJars())
.infLibs(project.runtimeClasspathJars())
.launcherClass("org.springframework.boot.loader.JarLauncher")
.launcherJars(project.standaloneClasspathJars())
.manifestAttributes(
List.of(
new BootManifestAttribute("Manifest-Version", "1.0"),
new BootManifestAttribute("Main-Class", launcherClass()),
new BootManifestAttribute("Start-Class", mainClass()))
)
.sourceDirectories(project.buildMainDirectory(), project.srcMainResourcesDirectory());
}
}

View file

@ -25,63 +25,156 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Builds and creates a Sprint Boot web archive (WAR).
* Builds and creates a Spring Boot web archive (WAR).
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class BootWarOperation extends AbstractBootOperation {
private static final Logger LOGGER = Logger.getLogger(BootWarOperation.class.getName());
private final List<File> webInfLibs_ = new ArrayList<>();
private final List<File> webInfProvidedLibs_ = new ArrayList<>();
/**
* Provides the destination file name that will be used for the archive creation.
*
* @param name the war archive destination file name
* @return this operation instance
*/
@Override
public BootWarOperation destinationArchiveFileName(String name) {
return (BootWarOperation) super.destinationArchiveFileName(name);
}
/**
* Provides the destination directory in which the archive will be created.
*
* @param directory the war destination directory
* @return this operation instance
*/
@Override
public BootWarOperation destinationDirectory(File directory) throws IOException {
return (BootWarOperation) super.destinationDirectory(directory);
}
/**
* Provides library JARs that will be used for the WAR creation.
*
* @param jars Java archive files
* @return this operation instance
*/
@Override
public BootWarOperation infLibs(List<File> jars) {
return (BootWarOperation) super.infLibs(jars);
}
/**
* Provides library JARs that will be used for the WAR creation.
*
* @param jar Java archive file
* @return this operation instance
*/
@Override
public BootWarOperation infLibs(File... jar) {
return (BootWarOperation) super.infLibs(jar);
}
/**
* Part of the {@link #execute} operation, configure the JAR launcher ({@code spring-boot-loader}) class.
*/
@Override
public BootWarOperation launcherClass(String className) {
return (BootWarOperation) super.launcherClass(className);
}
/**
* Part of the {@link #execute} operation, configure the launcher ({@code spring-boot-loader}) JAR(s).
*/
public BootWarOperation launcherJars(List<File> jars) throws IOException {
return (BootWarOperation) super.launcherJars(jars);
}
/**
* Provides the fully-qualified main class name.
*/
public BootWarOperation mainClass(String className) {
return (BootWarOperation) super.mainClass(className);
}
/**
* Provides an attribute to put in the JAR manifest.
*
* @param name the attribute name to put in the manifest
* @param value the attribute value to put in the manifest
* @return this operation instance
*/
@Override
public BootWarOperation manifestAttribute(String name, String value) {
return (BootWarOperation) super.manifestAttribute(name, value);
}
/**
* Provides a map of attributes to put in the jar manifest.
* <p>
* A copy will be created to allow this map to be independently modifiable.
*
* @param attributes the attributes to put in the manifest
* @return this operation instance
*/
@Override
public BootWarOperation manifestAttributes(Collection<BootManifestAttribute> attributes) {
return (BootWarOperation) super.manifestAttributes(attributes);
}
/**
* Provides the bld project.
*/
@Override
public BootWarOperation project(Project project) {
return (BootWarOperation) super.project(project);
}
/**
* Provides source directories that will be used for the jar archive creation.
*
* @param directories source directories
* @return this operation instance
*/
@Override
public BootWarOperation sourceDirectories(File... directories) {
return (BootWarOperation) super.sourceDirectories(directories);
}
/**
* Performs the BootJar operation.
*/
@Override
public void execute() throws Exception {
if (project_ == null) {
throw new IllegalArgumentException("ERROR: project required.");
} else if (project_.mainClass() == null) {
throw new IllegalArgumentException("ERROR: project mainClass required.");
}
verifyExecute();
var staging_dir = Files.createTempDirectory("bootwar").toFile();
try {
var boot_web_inf_dir = executeCreateWebInfDirectory(staging_dir);
executeCopyBootInfClassesFiles(boot_web_inf_dir);
executeCopyWebInfLib(boot_web_inf_dir);
executeCopyWebInfProvidedLib(boot_web_inf_dir);
var web_inf_dir = executeCreateWebInfDirectory(staging_dir);
executeCopyInfClassesFiles(web_inf_dir);
executeCopyInfLibs(web_inf_dir);
executeCopyWebInfProvidedLib(web_inf_dir);
executeCopyBootLoader(staging_dir);
executeCreateArchive(staging_dir, LOGGER);
if (!silent()) {
System.out.printf("The WAR (%s) was created in: %s%n", destinationArchiveFileName(),
destinationDirectory());
if (!silent() && LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(String.format("The executable WAR (%s) was created in: %s%n", destinationArchiveFileName(),
destinationDirectory()));
}
} finally {
FileUtils.deleteDirectory(staging_dir);
}
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} libs.
*/
protected void executeCopyWebInfLib(File stagingBootInfDirectory) throws IOException {
var boot_inf_lib_dir = new File(stagingBootInfDirectory, "lib");
mkDirs(boot_inf_lib_dir);
for (var jar : webInfLibs_) {
Files.copy(jar.toPath(), boot_inf_lib_dir.toPath().resolve(jar.getName()));
}
}
/**
* Part of the {@link #execute} operation, copy the {@code BOOT-INF} libs.
*/
@ -94,50 +187,41 @@ public class BootWarOperation extends AbstractBootOperation {
}
}
/**
* Part of the {@link #execute} operation, creates the {@code WEB-INF} staging directory.
*/
protected File executeCreateWebInfDirectory(File stagingDirectory) throws IOException {
var boot_inf = new File(stagingDirectory, "WEB-INF");
mkDirs(boot_inf);
return boot_inf;
}
/**
* Configures the operation from a {@link Project}.
*
* @param project the project to configure the operation from
*/
public AbstractBootOperation fromProject(Project project) throws IOException {
project_ = project;
return webInfLibs(project.compileClasspathJars())
.webInfLibs(project.runtimeClasspathJars())
.webInfLibs(project.buildDistDirectory())
public BootWarOperation fromProject(Project project) throws IOException {
project(project);
mainClass(project.mainClass());
return destinationDirectory(project.buildDistDirectory())
.destinationArchiveFileName(project.archiveBaseName() + "-" + project.version() + "-boot.war")
.infLibs(project.compileClasspathJars())
.infLibs(project.runtimeClasspathJars())
.infLibs(project.buildDistDirectory())
// TODO add provided libs
.launcherClass("org.springframework.boot.loader.WarLauncher")
.launcherJars(project.standaloneClasspathJars())
.manifestAttributes(
List.of(
new BootManifestAttribute("Manifest-Version", "1.0"),
new BootManifestAttribute("Main-Class", launcherClass()),
new BootManifestAttribute("Start-Class", project.mainClass())
new BootManifestAttribute("Start-Class", mainClass())
))
.destinationDirectory(project.buildDistDirectory())
.destinationArchiveFileName(project.archiveBaseName() + "-" + project.version() + "-boot.war")
.sourceDirectories(project.buildMainDirectory(), project.srcMainResourcesDirectory());
}
/**
* Provides library JARs that will be used for the WAR creation.
*
* @param jars Java archive files
* @return this operation instance
*/
public BootWarOperation webInfLibs(Collection<File> jars) {
webInfLibs_.addAll(jars);
return this;
}
/**
* Provides library JARs that will be used for the WAR creation.
*
* @param jar Java archive file
* @return this operation instance
*/
public BootWarOperation webInfLibs(File... jar) {
webInfLibs_.addAll(List.of(jar));
return this;
}
/**
* Provides library JARs that will be used for the WAR creation in {@code /WEB-INF/lib-provided}.