Skip to content

Instantly share code, notes, and snippets.

@raphw
Created November 23, 2023 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphw/3127d7686615001e35b5ab8c0271dd7c to your computer and use it in GitHub Desktop.
Save raphw/3127d7686615001e35b5ab8c0271dd7c to your computer and use it in GitHub Desktop.
Mock-up for Java-based build tool (single source execution)
import javax.tools.*;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Build {
public static void main(String... args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<JavaFileObject> files = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("./src"), "*")) {
stream.forEach(path -> {
if (!path.getFileName().toString().endsWith(".java")) {
return;
}
files.add(new SimpleJavaFileObject(path.toUri(), JavaFileObject.Kind.SOURCE) {
@Override
public InputStream openInputStream() throws IOException {
return Files.newInputStream(path);
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
try (InputStream inputStream = Files.newInputStream(path)) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
});
});
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, StandardCharsets.UTF_8);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(Files.createDirectories(Paths.get("./build")).toFile()));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, files);
boolean success = task.call();
System.out.println("Compiled: " + success);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment