From c6c9d32c3536c4e63f778a3f67af2b090b5b57df Mon Sep 17 00:00:00 2001 From: Geert Bevin Date: Mon, 14 Oct 2024 09:07:24 -0400 Subject: [PATCH] Added support for automatically consolidating compile options that are named differently but serve the same purpose --- .../rife/bld/operations/CompileOperation.java | 37 +++++++++++++++++-- .../rife/bld/operations/JavacOptions.java | 4 +- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/rife/bld/operations/CompileOperation.java b/src/main/java/rife/bld/operations/CompileOperation.java index 6b600cf..03261ca 100644 --- a/src/main/java/rife/bld/operations/CompileOperation.java +++ b/src/main/java/rife/bld/operations/CompileOperation.java @@ -23,6 +23,13 @@ * @since 1.5 */ public class CompileOperation extends AbstractOperation { + static final String COMPILE_OPTION_D = "-d"; + static final String COMPILE_OPTION_CP = "-cp"; + static final String COMPILE_OPTION_CLASS_PATH = "--class-path"; + static final String COMPILE_OPTION_CLASSPATH = "--classpath"; + static final String COMPILE_OPTION_P = "-p"; + static final String COMPILE_OPTION_MODULE_PATH = "--module-path"; + private File buildMainDirectory_; private File buildTestDirectory_; private final List compileMainClasspath_ = new ArrayList<>(); @@ -124,19 +131,33 @@ protected void executeBuildSources(List classpath, List modulePa try (var file_manager = compiler.getStandardFileManager(null, null, null)) { var compilation_units = file_manager.getJavaFileObjectsFromFiles(sources); var diagnostics = new DiagnosticCollector(); - var options = new ArrayList<>(List.of("-d", destination.getAbsolutePath())); + var options = new ArrayList<>(List.of(COMPILE_OPTION_D, destination.getAbsolutePath())); + if (!classpath.isEmpty()) { - options.addAll(List.of("-cp", FileUtils.joinPaths(classpath))); + var class_path = FileUtils.joinPaths(classpath); + class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CP); + class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CLASS_PATH); + class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CLASSPATH); + + options.addAll(List.of(COMPILE_OPTION_CP, class_path)); } + if (!modulePath.isEmpty()) { - options.addAll(List.of("-p", FileUtils.joinPaths(modulePath))); + var module_path = FileUtils.joinPaths(modulePath); + module_path = removeAndAppendCompileOptionPath(module_path, COMPILE_OPTION_P); + module_path = removeAndAppendCompileOptionPath(module_path, COMPILE_OPTION_MODULE_PATH); + + options.addAll(List.of(COMPILE_OPTION_P, module_path)); } + options.addAll(compileOptions()); + var compilation_task = compiler.getTask(null, file_manager, diagnostics, options, null, compilation_units); if (!compilation_task.call()) { diagnostics_.addAll(diagnostics.getDiagnostics()); executeProcessDiagnostics(diagnostics); } + var module_info_class = new File(destination, "module-info.class"); if (module_info_class.exists() && moduleMainClass() != null) { var orig_bytes = FileUtils.readBytes(module_info_class); @@ -146,6 +167,16 @@ protected void executeBuildSources(List classpath, List modulePa } } + private String removeAndAppendCompileOptionPath(String basePath, String option) { + var index = compileOptions_.indexOf(option); + if (index != -1 && index + 1 < compileOptions_.size() - 1) { + compileOptions_.remove(index); + return basePath + File.pathSeparator + compileOptions_.remove(index); + } + + return basePath; + } + /** * Part of the {@link #execute} operation, processes the compilation diagnostics. * diff --git a/src/main/java/rife/bld/operations/JavacOptions.java b/src/main/java/rife/bld/operations/JavacOptions.java index 3b2b1bf..cd2d752 100644 --- a/src/main/java/rife/bld/operations/JavacOptions.java +++ b/src/main/java/rife/bld/operations/JavacOptions.java @@ -14,6 +14,8 @@ import java.util.Arrays; import java.util.List; +import static rife.bld.operations.CompileOperation.COMPILE_OPTION_MODULE_PATH; + /** * Options for the standard javac tool. * @@ -410,7 +412,7 @@ public JavacOptions modulePath(String... paths) { * @since 2.1 */ public JavacOptions modulePathStrings(List paths) { - add("--module-path"); + add(COMPILE_OPTION_MODULE_PATH); add(FileUtils.joinPaths(paths)); return this; }