Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk11-test/foo/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo;

public class Foo {
public static void main(String[] args) {
System.out.println("Hello from JDK 11!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=--no-transfer-progress install -X
47 changes: 47 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk11-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project>
<groupId>foo</groupId>
<artifactId>toolchain-jdk11-test</artifactId>
<version>1</version>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.basedir}/target/classes</outputDirectory>
<testOutputDirectory>${project.basedir}/target/test-classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>11</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>io.github.rockcrafters</groupId>
<artifactId>rockcraft-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>create-rock</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk11-test/prebuild.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.io.*;

// Copy the toolchains.xml file to ~/.m2/ for this test
File toolchainsSource = new File(basedir, "toolchains.xml");
File toolchainsTarget = new File(System.getProperty("user.home") + "/.m2/toolchains.xml");

// Ensure the directory exists
toolchainsTarget.getParentFile().mkdirs();

// Copy the file using streams
FileInputStream fis = new FileInputStream(toolchainsSource);
FileOutputStream fos = new FileOutputStream(toolchainsTarget);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();

System.out.println("Copied toolchains.xml to " + toolchainsTarget);
13 changes: 13 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk11-test/toolchains.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<vendor>temurin</vendor>
</provides>
<configuration>
<jdkHome>/usr/lib/jvm/temurin-11-jdk-amd64</jdkHome>
</configuration>
</toolchain>
</toolchains>
33 changes: 33 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk11-test/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.*;

// Check that the rockcraft.yaml file was created
File target = new File(basedir, "target");

File[] files = target.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.equals("rockcraft.yaml");
}
});

if (files == null || files.length == 0) {
throw new FileNotFoundException("Could not find generated rockcraft.yaml file in " + target);
}

// Read the file and check for openjdk-11-jdk
File rockcraftYaml = files[0];
BufferedReader reader = new BufferedReader(new FileReader(rockcraftYaml));
String line;
boolean foundJdk11 = false;
while ((line = reader.readLine()) != null) {
if (line.contains("openjdk-11-jdk")) {
foundJdk11 = true;
break;
}
}
reader.close();

if (!foundJdk11) {
throw new RuntimeException("Expected to find openjdk-11-jdk in rockcraft.yaml, but it was not found.");
}

System.out.println("SUCCESS: Toolchain correctly detected JDK 11 and set build-packages to openjdk-11-jdk");
7 changes: 7 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk17-test/foo/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo;

public class Foo {
public static void main(String[] args) {
System.out.println("Hello from JDK 17!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=--no-transfer-progress install -X
47 changes: 47 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk17-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project>
<groupId>foo</groupId>
<artifactId>toolchain-jdk17-test</artifactId>
<version>1</version>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.basedir}/target/classes</outputDirectory>
<testOutputDirectory>${project.basedir}/target/test-classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>17</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>io.github.rockcrafters</groupId>
<artifactId>rockcraft-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>create-rock</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk17-test/prebuild.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.io.*;

// Copy the toolchains.xml file to ~/.m2/ for this test
File toolchainsSource = new File(basedir, "toolchains.xml");
File toolchainsTarget = new File(System.getProperty("user.home") + "/.m2/toolchains.xml");

// Ensure the directory exists
toolchainsTarget.getParentFile().mkdirs();

// Copy the file using streams
FileInputStream fis = new FileInputStream(toolchainsSource);
FileOutputStream fos = new FileOutputStream(toolchainsTarget);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();

System.out.println("Copied toolchains.xml to " + toolchainsTarget);
13 changes: 13 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk17-test/toolchains.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>17</version>
<vendor>temurin</vendor>
</provides>
<configuration>
<jdkHome>/usr/lib/jvm/temurin-17-jdk-amd64</jdkHome>
</configuration>
</toolchain>
</toolchains>
33 changes: 33 additions & 0 deletions rockcraft-maven/src/it/toolchain-jdk17-test/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.*;

// Check that the rockcraft.yaml file was created
File target = new File(basedir, "target");

File[] files = target.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.equals("rockcraft.yaml");
}
});

if (files == null || files.length == 0) {
throw new FileNotFoundException("Could not find generated rockcraft.yaml file in " + target);
}

// Read the file and check for openjdk-17-jdk
File rockcraftYaml = files[0];
BufferedReader reader = new BufferedReader(new FileReader(rockcraftYaml));
String line;
boolean foundJdk17 = false;
while ((line = reader.readLine()) != null) {
if (line.contains("openjdk-17-jdk")) {
foundJdk17 = true;
break;
}
}
reader.close();

if (!foundJdk17) {
throw new RuntimeException("Expected to find openjdk-17-jdk in rockcraft.yaml, but it was not found.");
}

System.out.println("SUCCESS: Toolchain correctly detected JDK 17 and set build-packages to openjdk-17-jdk");
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Arrays;

Expand All @@ -21,6 +24,39 @@ public final class Toolchain {
private Toolchain() {
}

private static String getToolchainJavacPath(ToolchainManager toolchainManager, MavenSession session) {
if (toolchainManager == null) {
return null;
}
org.apache.maven.toolchain.Toolchain jdkToolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
if (jdkToolchain == null) {
return null;
}
return jdkToolchain.findTool("javac");
}

private static String getJavacPath(ToolchainManager toolchainManager, MavenSession session, Log log) {
String where = getToolchainJavacPath(toolchainManager, session);
if (where != null) {
return where;
}
String javaHome = System.getProperty("java.home");
if (javaHome == null || javaHome.isEmpty()) {
log.warn("java-rockcraft-plugin: please configure Maven toolchain or provide a valid Java Home.");
return null;
}
Path java9Path = Paths.get(javaHome, "bin", "javac");
Path java8Path = Paths.get(Paths.get(javaHome).getParent().toString(), "bin", "javac");

if (Files.isExecutable(java9Path)) {
return java9Path.toString();
} else if (Files.isExecutable(java8Path)) {
return java8Path.toString();
}
log.warn("java-rockcraft-plugin: please configure Maven toolchain or provide a valid Java Home: "+ javaHome);
return null;
}

/**
* Gets the toolchain settings for the project
*
Expand All @@ -31,31 +67,21 @@ private Toolchain() {
*/
public static String getToolchainPackage(MavenSession session, ToolchainManager toolchainManager, Log log) {
try {
if (toolchainManager == null) {
log.warn("java-rockcraft-plugin: Maven Toolchain manager is not present.");
return ToolchainHelper.DEFAULT_JDK;
}
System.err.println("---- has toolchain manager --- ");
org.apache.maven.toolchain.Toolchain jdkToolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
if (jdkToolchain == null) {
log.warn("java-rockcraft-plugin: Maven Toolchain is not configured. Please configure toolchain or use buildPackage configuration");
return ToolchainHelper.DEFAULT_JDK;
}
String tool = jdkToolchain.findTool("javac");
String tool = getJavacPath(toolchainManager, session, log);
if (tool == null) {
log.warn("java-rockcraft-plugin: Maven Toolchain - javac tool is not found.");
return ToolchainHelper.DEFAULT_JDK;
}

ToolchainHelper.ToolchainPackage p = ToolchainHelper.getBuildPackage(tool);
switch (p.getReason()) {
case JAVAC_ERROR:
log.warn("java-rockcraft-plugin: Maven Toolchain - javac error {}, please set buildPackage configuration option: {}", tool, p.getRawOutput());
log.warn("java-rockcraft-plugin: Maven Toolchain - javac error " + tool + ", please set buildPackage configuration option: " + p.getRawOutput());
break;
case JAVAC_VERSION_STRING:
log.warn("java-rockcraft-plugin: Maven Toolchain - unable to parse javac version string, please set buildPackage configuration option: {}", p.getRawOutput());
log.warn("java-rockcraft-plugin: Maven Toolchain - unable to parse javac version string, please set buildPackage configuration option: " + p.getRawOutput());
break;
case JAVAC_UNSUPPORTED_VERSION_STRING:
log.warn("java-rockcraft-plugin: Maven Toolchain - unsupported version string, please set buildPackage configuration option. {}", p.getRawOutput());
log.warn("java-rockcraft-plugin: Maven Toolchain - unsupported version string, please set buildPackage configuration option. " + p.getRawOutput());
break;
}
return p.getName();
Expand Down