From a0fe6cf1539d0dcef071546867073b206ca04bb4 Mon Sep 17 00:00:00 2001 From: Tim Carter Date: Wed, 8 May 2024 15:37:54 +1000 Subject: [PATCH] Generate javadoc JARs. --- .../java/dev/sashimono/builder/Sashimono.java | 13 +++ .../builder/compiler/JavaCompiler.java | 98 +++++-------------- .../builder/compiler/JavaCompilerTask.java | 17 ++-- .../documenter/DocumentationResult.java | 11 +++ .../builder/documenter/JavaDocumenter.java | 68 +++++++++++++ .../documenter/JavaDocumenterTask.java | 25 +++++ .../builder/jar/AbstractJarTask.java | 13 +-- .../dev/sashimono/builder/jar/JarTask.java | 7 ++ .../sashimono/builder/jar/JavadocJarTask.java | 39 ++++++++ .../sashimono/builder/jar/SourcesJarTask.java | 9 +- .../builder/tool/AbstractJavaTool.java | 85 ++++++++++++++++ .../builder/tool/AbstractJavaToolTask.java | 13 +++ .../complete/BuildProjectTestCase.java | 29 +++++- 13 files changed, 339 insertions(+), 88 deletions(-) create mode 100644 builder/src/main/java/dev/sashimono/builder/documenter/DocumentationResult.java create mode 100644 builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenter.java create mode 100644 builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenterTask.java create mode 100644 builder/src/main/java/dev/sashimono/builder/jar/JavadocJarTask.java create mode 100644 builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaTool.java create mode 100644 builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaToolTask.java diff --git a/builder/src/main/java/dev/sashimono/builder/Sashimono.java b/builder/src/main/java/dev/sashimono/builder/Sashimono.java index 4822f4a..d641647 100644 --- a/builder/src/main/java/dev/sashimono/builder/Sashimono.java +++ b/builder/src/main/java/dev/sashimono/builder/Sashimono.java @@ -18,10 +18,13 @@ import dev.sashimono.builder.config.RepositoryConfig; import dev.sashimono.builder.dependencies.DownloadDependencyTask; import dev.sashimono.builder.dependencies.ResolvedDependency; +import dev.sashimono.builder.documenter.DocumentationResult; +import dev.sashimono.builder.documenter.JavaDocumenterTask; import dev.sashimono.builder.jar.DigestTask; import dev.sashimono.builder.jar.FileOutput; import dev.sashimono.builder.jar.JarResult; import dev.sashimono.builder.jar.JarTask; +import dev.sashimono.builder.jar.JavadocJarTask; import dev.sashimono.builder.jar.PomTask; import dev.sashimono.builder.jar.SourcesJarTask; import dev.sashimono.builder.util.Task; @@ -52,6 +55,7 @@ public void buildProject() { Task digestTask = runner.newTask(Void.class, new DigestTask()); Map> pomTasks = new HashMap<>(); Map> sourcesJarTasks = new HashMap<>(); + Map> javadocJarTasks = new HashMap<>(); //first we need to figure out what we are building locally, so we don't try and download it for (var m : config.moduleConfigs()) { if (m.packaging().equals(JAR)) { @@ -65,6 +69,9 @@ public void buildProject() { Task sourcesJarTask = runner.newTask(FileOutput.class, new SourcesJarTask(outputDir, m.gav(), m.filteredResourcesDir(), m.sourceDirectories())); sourcesJarTasks.put(m.gav(), sourcesJarTask); + Task javadocJarTask = runner.newTask(FileOutput.class, + new JavadocJarTask(outputDir, m.gav(), m.filteredResourcesDir())); + javadocJarTasks.put(m.gav(), javadocJarTask); } } for (var m : config.moduleConfigs()) { @@ -92,8 +99,11 @@ public void buildProject() { if (m.packaging().equals(JAR)) { Task compileTask = runner.newTask(CompileResult.class, new JavaCompilerTask(m.sourceDirectories())); + Task documentationTask = runner.newTask(DocumentationResult.class, + new JavaDocumenterTask(m.sourceDirectories())); for (var i : moduleDependencies) { compileTask.addDependency(i); + documentationTask.addDependency(i); } Task jarTask = jarTasks.get(m.gav()); jarTask.addDependency(compileTask); @@ -102,6 +112,9 @@ public void buildProject() { digestTask.addDependency(pomTask); Task sourcesJarTask = sourcesJarTasks.get(m.gav()); digestTask.addDependency(sourcesJarTask); + Task javadocJarTask = javadocJarTasks.get(m.gav()); + javadocJarTask.addDependency(documentationTask); + digestTask.addDependency(javadocJarTask); } } runner.run(); diff --git a/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompiler.java b/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompiler.java index 47143e0..3dd3b3d 100644 --- a/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompiler.java +++ b/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompiler.java @@ -2,107 +2,63 @@ import java.io.File; import java.io.IOException; -import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.Locale; -import java.util.stream.Collectors; -import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; -import javax.tools.DiagnosticListener; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; import javax.tools.ToolProvider; +import dev.sashimono.builder.tool.AbstractJavaTool; import dev.sashimono.builder.util.Log; -public class JavaCompiler { +public class JavaCompiler extends AbstractJavaTool { - static final Log log = Log.of(JavaCompiler.class); + private static final Log log = Log.of(JavaCompiler.class); - private final javax.tools.JavaCompiler compiler; - private final List compilerFlags; - private final List dependencies; - private final List sourceDirectories; - - public static JavaCompiler build(List dependencies, List sourceDirectories) { - return new JavaCompiler(ToolProvider.getSystemJavaCompiler(), List.of(), dependencies, sourceDirectories); - } - - JavaCompiler(javax.tools.JavaCompiler compiler, List compilerFlags, List dependencies, - List sourceDirectories) { - this.compiler = compiler; - this.compilerFlags = compilerFlags; - this.dependencies = dependencies; - this.sourceDirectories = sourceDirectories; + public JavaCompiler(final javax.tools.JavaCompiler compiler, final List flags, final List dependencies, + final List sourceDirectories) { + super(compiler, flags, dependencies, sourceDirectories); if (compiler == null) { throw new RuntimeException("No system java compiler provided"); } } - public Path compile() { - - StandardJavaFileManager fileManager = compiler.getStandardFileManager((DiagnosticListener) null, (Locale) null, - StandardCharsets.UTF_8); + @Override + protected Log getLogger() { + return log; + } - List sourceFiles = new ArrayList<>(); - sourceDirectories.forEach(s -> { - try { - Files.walkFileTree(s, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.getFileName().toString().endsWith(".java")) { - sourceFiles.add(file.toFile()); - } - return FileVisitResult.CONTINUE; - } + public static JavaCompiler build(final List dependencies, final List sourceDirectories) { + return new JavaCompiler(ToolProvider.getSystemJavaCompiler(), List.of(), dependencies, sourceDirectories); + } - }); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - //we sort the source files to help with reproducibility - sourceFiles.sort(Comparator.comparing(Object::toString)); + @Override + public Path process() { + final javax.tools.JavaCompiler compiler = (javax.tools.JavaCompiler) tool; + final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, + StandardCharsets.UTF_8); + final List sourceFiles = collectSourceFiles(); try { - var output = Files.createTempDirectory("output"); - fileManager.setLocation(StandardLocation.CLASS_PATH, - dependencies.stream().map(Path::toFile).collect(Collectors.toSet())); - fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(output.toFile())); - - DiagnosticCollector diagnosticsCollector = new DiagnosticCollector(); - var sources = fileManager + final Path output = configureFileManager(fileManager, StandardLocation.CLASS_OUTPUT); + final DiagnosticCollector diagnosticsCollector = new DiagnosticCollector<>(); + final var sources = fileManager .getJavaFileObjectsFromFiles(sourceFiles); - javax.tools.JavaCompiler.CompilationTask task = this.compiler.getTask((Writer) null, fileManager, + final javax.tools.JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnosticsCollector, - this.compilerFlags, (Iterable) null, sources); - boolean compilationTaskSucceed = task.call(); - - for (Diagnostic diagnostic : diagnosticsCollector.getDiagnostics()) { - if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { - log.error(diagnostic.getMessage(Locale.getDefault())); - } else { - log.info(diagnostic.getMessage(Locale.getDefault())); - } - } - + this.flags, null, sources); + final boolean compilationTaskSucceed = task.call(); + processDiagnostics(diagnosticsCollector); if (!compilationTaskSucceed) { throw new RuntimeException("compilation failed"); } log.infof("Compiled classes to %s", output); return output; - } catch (IOException e) { + } catch (final IOException e) { throw new RuntimeException("Cannot initialize file manager", e); } } - } diff --git a/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompilerTask.java b/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompilerTask.java index f21e2ad..6a1935a 100644 --- a/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompilerTask.java +++ b/builder/src/main/java/dev/sashimono/builder/compiler/JavaCompilerTask.java @@ -5,22 +5,21 @@ import java.util.function.Function; import dev.sashimono.builder.dependencies.ResolvedDependency; +import dev.sashimono.builder.tool.AbstractJavaToolTask; import dev.sashimono.builder.util.TaskMap; -public class JavaCompilerTask implements Function { +public class JavaCompilerTask extends AbstractJavaToolTask implements Function { - private final List sourceDirectories; - - public JavaCompilerTask(List sourceDirectories) { - this.sourceDirectories = sourceDirectories; + public JavaCompilerTask(final List sourceDirectories) { + super(sourceDirectories); } @Override - public CompileResult apply(TaskMap taskMap) { + public CompileResult apply(final TaskMap taskMap) { //grab both the downloaded and compiled dependencies - var deps = taskMap.results(ResolvedDependency.class).stream().map(ResolvedDependency::path).toList(); - var compiler = JavaCompiler.build(deps, sourceDirectories); + final List deps = taskMap.results(ResolvedDependency.class).stream().map(ResolvedDependency::path).toList(); + final JavaCompiler compiler = JavaCompiler.build(deps, sourceDirectories); - return new CompileResult(compiler.compile()); + return new CompileResult(compiler.process()); } } diff --git a/builder/src/main/java/dev/sashimono/builder/documenter/DocumentationResult.java b/builder/src/main/java/dev/sashimono/builder/documenter/DocumentationResult.java new file mode 100644 index 0000000..1d61e52 --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/documenter/DocumentationResult.java @@ -0,0 +1,11 @@ +package dev.sashimono.builder.documenter; + +import java.nio.file.Path; + +/** + * The results of documenting, basically just a temp directory with javadoc files + * + * @param documentationDirectory + */ +public record DocumentationResult(Path documentationDirectory) { +} diff --git a/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenter.java b/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenter.java new file mode 100644 index 0000000..1d02b3d --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenter.java @@ -0,0 +1,68 @@ +package dev.sashimono.builder.documenter; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.List; + +import javax.tools.DiagnosticCollector; +import javax.tools.DocumentationTool; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import dev.sashimono.builder.compiler.JavaCompiler; +import dev.sashimono.builder.tool.AbstractJavaTool; +import dev.sashimono.builder.util.Log; + +public class JavaDocumenter extends AbstractJavaTool { + + private static final Log log = Log.of(JavaCompiler.class); + + public JavaDocumenter(final javax.tools.DocumentationTool documenter, final List flags, + final List dependencies, final List sourceDirectories) { + super(documenter, flags, dependencies, sourceDirectories); + if (documenter == null) { + throw new RuntimeException("No system java documenter provided"); + } + } + + @Override + protected Log getLogger() { + return log; + } + + public static JavaDocumenter build(final List dependencies, final List sourceDirectories) { + // '-notimestamp' prevents timestamp from being written to .html files, ensures that hashes are identical between builds + return new JavaDocumenter(ToolProvider.getSystemDocumentationTool(), List.of("-notimestamp"), dependencies, + sourceDirectories); + } + + @Override + public Path process() { + final javax.tools.DocumentationTool documenter = (DocumentationTool) tool; + final StandardJavaFileManager fileManager = documenter.getStandardFileManager(null, null, + StandardCharsets.UTF_8); + final List sourceFiles = collectSourceFiles(); + try { + final Path output = configureFileManager(fileManager, DocumentationTool.Location.DOCUMENTATION_OUTPUT); + final DiagnosticCollector diagnosticsCollector = new DiagnosticCollector<>(); + final Iterable sources = fileManager + .getJavaFileObjectsFromFiles(sourceFiles); + final DocumentationTool.DocumentationTask task = documenter.getTask(null, fileManager, + diagnosticsCollector, + null, this.flags, sources); + final boolean documentationTaskSucceeded = task.call(); + processDiagnostics(diagnosticsCollector); + if (!documentationTaskSucceeded) { + throw new RuntimeException("documenting failed"); + } + log.infof("Documented classes to %s", output); + return output; + } catch (final IOException e) { + throw new RuntimeException("Cannot initialize file manager", e); + } + } + +} diff --git a/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenterTask.java b/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenterTask.java new file mode 100644 index 0000000..5d3ffe6 --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/documenter/JavaDocumenterTask.java @@ -0,0 +1,25 @@ +package dev.sashimono.builder.documenter; + +import java.nio.file.Path; +import java.util.List; +import java.util.function.Function; + +import dev.sashimono.builder.dependencies.ResolvedDependency; +import dev.sashimono.builder.tool.AbstractJavaToolTask; +import dev.sashimono.builder.util.TaskMap; + +public class JavaDocumenterTask extends AbstractJavaToolTask implements Function { + + public JavaDocumenterTask(final List sourceDirectories) { + super(sourceDirectories); + } + + @Override + public DocumentationResult apply(final TaskMap taskMap) { + //grab both the downloaded and compiled dependencies + final List deps = taskMap.results(ResolvedDependency.class).stream().map(ResolvedDependency::path).toList(); + final JavaDocumenter documenter = JavaDocumenter.build(deps, sourceDirectories); + + return new DocumentationResult(documenter.process()); + } +} diff --git a/builder/src/main/java/dev/sashimono/builder/jar/AbstractJarTask.java b/builder/src/main/java/dev/sashimono/builder/jar/AbstractJarTask.java index 463c4e2..3ca48af 100644 --- a/builder/src/main/java/dev/sashimono/builder/jar/AbstractJarTask.java +++ b/builder/src/main/java/dev/sashimono/builder/jar/AbstractJarTask.java @@ -15,14 +15,13 @@ import dev.sashimono.builder.util.FileUtil; import dev.sashimono.builder.util.Log; -public class AbstractJarTask { +public abstract class AbstractJarTask { - private static final Log log = Log.of(AbstractJarTask.class); public static final String DELIMITER = "-"; protected final Path outputDir; protected final GAV gav; protected final Path filteredResourcesDir; - protected final Map> toJar = new TreeMap<>(); + protected final Map> toJarFilesByDir = new TreeMap<>(); public AbstractJarTask(final Path outputDir, final GAV gav, final Path filteredResourcesDir) { this.outputDir = outputDir; @@ -30,6 +29,8 @@ public AbstractJarTask(final Path outputDir, final GAV gav, final Path filteredR this.filteredResourcesDir = filteredResourcesDir; } + protected abstract Log getLogger(); + protected Path createJar() throws IOException { return createJar(""); } @@ -47,7 +48,7 @@ protected Path createJar(final String suffix) throws IOException { artifactName += ".jar"; final Path target = parentDir.resolve(artifactName); try (final JarOutputStream out = new JarOutputStream(Files.newOutputStream(target))) { - toJar.forEach((dir, files) -> { + toJarFilesByDir.forEach((dir, files) -> { try { files.sort(Comparator.comparing(Object::toString)); for (final Path file : files) { @@ -65,13 +66,13 @@ protected Path createJar(final String suffix) throws IOException { } }); } - log.infof("created jar %s", target); + getLogger().infof("created jar %s", target); return target; } protected void collectFiles(final Path... dirs) throws IOException { for (final Path dir : dirs) { - toJar.put(dir, FileUtil.collectFiles(dir)); + toJarFilesByDir.put(dir, FileUtil.collectFiles(dir)); } } } diff --git a/builder/src/main/java/dev/sashimono/builder/jar/JarTask.java b/builder/src/main/java/dev/sashimono/builder/jar/JarTask.java index 8f2deab..0af4e14 100644 --- a/builder/src/main/java/dev/sashimono/builder/jar/JarTask.java +++ b/builder/src/main/java/dev/sashimono/builder/jar/JarTask.java @@ -13,6 +13,7 @@ import dev.sashimono.builder.config.Dependency; import dev.sashimono.builder.config.GAV; import dev.sashimono.builder.dependencies.ResolvedDependency; +import dev.sashimono.builder.util.Log; import dev.sashimono.builder.util.StringUtil; import dev.sashimono.builder.util.TaskMap; @@ -21,6 +22,7 @@ */ public class JarTask extends AbstractJarTask implements Function { + private static final Log log = Log.of(JarTask.class); public static final String BUILD_TOOL_JDK_SPEC = "Build-Tool-Jdk-Spec"; public static final String JAVA_SPEC_VERSION = "java.specification.version"; public static final String EOL = "\r\n"; // For consistency across manifests @@ -32,6 +34,11 @@ public JarTask(final Path outputDir, final GAV gav, final Path filteredResources this.manifestEntries = manifestEntries; } + @Override + protected Log getLogger() { + return log; + } + @Override public JarResult apply(final TaskMap taskMap) { final CompileResult deps = taskMap.results(CompileResult.class).get(0); diff --git a/builder/src/main/java/dev/sashimono/builder/jar/JavadocJarTask.java b/builder/src/main/java/dev/sashimono/builder/jar/JavadocJarTask.java new file mode 100644 index 0000000..ed43b0b --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/jar/JavadocJarTask.java @@ -0,0 +1,39 @@ +package dev.sashimono.builder.jar; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.function.Function; + +import dev.sashimono.builder.config.GAV; +import dev.sashimono.builder.documenter.DocumentationResult; +import dev.sashimono.builder.util.Log; +import dev.sashimono.builder.util.TaskMap; + +/** + * Creates a jar file from javadoc. + */ +public class JavadocJarTask extends AbstractJarTask implements Function { + + private static final Log log = Log.of(JavadocJarTask.class); + + public JavadocJarTask(final Path outputDir, final GAV gav, final Path filteredResourcesDir) { + super(outputDir, gav, filteredResourcesDir); + } + + @Override + protected Log getLogger() { + return log; + } + + @Override + public FileOutput apply(final TaskMap taskMap) { + final DocumentationResult deps = taskMap.results(DocumentationResult.class).get(0); + try { + // TODO Write manifest file like JarTask + collectFiles(deps.documentationDirectory()); + return new FileOutput(createJar("javadoc")); + } catch (final IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/builder/src/main/java/dev/sashimono/builder/jar/SourcesJarTask.java b/builder/src/main/java/dev/sashimono/builder/jar/SourcesJarTask.java index 87dc811..ade42d3 100644 --- a/builder/src/main/java/dev/sashimono/builder/jar/SourcesJarTask.java +++ b/builder/src/main/java/dev/sashimono/builder/jar/SourcesJarTask.java @@ -6,6 +6,7 @@ import java.util.function.Function; import dev.sashimono.builder.config.GAV; +import dev.sashimono.builder.util.Log; import dev.sashimono.builder.util.TaskMap; /** @@ -13,13 +14,19 @@ */ public class SourcesJarTask extends AbstractJarTask implements Function { - private final List sourceDirs; + private static final Log log = Log.of(SourcesJarTask.class); + protected final List sourceDirs; public SourcesJarTask(final Path outputDir, final GAV gav, final Path filteredResourcesDir, final List sourceDirs) { super(outputDir, gav, filteredResourcesDir); this.sourceDirs = sourceDirs; } + @Override + protected Log getLogger() { + return log; + } + @Override public FileOutput apply(final TaskMap taskMap) { try { diff --git a/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaTool.java b/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaTool.java new file mode 100644 index 0000000..b50772f --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaTool.java @@ -0,0 +1,85 @@ +package dev.sashimono.builder.tool; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; + +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; + +import dev.sashimono.builder.util.Log; + +public abstract class AbstractJavaTool { + + protected abstract Log getLogger(); + + public abstract Path process(); + + protected final javax.tools.Tool tool; + protected final List flags; + protected final List dependencies; + protected final List sourceDirectories; + + protected AbstractJavaTool(final javax.tools.Tool tool, final List flags, final List dependencies, + final List sourceDirectories) { + this.tool = tool; + this.flags = flags; + this.dependencies = dependencies; + this.sourceDirectories = sourceDirectories; + } + + protected List collectSourceFiles() { + final List sourceFiles = new ArrayList<>(); + sourceDirectories.forEach(s -> { + try { + Files.walkFileTree(s, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) { + if (file.getFileName().toString().endsWith(".java")) { + sourceFiles.add(file.toFile()); + } + return FileVisitResult.CONTINUE; + } + + }); + } catch (final IOException e) { + throw new RuntimeException(e); + } + }); + //we sort the source files to help with reproducibility + sourceFiles.sort(Comparator.comparing(Object::toString)); + return sourceFiles; + } + + protected Path configureFileManager(final StandardJavaFileManager fileManager, final JavaFileManager.Location to) + throws IOException { + final Path output = Files.createTempDirectory("output"); + fileManager.setLocation(StandardLocation.CLASS_PATH, + dependencies.stream().map(Path::toFile).collect(Collectors.toSet())); + fileManager.setLocation(to, List.of(output.toFile())); + return output; + } + + protected void processDiagnostics(final DiagnosticCollector diagnosticsCollector) { + for (final Diagnostic diagnostic : diagnosticsCollector.getDiagnostics()) { + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { + getLogger().error(diagnostic.getMessage(Locale.getDefault())); + } else { + getLogger().info(diagnostic.getMessage(Locale.getDefault())); + } + } + } +} diff --git a/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaToolTask.java b/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaToolTask.java new file mode 100644 index 0000000..36bf36c --- /dev/null +++ b/builder/src/main/java/dev/sashimono/builder/tool/AbstractJavaToolTask.java @@ -0,0 +1,13 @@ +package dev.sashimono.builder.tool; + +import java.nio.file.Path; +import java.util.List; + +public abstract class AbstractJavaToolTask { + + protected final List sourceDirectories; + + protected AbstractJavaToolTask(final List sourceDirectories) { + this.sourceDirectories = sourceDirectories; + } +} diff --git a/builder/src/test/java/dev/sashimono/builder/complete/BuildProjectTestCase.java b/builder/src/test/java/dev/sashimono/builder/complete/BuildProjectTestCase.java index b000889..34dd51d 100644 --- a/builder/src/test/java/dev/sashimono/builder/complete/BuildProjectTestCase.java +++ b/builder/src/test/java/dev/sashimono/builder/complete/BuildProjectTestCase.java @@ -23,12 +23,14 @@ public void testBuildingSimpleProject(BuildResult result) throws IOException { Path jar = dir.resolve("test-1.1.0.Final.jar"); Path pom = dir.resolve("pom.xml"); Path sourcesJar = dir.resolve("test-1.1.0.Final-sources.jar"); + Path javadocJar = dir.resolve("test-1.1.0.Final-javadoc.jar"); Assertions.assertTrue(Files.exists(jar)); Assertions.assertTrue(Files.exists(pom)); Assertions.assertTrue(Files.exists(sourcesJar)); + Assertions.assertTrue(Files.exists(javadocJar)); String expectedAppPropsContents = """ greeting.message = hello - greeting.name = quarkus""".replaceAll("\n", System.lineSeparator()); + greeting.name = quarkus""".replaceAll(NEW_LINE, System.lineSeparator()); try (JarFile jarFile = new JarFile(jar.toFile())) { var main = jarFile.getJarEntry("foo/bar/Main.class"); @@ -127,6 +129,13 @@ public static void main(String ... args) { Assertions.assertEquals(expectedAppPropsContents, appPropsContents); Assertions.assertEquals(0, applicationProperties.getLastModifiedTime().toMillis()); } + + try (JarFile javadocJarFile = new JarFile(javadocJar.toFile())) { + var main = javadocJarFile.getJarEntry("foo/bar/Main.html"); + Assertions.assertNotNull(main); + Assertions.assertTrue(main.getSize() > 100); + Assertions.assertEquals(0, main.getLastModifiedTime().toMillis()); + } } @BuildTest("src/test/resources/multi-module-project") @@ -135,9 +144,11 @@ public void testBuildingMultiModuleProject(BuildResult result) throws IOExceptio Path jar = dir.resolve("foo-1.0.jar"); Path pom = dir.resolve("pom.xml"); Path sourcesJar = dir.resolve("foo-1.0-sources.jar"); + Path javadocJar = dir.resolve("foo-1.0-javadoc.jar"); Assertions.assertTrue(Files.exists(jar)); Assertions.assertTrue(Files.exists(pom)); Assertions.assertTrue(Files.exists(sourcesJar)); + Assertions.assertTrue(Files.exists(javadocJar)); try (JarFile jarFile = new JarFile(jar.toFile())) { var main = jarFile.getJarEntry("acme/foo/Greeter.class"); @@ -201,13 +212,22 @@ public String greet() { Assertions.assertEquals(0, greeter.getLastModifiedTime().toMillis()); } + try (JarFile javadocJarFile = new JarFile(javadocJar.toFile())) { + var greeter = javadocJarFile.getJarEntry("acme/foo/Greeter.html"); + Assertions.assertNotNull(greeter); + Assertions.assertTrue(greeter.getSize() > 100); + Assertions.assertEquals(0, greeter.getLastModifiedTime().toMillis()); + } + dir = result.output().resolve("com").resolve("acme").resolve("bar").resolve("1.0"); jar = dir.resolve("bar-1.0.jar"); pom = dir.resolve("pom.xml"); sourcesJar = dir.resolve("bar-1.0-sources.jar"); + javadocJar = dir.resolve("bar-1.0-javadoc.jar"); Assertions.assertTrue(Files.exists(jar)); Assertions.assertTrue(Files.exists(pom)); Assertions.assertTrue(Files.exists(sourcesJar)); + Assertions.assertTrue(Files.exists(javadocJar)); try (JarFile jarFile = new JarFile(jar.toFile())) { var main = jarFile.getJarEntry("acme/bar/Main.class"); @@ -281,5 +301,12 @@ public static void main(String ... args) { Assertions.assertEquals(expectedMainContents, mainContents); Assertions.assertEquals(0, main.getLastModifiedTime().toMillis()); } + + try (JarFile javadocJarFile = new JarFile(javadocJar.toFile())) { + var main = javadocJarFile.getJarEntry("acme/bar/Main.html"); + Assertions.assertNotNull(main); + Assertions.assertTrue(main.getSize() > 100); + Assertions.assertEquals(0, main.getLastModifiedTime().toMillis()); + } } }