mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Merge pull request #20615 from github/idrissrio/java-jdk
Java: Add test for multi-module projects with different Java versions
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-add-exports-module-flags</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>maven-add-exports-module-flags</name>
|
||||
<description>Test case: Project using --add-exports. Autobuilder should detect this and use --source/--target.</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<compilerArgs>
|
||||
<arg>--add-exports</arg>
|
||||
<arg>jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
|
||||
<arg>--add-exports</arg>
|
||||
<arg>jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
|
||||
<arg>--add-exports</arg>
|
||||
<arg>jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
|
||||
<arg>--add-exports</arg>
|
||||
<arg>jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
pom.xml
|
||||
src/main/java/com/example/CompilerUser.java
|
||||
target/maven-archiver/pom.properties
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example;
|
||||
|
||||
import com.sun.tools.javac.api.JavacTool;
|
||||
|
||||
/**
|
||||
* Simple class that uses JDK compiler internals.
|
||||
* This requires --add-exports flags to compile.
|
||||
*/
|
||||
public class CompilerUser {
|
||||
public static void main(String[] args) {
|
||||
// Use JavacTool from jdk.compiler module
|
||||
JavacTool tool = JavacTool.create();
|
||||
System.out.println("Compiler tool: " + tool.getClass().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def test(codeql, java, actions_toolchains_file):
|
||||
codeql.database.create(_env={"LGTM_INDEX_MAVEN_TOOLCHAINS_FILE": str(actions_toolchains_file)})
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-execution-specific-java-version</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>maven-execution-specific-java-version</name>
|
||||
<description>Test case: Project with execution-specific Java versions (Java 11 for main, Java 17 for test). Maven.java should detect the highest version (17) and use it for compilation.</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<executions>
|
||||
<!-- Compilation for src/main/java -->
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<release>11</release> <!-- Java 11 for main -->
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
<!-- Compilation for src/test/java -->
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<release>17</release> <!-- Java 17 for test -->
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
pom.xml
|
||||
src/main/java/com/example/App.java
|
||||
src/test/java/com/example/AppTest.java
|
||||
target/maven-archiver/pom.properties
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
var message = "Hello World! Running on Java " + System.getProperty("java.version");
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return "Hello from App";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example;
|
||||
|
||||
public class AppTest {
|
||||
public static void main(String[] args) {
|
||||
var text = """
|
||||
Hello
|
||||
World
|
||||
""";
|
||||
System.out.println(text.strip());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def test(codeql, java, actions_toolchains_file):
|
||||
codeql.database.create(_env={"LGTM_INDEX_MAVEN_TOOLCHAINS_FILE": str(actions_toolchains_file)})
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-java16-with-higher-jdk</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>maven-java16-with-higher-jdk</name>
|
||||
<description>Test case: Java 16 target when only Java 17+ is available.</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>16</java.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<release>${java.version}</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
pom.xml
|
||||
src/main/java/com/example/App.java
|
||||
target/maven-archiver/pom.properties
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Simple class using Java 16 features (e.g.,records).
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Person person = new Person("Bob", 42);
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
|
||||
record Person(String name, int age) {}
|
||||
@@ -0,0 +1,2 @@
|
||||
def test(codeql, java, actions_toolchains_file):
|
||||
codeql.database.create(_env={"LGTM_INDEX_MAVEN_TOOLCHAINS_FILE": str(actions_toolchains_file)})
|
||||
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-java8-java11-dependency</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>maven-java8-java11-dependency</name>
|
||||
<description>Test case: Java 8 project with dependency requiring Java 11+</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- TestNG 7.7.0 is compiled with Java 11 (class file version 55.0) -->
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>7.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
pom.xml
|
||||
src/main/java/com/example/Calculator.java
|
||||
src/test/java/com/example/CalculatorTest.java
|
||||
target/maven-archiver/pom.properties
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example;
|
||||
|
||||
public class Calculator {
|
||||
public int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public int multiply(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Test class using TestNG 7.7.0 which requires Java 11+.
|
||||
*/
|
||||
public class CalculatorTest {
|
||||
@Test
|
||||
public void testAdd() {
|
||||
Calculator calc = new Calculator();
|
||||
Assert.assertEquals(calc.add(2, 3), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiply() {
|
||||
Calculator calc = new Calculator();
|
||||
Assert.assertEquals(calc.multiply(3, 4), 12);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def test(codeql, java, actions_toolchains_file):
|
||||
codeql.database.create(_env={"LGTM_INDEX_MAVEN_TOOLCHAINS_FILE": str(actions_toolchains_file)})
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-multimodule-test-java-version</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<artifactId>main-module</artifactId>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-multimodule-test-java-version</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>main-module</module>
|
||||
<module>test-module</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
main-module/pom.xml
|
||||
main-module/src/main/java/com/example/App.java
|
||||
main-module/target/maven-archiver/pom.properties
|
||||
pom.xml
|
||||
test-module/pom.xml
|
||||
test-module/src/main/java/com/example/tests/TestUtils.java
|
||||
test-module/target/maven-archiver/pom.properties
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>maven-multimodule-test-java-version</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<artifactId>test-module</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>21</maven.compiler.release>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.tests;
|
||||
|
||||
// Requires Java 21: switch with pattern matching and guards
|
||||
public class TestUtils {
|
||||
public static String analyze(Object obj) {
|
||||
return switch (obj) {
|
||||
case String s when s.length() > 5 -> "long";
|
||||
case String s -> "short";
|
||||
default -> "other";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def test(codeql, java, actions_toolchains_file):
|
||||
codeql.database.create(_env={"LGTM_INDEX_MAVEN_TOOLCHAINS_FILE": str(actions_toolchains_file)})
|
||||
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: minorAnalysis
|
||||
---
|
||||
* Java analysis now selects the Java version to use informed by Maven POM files across all project modules. It also tries to use Java 17 or higher for all Maven projects if possible, for improved build compatibility.
|
||||
Reference in New Issue
Block a user