Made AbstractBootOperation more generic

This commit is contained in:
Erik C. Thauvin 2023-10-29 16:09:47 -07:00
parent b4282b2b73
commit 700bf84b34
5 changed files with 39 additions and 285 deletions

View file

@ -39,12 +39,8 @@ public class DemoApplicationBuild extends WebProject {
} }
public static void main(String[] args) { public static void main(String[] args) {
var level = Level.FINER; var logger = Logger.getLogger("rife.bld.extension");
var consoleHandler = new ConsoleHandler(); logger.setLevel(Level.FINER);
consoleHandler.setLevel(level);
var logger = Logger.getLogger(BootJarOperation.class.getName());
logger.addHandler(consoleHandler);
logger.setLevel(level);
new DemoApplicationBuild().start(args); new DemoApplicationBuild().start(args);
} }

View file

@ -16,7 +16,6 @@
package rife.bld.extension; package rife.bld.extension;
import rife.bld.Project;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import rife.tools.FileUtils; import rife.tools.FileUtils;
import rife.tools.exceptions.FileUtilsErrorException; import rife.tools.exceptions.FileUtilsErrorException;
@ -35,13 +34,14 @@ import java.util.logging.Logger;
import java.util.spi.ToolProvider; import java.util.spi.ToolProvider;
/** /**
* Implements commons methods used by Spring Boot operations, such as {@link BootJarOperation} and * Implements common methods used by Spring Boot operations, such as {@link BootJarOperation} and
* {@link BootWarOperation}. * {@link BootWarOperation}.
* *
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
public abstract class AbstractBootOperation extends AbstractOperation<AbstractBootOperation> { public abstract class AbstractBootOperation<T extends AbstractBootOperation<T>>
extends AbstractOperation<AbstractBootOperation<T>> {
private final List<File> infLibs_ = new ArrayList<>(); private final List<File> infLibs_ = new ArrayList<>();
private final List<File> launcherJars_ = new ArrayList<>(); private final List<File> launcherJars_ = new ArrayList<>();
private final List<BootManifestAttribute> manifestAttributes_ = new ArrayList<>(); private final List<BootManifestAttribute> manifestAttributes_ = new ArrayList<>();
@ -50,7 +50,6 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
private File destinationDirectory_; private File destinationDirectory_;
private String launcherClass_; private String launcherClass_;
private String mainClass_; private String mainClass_;
private Project project_;
public void deleteDirectories(File... directory) throws FileUtilsErrorException { public void deleteDirectories(File... directory) throws FileUtilsErrorException {
for (var d : directory) { for (var d : directory) {
@ -66,9 +65,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param name the archive file name * @param name the archive file name
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation destinationArchiveFileName(String name) { public T destinationArchiveFileName(String name) {
destinationArchiveFileName = name; destinationArchiveFileName = name;
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -95,10 +95,11 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param directory the destination directory * @param directory the destination directory
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation destinationDirectory(File directory) throws IOException { public T destinationDirectory(File directory) throws IOException {
destinationDirectory_ = directory; destinationDirectory_ = directory;
mkDirs(destinationDirectory_); mkDirs(destinationDirectory_);
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -209,9 +210,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param jars Java archive files * @param jars Java archive files
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation infLibs(List<File> jars) { public T infLibs(List<File> jars) {
infLibs_.addAll(jars); infLibs_.addAll(jars);
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -220,9 +222,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param jar Java archive file * @param jar Java archive file
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation infLibs(File... jar) { public T infLibs(File... jar) {
infLibs_.addAll(List.of(jar)); infLibs_.addAll(List.of(jar));
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -235,9 +238,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
/** /**
* Part of the {@link #execute} operation, configure the JAR launcher ({@code spring-boot-loader}) class name. * Part of the {@link #execute} operation, configure the JAR launcher ({@code spring-boot-loader}) class name.
*/ */
public AbstractBootOperation launcherClass(String className) { public T launcherClass(String className) {
launcherClass_ = className; launcherClass_ = className;
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -261,7 +265,7 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
/** /**
* Part of the {@link #execute} operation, configure the launcher ({@code spring-boot-loader}) JAR location. * Part of the {@link #execute} operation, configure the launcher ({@code spring-boot-loader}) JAR location.
*/ */
public AbstractBootOperation launcherJars(List<File> jars) throws IOException { public T launcherJars(List<File> jars) throws IOException {
if (!jars.isEmpty()) { if (!jars.isEmpty()) {
for (var j : jars) { for (var j : jars) {
if (!j.exists()) { if (!j.exists()) {
@ -270,15 +274,17 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
} }
launcherJars_.addAll(jars); launcherJars_.addAll(jars);
} }
return this; //noinspection unchecked
return (T) this;
} }
/** /**
* Provides the fully-qualified main class name. * Provides the fully-qualified main class name.
*/ */
protected AbstractBootOperation mainClass(String className) { protected T mainClass(String className) {
mainClass_ = className; mainClass_ = className;
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -295,9 +301,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param value the attribute value to put in the manifest * @param value the attribute value to put in the manifest
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation manifestAttribute(String name, String value) { public T manifestAttribute(String name, String value) {
manifestAttributes_.add(new BootManifestAttribute(name, value)); manifestAttributes_.add(new BootManifestAttribute(name, value));
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -315,9 +322,10 @@ public abstract class AbstractBootOperation extends AbstractOperation<AbstractBo
* @param attributes the attributes to put in the manifest * @param attributes the attributes to put in the manifest
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation manifestAttributes(Collection<BootManifestAttribute> attributes) { public T manifestAttributes(Collection<BootManifestAttribute> attributes) {
manifestAttributes_.addAll(attributes); manifestAttributes_.addAll(attributes);
return this; //noinspection unchecked
return (T) this;
} }
/** /**
@ -329,30 +337,16 @@ 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. * Provides source directories that will be used for the jar archive creation.
* *
* @param directories source directories * @param directories source directories
* @return this operation instance * @return this operation instance
*/ */
public AbstractBootOperation sourceDirectories(File... directories) { public T sourceDirectories(File... directories) {
sourceDirectories_.addAll(List.of(directories)); sourceDirectories_.addAll(List.of(directories));
return this; //noinspection unchecked
return (T) this;
} }
/** /**

View file

@ -22,7 +22,6 @@ import rife.tools.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -33,121 +32,9 @@ import java.util.logging.Logger;
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
public class BootJarOperation extends AbstractBootOperation { public class BootJarOperation extends AbstractBootOperation<BootJarOperation> {
private static final Logger LOGGER = Logger.getLogger(BootJarOperation.class.getName()); private static final Logger LOGGER = Logger.getLogger(BootJarOperation.class.getName());
/**
* Provides the destination file name that will be used for the JAR creation.
*
* @param name the JAR destination file name
* @return this operation instance
*/
@Override
public BootJarOperation destinationArchiveFileName(String name) {
return (BootJarOperation) super.destinationArchiveFileName(name);
}
/**
* Provides the destination directory in which the JAR will be created.
*
* @param directory the JAR destination directory
* @return this operation instance
*/
@Override
public BootJarOperation destinationDirectory(File directory) throws IOException {
return (BootJarOperation) super.destinationDirectory(directory);
}
/**
* Provides JAR libraries that will be used for the JAR creation.
*
* @param jars Java archive files
* @return this operation instance
*/
@Override
public BootJarOperation infLibs(List<File> jars) {
return (BootJarOperation) super.infLibs(jars);
}
/**
* Provides JAR libraries that will be used for the JAR creation.
*
* @param jar Java archive file
* @return this operation instance
*/
@Override
public BootJarOperation infLibs(File... jar) {
return (BootJarOperation) 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).
*/
@Override
public BootJarOperation launcherJars(List<File> jars) throws IOException {
return (BootJarOperation) super.launcherJars(jars);
}
/**
* Provides the fully-qualified main class name.
*/
@Override
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. * Performs the BootJar operation.
*/ */
@ -174,7 +61,6 @@ public class BootJarOperation extends AbstractBootOperation {
} }
} }
/** /**
* Part of the {@link #execute} operation, creates the {@code BOOT-INF} staging directory. * Part of the {@link #execute} operation, creates the {@code BOOT-INF} staging directory.
*/ */
@ -188,7 +74,6 @@ public class BootJarOperation extends AbstractBootOperation {
* Configures the operation from a {@link Project}. * Configures the operation from a {@link Project}.
*/ */
public BootJarOperation fromProject(Project project) throws IOException { public BootJarOperation fromProject(Project project) throws IOException {
project(project);
mainClass(project.mainClass()); mainClass(project.mainClass());
return destinationDirectory(project.buildDistDirectory()) return destinationDirectory(project.buildDistDirectory())

View file

@ -34,122 +34,10 @@ import java.util.logging.Logger;
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
public class BootWarOperation extends AbstractBootOperation { public class BootWarOperation extends AbstractBootOperation<BootWarOperation> {
private static final Logger LOGGER = Logger.getLogger(BootWarOperation.class.getName()); private static final Logger LOGGER = Logger.getLogger(BootWarOperation.class.getName());
private final List<File> webInfProvidedLibs_ = new ArrayList<>(); private final List<File> webInfProvidedLibs_ = new ArrayList<>();
/**
* Provides the destination file name that will be used for the WAR 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 WAR 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 JAR libraries 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 JAR libraries 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).
*/
@Override
public BootWarOperation launcherJars(List<File> jars) throws IOException {
return (BootWarOperation) super.launcherJars(jars);
}
/**
* Provides the fully-qualified main class name.
*/
@Override
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. * Performs the BootJar operation.
*/ */
@ -204,7 +92,6 @@ public class BootWarOperation extends AbstractBootOperation {
* @param project the project to configure the operation from * @param project the project to configure the operation from
*/ */
public BootWarOperation fromProject(Project project) throws IOException { public BootWarOperation fromProject(Project project) throws IOException {
project(project);
mainClass(project.mainClass()); mainClass(project.mainClass());
return destinationDirectory(project.buildDistDirectory()) return destinationDirectory(project.buildDistDirectory())
@ -224,7 +111,6 @@ public class BootWarOperation extends AbstractBootOperation {
.sourceDirectories(project.buildMainDirectory(), project.srcMainResourcesDirectory()); .sourceDirectories(project.buildMainDirectory(), project.srcMainResourcesDirectory());
} }
/** /**
* Provides JAR libraries that will be used for the WAR creation in {@code /WEB-INF/lib-provided}. * Provides JAR libraries that will be used for the WAR creation in {@code /WEB-INF/lib-provided}.
* *

View file

@ -177,9 +177,7 @@ class BootJarOperationTest {
"BOOT-INF/classes/rife/\n" + "BOOT-INF/classes/rife/\n" +
"BOOT-INF/classes/rife/bld/\n" + "BOOT-INF/classes/rife/bld/\n" +
"BOOT-INF/classes/rife/bld/extension/\n" + "BOOT-INF/classes/rife/bld/extension/\n" +
"BOOT-INF/classes/rife/bld/extension/AbstractBootOperation$ManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootJarOperation$ManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + "BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" +
@ -187,7 +185,7 @@ class BootJarOperationTest {
"BOOT-INF/lib/" + SPRING_BOOT + '\n' + "BOOT-INF/lib/" + SPRING_BOOT + '\n' +
"BOOT-INF/lib/" + SPRING_BOOT_ACTUATOR + '\n' + "BOOT-INF/lib/" + SPRING_BOOT_ACTUATOR + '\n' +
"META-INF/\n" + "META-INF/\n" +
"META-INF/MANIFEST.MF" + LAUNCHER_JARS); "META-INF/MANIFEST.MF\n" + LAUNCHER_JARS);
FileUtils.deleteDirectory(tmp_dir); FileUtils.deleteDirectory(tmp_dir);
} }
@ -213,9 +211,7 @@ class BootJarOperationTest {
"BOOT-INF/classes/rife/\n" + "BOOT-INF/classes/rife/\n" +
"BOOT-INF/classes/rife/bld/\n" + "BOOT-INF/classes/rife/bld/\n" +
"BOOT-INF/classes/rife/bld/extension/\n" + "BOOT-INF/classes/rife/bld/extension/\n" +
"BOOT-INF/classes/rife/bld/extension/AbstractBootOperation$ManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootJarOperation$ManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + "BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" +
"BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" +
@ -224,7 +220,7 @@ class BootJarOperationTest {
"BOOT-INF/lib/" + SPRING_BOOT + '\n' + "BOOT-INF/lib/" + SPRING_BOOT + '\n' +
"BOOT-INF/lib/" + SPRING_BOOT_ACTUATOR + '\n' + "BOOT-INF/lib/" + SPRING_BOOT_ACTUATOR + '\n' +
"META-INF/\n" + "META-INF/\n" +
"META-INF/MANIFEST.MF" + LAUNCHER_JARS); "META-INF/MANIFEST.MF\n" + LAUNCHER_JARS);
FileUtils.deleteDirectory(tmp_dir); FileUtils.deleteDirectory(tmp_dir);
} }
@ -235,7 +231,6 @@ class BootJarOperationTest {
var project = new CustomProject(tmp_dir); var project = new CustomProject(tmp_dir);
var bootJar = new BootJarOperation().fromProject(project); var bootJar = new BootJarOperation().fromProject(project);
assertThat(bootJar.project()).as("project").isEqualTo(project);
assertThat(bootJar.mainClass()).as("mainClass").isEqualTo(MAIN_CLASS); assertThat(bootJar.mainClass()).as("mainClass").isEqualTo(MAIN_CLASS);
assertThat(bootJar.sourceDirectories()).as("sourceDirectories.size").hasSize(2); assertThat(bootJar.sourceDirectories()).as("sourceDirectories.size").hasSize(2);
assertThat(bootJar.manifestAttributes()).as("manifestAttributes.size").hasSize(3); assertThat(bootJar.manifestAttributes()).as("manifestAttributes.size").hasSize(3);
@ -280,9 +275,7 @@ class BootJarOperationTest {
"WEB-INF/classes/rife/\n" + "WEB-INF/classes/rife/\n" +
"WEB-INF/classes/rife/bld/\n" + "WEB-INF/classes/rife/bld/\n" +
"WEB-INF/classes/rife/bld/extension/\n" + "WEB-INF/classes/rife/bld/extension/\n" +
"WEB-INF/classes/rife/bld/extension/AbstractBootOperation$ManifestAttribute.class\n" +
"WEB-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "WEB-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" +
"WEB-INF/classes/rife/bld/extension/BootJarOperation$ManifestAttribute.class\n" +
"WEB-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "WEB-INF/classes/rife/bld/extension/BootJarOperation.class\n" +
"WEB-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + "WEB-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" +
"WEB-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "WEB-INF/classes/rife/bld/extension/BootWarOperation.class\n" +