Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,19 @@ void resolutionUsesProjectPathsFromDiscoveryAndProjectsRequestedOptions() throws
.contains("Gradle build output"));
}

@Test
@EnabledOnOs({OS.LINUX, OS.MAC})
void exposesTheGradleCompilerAsTheSystemImage() throws Exception {
var fixture = fixture();
var values = capture(fixture);
values.remove("release");
values.setProperty("system", System.getProperty("java.home"));
storeCapture(fixture, values);
var result = resolve(fixture, "compile", "system");
assertEquals(0, result.exitCode(), result.error());
assertEquals(List.of("--system", System.getProperty("java.home")), arguments(result));
}

@Test
@EnabledOnOs({OS.LINUX, OS.MAC})
void compileAndRuntimeClasspathsAreDifferentViews() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public final class Compile extends AbstractMojo {
private boolean noop;
private boolean generate;
private String moduleName;
private String compilerId = "javac";
private String release;
private String executable;
private boolean fork;
private List<String> compilerArgs;
public void execute() throws MojoExecutionException {
if (noop) { return; }
try {
Expand Down Expand Up @@ -134,6 +139,7 @@ private static void sources(File root, List<String> arguments) {
"-d", classes.toString(), source.toString()), diagnostics.toString());
install(repository, classes, "maven-compiler-plugin", "3.1", List.of(goal("compile", "compile", false, false), goal("testCompile", "test", true, false), goal("generate", "", false, false)));
install(repository, classes, "maven-resources-plugin", "2.6", List.of(goal("resources", "", false, true), goal("testResources", "", true, true)));
install(repository, classes, "maven-toolchains-plugin", "3.2.0", List.of(goal("toolchain", "", false, true)));
// Older Maven versions inject this legacy plugin dependency even when the
// plugin does not use it. An empty fixture artifact keeps resolution offline.
Path plexus = Files.createDirectories(repository.resolve("org/codehaus/plexus/plexus-utils/1.1"));
Expand All @@ -156,12 +162,20 @@ private static String goal(String name, String scope, boolean test, boolean noop
<parameter><name>noop</name><type>boolean</type><editable>false</editable></parameter>
<parameter><name>generate</name><type>boolean</type><editable>false</editable></parameter>
<parameter><name>moduleName</name><type>java.lang.String</type><editable>true</editable></parameter>
<parameter><name>compilerId</name><type>java.lang.String</type><editable>true</editable></parameter>
<parameter><name>release</name><type>java.lang.String</type><editable>true</editable></parameter>
<parameter><name>executable</name><type>java.lang.String</type><editable>true</editable></parameter>
<parameter><name>fork</name><type>boolean</type><editable>true</editable></parameter>
<parameter><name>jdkToolchain</name><type>java.util.Map</type><editable>true</editable></parameter>
<parameter><name>toolchains</name><type>java.util.Map</type><editable>true</editable></parameter>
<parameter><name>compilerArgs</name><type>java.util.List</type><editable>true</editable></parameter>
</parameters>
<configuration>
<project implementation="org.apache.maven.project.MavenProject">${project}</project>
<test implementation="boolean" default-value="%s"/>
<noop implementation="boolean" default-value="%s"/>
<generate implementation="boolean" default-value="%s"/>
<compilerId implementation="java.lang.String" default-value="javac"/>
</configuration>
</mojo>
""".formatted(name, scope.isEmpty() ? "" : "<requiresDependencyResolution>" + scope + "</requiresDependencyResolution>", test, noop, name.equals("generate"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/** Contract checks on every Maven version; detailed scenarios at lifecycle API boundaries. */
@EnabledOnOs({OS.LINUX, OS.MAC})
Expand Down Expand Up @@ -73,6 +74,99 @@ void reusesCompiledPluginFixturesWithoutSharingMutableRepositories() throws Exce
assertArrayEquals(original, Files.readAllBytes(root.resolve("repository").resolve(plugin)));
}

@Test
void exposesTheMavenRuntimeAsTheSystemImage() throws Exception {
assumeTrue(maven.javaVersion() >= 9);
Path root = fixture(true);
var result = discover(root, "--project", ":app", "--scope", "compile", "-r", "system");
assertEquals(0, result.status(), result.error());
assertEquals("--system\n" + maven.javaHome().toRealPath() + "\n", result.output());
}

@Test
@EnabledIf("detailedCoverage")
void exposesTheConfiguredForkedCompilerSystemImage() throws Exception {
Path root = fixture(true);
Path app = root.resolve("app");
Path javaHome = Path.of(System.getProperty("java.home")).toRealPath();
String model = pom("app", "", customLayout()).replace("<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>", """
<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>
<configuration><fork>true</fork><executable>%s/bin/javac</executable></configuration>
""".formatted(javaHome));
Files.writeString(app.resolve("pom.xml"), model);
var result = discover(root, "--project", ":app", "--scope", "compile", "-r", "system");
assertEquals(0, result.status(), result.error());
assertEquals("--system\n" + javaHome + "\n", result.output());
}

@Test
@EnabledIf("detailedMavenConfigCoverage")
void exposesTheCompilerJdkToolchainSystemImage() throws Exception {
Path root = fixture(true);
Path app = root.resolve("app");
Path javaHome = Path.of(System.getProperty("java.home")).toRealPath();
String version = Integer.toString(Runtime.version().feature());
String model = pom("app", "", customLayout()).replace("<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>", """
<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>
<configuration><jdkToolchain><version>%s</version></jdkToolchain></configuration>
""".formatted(version));
Files.writeString(app.resolve("pom.xml"), model);
Path toolchains = root.resolve("toolchains.xml");
Files.writeString(toolchains, """
<toolchains><toolchain><type>jdk</type><provides><version>%s</version></provides>
<configuration><jdkHome>%s</jdkHome></configuration>
</toolchain></toolchains>
""".formatted(version, javaHome));
Path wrapper = root.resolve("mvnw");
Files.writeString(wrapper, Files.readString(wrapper).replace("\"$@\"", "--toolchains " + shellQuote(toolchains.toString()) + " \"$@\""));
var result = discover(root, "--project", ":app", "--scope", "compile", "-r", "system");
assertEquals(0, result.status(), result.error());
assertEquals("--system\n" + javaHome + "\n", result.output());
}

@Test
@EnabledIf("detailedMavenConfigCoverage")
void exposesTheBuildContextToolchainSystemImage() throws Exception {
Path root = fixture(true);
Path app = root.resolve("app");
Path javaHome = Path.of(System.getProperty("java.home")).toRealPath();
String version = Integer.toString(Runtime.version().feature());
String plugin = """
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-toolchains-plugin</artifactId><version>3.2.0</version>
<executions><execution><phase>validate</phase><goals><goal>toolchain</goal></goals>
<configuration><toolchains><jdk><version>%s</version></jdk></toolchains></configuration>
</execution></executions>
</plugin>
""".formatted(version);
Files.writeString(app.resolve("pom.xml"), pom("app", "", customLayout()).replace("</plugins>", plugin + "</plugins>"));
Path toolchains = root.resolve("toolchains.xml");
Files.writeString(toolchains, """
<toolchains><toolchain><type>jdk</type><provides><version>%s</version></provides>
<configuration><jdkHome>%s</jdkHome></configuration>
</toolchain></toolchains>
""".formatted(version, javaHome));
Path wrapper = root.resolve("mvnw");
Files.writeString(wrapper, Files.readString(wrapper).replace("\"$@\"", "--toolchains " + shellQuote(toolchains.toString()) + " \"$@\""));
var result = discover(root, "--project", ":app", "--scope", "compile", "-r", "system");
assertEquals(0, result.status(), result.error());
assertEquals("--system\n" + javaHome + "\n", result.output());
}

@Test
@EnabledIf("detailedCoverage")
void exposesReleaseInsteadOfTheCompilerSystemImage() throws Exception {
Path root = fixture(true);
Path app = root.resolve("app");
String model = pom("app", "", customLayout()).replace("<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>", """
<artifactId>maven-compiler-plugin</artifactId><version>3.1</version>
<configuration><release>17</release></configuration>
""");
Files.writeString(app.resolve("pom.xml"), model);
var result = discover(root, "--project", ":app", "--scope", "compile", "-r", "release,system");
assertEquals(0, result.status(), result.error());
assertEquals("--release\n17\n", result.output());
}

@Test
void resolutionCacheReusesAnswersAndRefreshInvokesMaven() throws Exception {
Path root = fixture();
Expand Down
Loading
Loading