Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active March 21, 2023 14:51
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 thomasdarimont/2e69c12d7afb2cc63348d4ea63cc3a77 to your computer and use it in GitHub Desktop.
Save thomasdarimont/2e69c12d7afb2cc63348d4ea63cc3a77 to your computer and use it in GitHub Desktop.
Java Build Tool PoC

Idea: java build within a maven / gradle project root should detect and run build tool automatically with "appropriate" settings.

Running java build within a maven project root should execute mvn clean verify by default (maven BCP).

Example command

tom@neumann ~/dev/repos/gh/thomasdarimont/training/java-workbench/lib-java-build-tool (master) 
$ java --enable-preview --source=20 -cp target/classes build.java clean package

if this were explicitly supported by the java launcher, we could have something like the following to produce the output below:

$ java build

This would still give users the choice of their build tool but invoking it could be build-tool agnostic.

Example Output

build: Found maven project
build: Maven start
[INFO] Scanning for projects...
[INFO] 
[INFO] -------< com.github.thomasdarimont.training:lib-java-build-tool >-------
[INFO] Building lib-java-build-tool 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.2.0:clean (default-clean) @ lib-java-build-tool ---
[INFO] Deleting /home/tom/dev/repos/gh/thomasdarimont/training/java-workbench/lib-java-build-tool/target
[INFO] 
[INFO] --- toolchains:3.1.0:toolchain (default) @ lib-java-build-tool ---
[INFO] Required toolchain: jdk [ os='Linux' vendor='OpenJDK' version='20-open' ]
[INFO] Found matching toolchain for type jdk: JDK[/home/tom/.sdkman/candidates/java/20.ea.20-open]
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ lib-java-build-tool ---
[INFO] Copying 0 resource
[INFO] 
[INFO] --- compiler:3.8.1:compile (default-compile) @ lib-java-build-tool ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/tom/.sdkman/candidates/java/20.ea.20-open]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/tom/dev/repos/gh/thomasdarimont/training/java-workbench/lib-java-build-tool/target/classes
[INFO] 
[INFO] --- resources:3.3.0:testResources (default-testResources) @ lib-java-build-tool ---
[INFO] skip non existing resourceDirectory /home/tom/dev/repos/gh/thomasdarimont/training/java-workbench/lib-java-build-tool/src/test/resources
[INFO] 
[INFO] --- compiler:3.8.1:testCompile (default-testCompile) @ lib-java-build-tool ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/tom/.sdkman/candidates/java/20.ea.20-open]
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- surefire:2.22.2:test (default-test) @ lib-java-build-tool ---
[INFO] Toolchain in maven-surefire-plugin: JDK[/home/tom/.sdkman/candidates/java/20.ea.20-open]
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ lib-java-build-tool ---
[INFO] Building jar: /home/tom/dev/repos/gh/thomasdarimont/training/java-workbench/lib-java-build-tool/target/lib-java-build-tool-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.509 s
[INFO] Finished at: 2023-03-21T15:40:33+01:00
[INFO] ------------------------------------------------------------------------
build: Maven end
package demo.tools.build;
import com.google.auto.service.AutoService;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Scanner;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;
/**
* <pre>
* java --enable-preview --source=20 -agentlib:jdwp='transport=dt_socket,server=y,suspend=y,address=*:5005' -cp target/classes src/main/java/build.java clean package
*
* java --enable-preview --source=20 -cp target/classes src/main/java/build.java clean package
* </pre>
*/
@AutoService(ToolProvider.class)
public class BuildToolProvider implements ToolProvider {
@Override
public String name() {
return "build";
}
@Override
public int run(PrintWriter out, PrintWriter err, String... args) {
// check for maven
var pomXmlFound = Files.exists(Path.of("pom.xml"));
if (pomXmlFound) {
out.printf("%s: Found maven project%n", name());
var mvnArgs = Stream.of(args).toList();
var mvnCommand = List.of("mvn");
var mvnCommandLine = Stream.concat(mvnCommand.stream(), mvnArgs.stream()).toList();
var pb = new ProcessBuilder(mvnCommandLine);
try {
out.printf("%s: Maven start%n", name());
var process = pb.start();
Thread.startVirtualThread(() -> {
try (var sout = new Scanner(new InputStreamReader(process.getInputStream())); //
var serr = new Scanner(new InputStreamReader(process.getErrorStream()))) {
while (true) {
while (sout.hasNextLine()) {
out.println(sout.nextLine());
}
while (serr.hasNextLine()) {
err.println(serr.nextLine());
}
Thread.yield();
}
}
});
var exitCode = process.waitFor();
out.printf("%s: Maven end%n", name());
return exitCode;
} catch (InterruptedException | IOException e) {
err.printf("%s: Maven error %n", name());
throw new RuntimeException(e);
}
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment