From 457bb8e00020c4f60f51a7e6a03811df0bc5fdf7 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Nov 2024 17:41:13 +0100 Subject: [PATCH] CLIng: finish TODO (#1888) Also clear up javadoc. --- .../cli/mvn/forked/ForkedMavenInvoker.java | 2 +- .../api/cli/mvn/local/LocalMavenInvoker.java | 2 +- .../mvn/resident/ResidentMavenInvoker.java | 2 +- .../mvn/forked/DefaultForkedMavenParser.java | 21 +++++++++++++++++-- .../mvn/local/DefaultLocalMavenInvoker.java | 6 ++++++ .../resident/DefaultResidentMavenInvoker.java | 5 +++-- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/forked/ForkedMavenInvoker.java b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/forked/ForkedMavenInvoker.java index 330eea91c5c7..ee75b2400b1d 100644 --- a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/forked/ForkedMavenInvoker.java +++ b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/forked/ForkedMavenInvoker.java @@ -22,7 +22,7 @@ import org.apache.maven.api.cli.mvn.MavenInvoker; /** - * Local Maven invoker. + * Forked Maven invoker. * * @since 4.0.0 */ diff --git a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/local/LocalMavenInvoker.java b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/local/LocalMavenInvoker.java index 73d604413e31..ed429c2220e2 100644 --- a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/local/LocalMavenInvoker.java +++ b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/local/LocalMavenInvoker.java @@ -24,7 +24,7 @@ import org.apache.maven.api.cli.mvn.MavenOptions; /** - * Local Maven invoker. + * Local Maven invoker, it expects all the Maven be present in classpath. * * @since 4.0.0 */ diff --git a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/resident/ResidentMavenInvoker.java b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/resident/ResidentMavenInvoker.java index ec9f9e0161a6..5743c7bb2c97 100644 --- a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/resident/ResidentMavenInvoker.java +++ b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/resident/ResidentMavenInvoker.java @@ -25,7 +25,7 @@ import org.apache.maven.api.cli.mvn.MavenOptions; /** - * Resident invoker. Instance is shut down when this instance is closed. + * Resident Maven invoker, similar to local. Instance is shut down when this instance is closed. * * @since 4.0.0 */ diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/forked/DefaultForkedMavenParser.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/forked/DefaultForkedMavenParser.java index 1f8173e1678a..c46164e822b5 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/forked/DefaultForkedMavenParser.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/forked/DefaultForkedMavenParser.java @@ -18,8 +18,13 @@ */ package org.apache.maven.cling.invoker.mvn.forked; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.cli.ParseException; import org.apache.maven.api.cli.ParserException; @@ -30,6 +35,9 @@ import org.apache.maven.cling.invoker.mvn.CommonsCliMavenOptions; import org.apache.maven.cling.invoker.mvn.LayeredMavenOptions; +/** + * Forked invoker that invokes Maven in a child process. + */ public class DefaultForkedMavenParser extends BaseMavenParser implements ForkedMavenParser { @@ -55,8 +63,17 @@ protected ForkedMavenInvokerRequest getInvokerRequest(LocalContext context) { protected List getJvmArguments(Path rootDirectory) { if (rootDirectory != null) { - // TODO: do this - return null; + Path jvmConfig = rootDirectory.resolve(".mvn/jvm.config"); + if (Files.exists(jvmConfig)) { + try { + return Files.readAllLines(jvmConfig).stream() + .filter(l -> !l.isBlank() && !l.startsWith("#")) + .flatMap(l -> Arrays.stream(l.split(" "))) + .collect(Collectors.toList()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } } return null; } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/local/DefaultLocalMavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/local/DefaultLocalMavenInvoker.java index 529af514cf3a..0f68baccdae2 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/local/DefaultLocalMavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/local/DefaultLocalMavenInvoker.java @@ -24,6 +24,12 @@ import org.apache.maven.cling.invoker.ProtoLookup; import org.apache.maven.cling.invoker.mvn.DefaultMavenInvoker; +/** + * Local invoker implementation, when Maven CLI is being run. System uses ClassWorld launcher, and class world + * instance is passed in via "enhanced" main method. Hence, this class expects fully setup ClassWorld via constructor. + * + * @see org.apache.maven.cling.MavenCling + */ public class DefaultLocalMavenInvoker extends DefaultMavenInvoker< MavenOptions, MavenInvokerRequest, DefaultLocalMavenInvoker.LocalContext> diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/DefaultResidentMavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/DefaultResidentMavenInvoker.java index 21bb815062bc..77890e9248e1 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/DefaultResidentMavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/DefaultResidentMavenInvoker.java @@ -29,8 +29,9 @@ import org.apache.maven.cling.invoker.mvn.DefaultMavenInvoker; /** - * Local invoker implementation, when Maven CLI is being run. System uses ClassWorld launcher, and class world - * instance is passed in via "enhanced" main method. Hence, this class expects fully setup ClassWorld via constructor. + * Local resident invoker implementation, similar to "local" but keeps Maven instance resident. This implies, that + * things like environment, system properties, extensions etc. are loaded only once. It is caller duty to ensure + * that subsequent call is right for the resident instance (ie no env change or different extension needed). */ public class DefaultResidentMavenInvoker extends DefaultMavenInvoker<