From 251cc2a1d7ea38753790a097b8c7dbac044b5629 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 11 Jan 2025 07:55:38 -0500 Subject: [PATCH 1/7] rename --- .../org/apache/maven/cling/invoker/BaseParser.java | 12 ++++++------ .../maven/cling/invoker/CommonsCliOptions.java | 2 +- .../cling/invoker/{Utils.java => InvokerUtils.java} | 7 ++++--- .../apache/maven/cling/invoker/LookupInvoker.java | 4 ++-- .../maven/cling/invoker/PlexusContainerCapsule.java | 2 +- .../cling/invoker/PlexusContainerCapsuleFactory.java | 2 +- .../cling/invoker/mvn/CommonsCliMavenOptions.java | 2 +- .../apache/maven/cling/invoker/mvn/MavenInvoker.java | 4 ++-- .../invoker/mvnenc/CommonsCliEncryptOptions.java | 2 +- .../cling/invoker/mvnsh/CommonsCliShellOptions.java | 2 +- 10 files changed, 20 insertions(+), 19 deletions(-) rename impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/{Utils.java => InvokerUtils.java} (97%) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java index e51d6311a7e..8bb2c5079ba 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java @@ -51,11 +51,11 @@ import org.apache.maven.properties.internal.SystemProperties; import static java.util.Objects.requireNonNull; -import static org.apache.maven.cling.invoker.Utils.getCanonicalPath; -import static org.apache.maven.cling.invoker.Utils.or; -import static org.apache.maven.cling.invoker.Utils.prefix; -import static org.apache.maven.cling.invoker.Utils.stripLeadingAndTrailingQuotes; -import static org.apache.maven.cling.invoker.Utils.toMap; +import static org.apache.maven.cling.invoker.InvokerUtils.getCanonicalPath; +import static org.apache.maven.cling.invoker.InvokerUtils.or; +import static org.apache.maven.cling.invoker.InvokerUtils.prefix; +import static org.apache.maven.cling.invoker.InvokerUtils.stripLeadingAndTrailingQuotes; +import static org.apache.maven.cling.invoker.InvokerUtils.toMap; public abstract class BaseParser implements Parser { @@ -216,7 +216,7 @@ protected Path getTopDirectory(LocalContext context) throws ParserException { @Nullable protected Path getRootDirectory(LocalContext context) throws ParserException { - return Utils.findRoot(context.topDirectory); + return InvokerUtils.findRoot(context.topDirectory); } protected Map populateSystemProperties(LocalContext context) throws ParserException { diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java index 0f8464d61b4..084aa8932b4 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java @@ -37,7 +37,7 @@ import org.apache.maven.jline.MessageUtils; import static java.util.Objects.requireNonNull; -import static org.apache.maven.cling.invoker.Utils.toMap; +import static org.apache.maven.cling.invoker.InvokerUtils.toMap; public abstract class CommonsCliOptions implements Options { diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/Utils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java similarity index 97% rename from impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/Utils.java rename to impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index 853cccc9313..7c68ef81602 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/Utils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -41,10 +41,11 @@ import static java.util.Objects.requireNonNull; /** - * Various utilities, mostly to bridge "old" and "new" stuff, like Properties vs Maps, File vs Paths, etc. + * Various utilities. */ -public final class Utils { - private Utils() {} +public final class InvokerUtils +{ + private InvokerUtils() {} @Nonnull public static String stripLeadingAndTrailingQuotes(String str) { diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index 971e0cedbdd..f2bd6e06398 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -87,8 +87,8 @@ import org.slf4j.spi.LocationAwareLogger; import static java.util.Objects.requireNonNull; -import static org.apache.maven.cling.invoker.Utils.toMavenExecutionRequestLoggingLevel; -import static org.apache.maven.cling.invoker.Utils.toProperties; +import static org.apache.maven.cling.invoker.InvokerUtils.toMavenExecutionRequestLoggingLevel; +import static org.apache.maven.cling.invoker.InvokerUtils.toProperties; /** * Lookup invoker implementation, that boots up DI container. diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java index ee4ae96b8ca..ac08b0c8f51 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java @@ -25,7 +25,7 @@ import org.codehaus.plexus.DefaultPlexusContainer; import static java.util.Objects.requireNonNull; -import static org.apache.maven.cling.invoker.Utils.toPlexusLoggingLevel; +import static org.apache.maven.cling.invoker.InvokerUtils.toPlexusLoggingLevel; /** * Container capsule backed by Plexus Container. diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java index 56d6cc099f3..c0e3aa2d3d5 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java @@ -61,7 +61,7 @@ import org.codehaus.plexus.logging.LoggerManager; import org.slf4j.ILoggerFactory; -import static org.apache.maven.cling.invoker.Utils.toPlexusLoggingLevel; +import static org.apache.maven.cling.invoker.InvokerUtils.toPlexusLoggingLevel; /** * Container capsule backed by Plexus Container. diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/CommonsCliMavenOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/CommonsCliMavenOptions.java index 33ff742d6ba..ebf8f5ad05f 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/CommonsCliMavenOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/CommonsCliMavenOptions.java @@ -33,7 +33,7 @@ import org.codehaus.plexus.interpolation.BasicInterpolator; import org.codehaus.plexus.interpolation.InterpolationException; -import static org.apache.maven.cling.invoker.Utils.createInterpolator; +import static org.apache.maven.cling.invoker.InvokerUtils.createInterpolator; public class CommonsCliMavenOptions extends CommonsCliOptions implements MavenOptions { public static CommonsCliMavenOptions parse(String source, String[] args) throws ParseException { diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 9e95b0d6599..1ddf28ba8b3 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -48,7 +48,7 @@ import org.apache.maven.cling.event.ExecutionEventLogger; import org.apache.maven.cling.invoker.LookupContext; import org.apache.maven.cling.invoker.LookupInvoker; -import org.apache.maven.cling.invoker.Utils; +import org.apache.maven.cling.invoker.InvokerUtils; import org.apache.maven.cling.transfer.ConsoleMavenTransferListener; import org.apache.maven.cling.transfer.QuietMavenTransferListener; import org.apache.maven.cling.transfer.SimplexTransferListener; @@ -252,7 +252,7 @@ protected void populateRequest(MavenContext context, Lookup lookup, MavenExecuti // project present, but we could not determine rootDirectory: extra work needed if (context.invokerRequest.rootDirectory().isEmpty()) { - Path rootDirectory = Utils.findMandatoryRoot(context.invokerRequest.topDirectory()); + Path rootDirectory = InvokerUtils.findMandatoryRoot(context.invokerRequest.topDirectory()); request.setMultiModuleProjectDirectory(rootDirectory.toFile()); request.setRootDirectory(rootDirectory); } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/CommonsCliEncryptOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/CommonsCliEncryptOptions.java index c21e3dce952..3129d0fa3b1 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/CommonsCliEncryptOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/CommonsCliEncryptOptions.java @@ -35,7 +35,7 @@ import org.codehaus.plexus.interpolation.BasicInterpolator; import org.codehaus.plexus.interpolation.InterpolationException; -import static org.apache.maven.cling.invoker.Utils.createInterpolator; +import static org.apache.maven.cling.invoker.InvokerUtils.createInterpolator; /** * Implementation of {@link EncryptOptions} (base + mvnenc). diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/CommonsCliShellOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/CommonsCliShellOptions.java index 04b1c367555..7266f3e2623 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/CommonsCliShellOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/CommonsCliShellOptions.java @@ -32,7 +32,7 @@ import org.codehaus.plexus.interpolation.BasicInterpolator; import org.codehaus.plexus.interpolation.InterpolationException; -import static org.apache.maven.cling.invoker.Utils.createInterpolator; +import static org.apache.maven.cling.invoker.InvokerUtils.createInterpolator; /** * Implementation of {@link ShellOptions} (base + shell). From 27cb131df14367f7f1ba305f3dc87df38403fea7 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 11 Jan 2025 08:03:09 -0500 Subject: [PATCH 2/7] spotless --- .../main/java/org/apache/maven/cling/invoker/InvokerUtils.java | 3 +-- .../java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index 7c68ef81602..1f0b683a390 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -43,8 +43,7 @@ /** * Various utilities. */ -public final class InvokerUtils -{ +public final class InvokerUtils { private InvokerUtils() {} @Nonnull diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 1ddf28ba8b3..555abad8a96 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -46,9 +46,9 @@ import org.apache.maven.api.services.ToolchainsBuilderResult; import org.apache.maven.api.services.model.ModelProcessor; import org.apache.maven.cling.event.ExecutionEventLogger; +import org.apache.maven.cling.invoker.InvokerUtils; import org.apache.maven.cling.invoker.LookupContext; import org.apache.maven.cling.invoker.LookupInvoker; -import org.apache.maven.cling.invoker.InvokerUtils; import org.apache.maven.cling.transfer.ConsoleMavenTransferListener; import org.apache.maven.cling.transfer.QuietMavenTransferListener; import org.apache.maven.cling.transfer.SimplexTransferListener; From f714ef4b06c941dd6f45acc69b2ffadce4cd9c74 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 11 Jan 2025 08:10:52 -0500 Subject: [PATCH 3/7] Make methods private --- .../maven/cling/invoker/BaseParser.java | 15 ++++++++++- .../maven/cling/invoker/InvokerUtils.java | 25 ++----------------- .../maven/cling/invoker/mvn/MavenInvoker.java | 15 ++++++++++- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java index 8bb2c5079ba..99c2595cf63 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.ServiceLoader; import java.util.function.Function; import java.util.stream.Collectors; @@ -44,6 +45,7 @@ import org.apache.maven.api.cli.ParserException; import org.apache.maven.api.cli.ParserRequest; import org.apache.maven.api.cli.extensions.CoreExtension; +import org.apache.maven.api.services.model.RootLocator; import org.apache.maven.cling.internal.extension.io.CoreExtensionsStaxReader; import org.apache.maven.cling.props.MavenPropertiesLoader; import org.apache.maven.cling.utils.CLIReportingUtils; @@ -59,6 +61,17 @@ public abstract class BaseParser implements Parser { + @Nullable + private static Path findRoot( Path topDirectory ) { + requireNonNull(topDirectory, "topDirectory"); + Path rootDirectory = + ServiceLoader.load( RootLocator.class).iterator().next().findRoot(topDirectory); + if (rootDirectory != null) { + return getCanonicalPath(rootDirectory); + } + return null; + } + @SuppressWarnings("VisibilityModifier") public static class LocalContext { public final ParserRequest parserRequest; @@ -216,7 +229,7 @@ protected Path getTopDirectory(LocalContext context) throws ParserException { @Nullable protected Path getRootDirectory(LocalContext context) throws ParserException { - return InvokerUtils.findRoot(context.topDirectory); + return findRoot(context.topDirectory); } protected Map populateSystemProperties(LocalContext context) throws ParserException { diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index 1f0b683a390..5cbb4c0e258 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -23,14 +23,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.Properties; -import java.util.ServiceLoader; import java.util.function.Function; import org.apache.maven.api.annotations.Nonnull; -import org.apache.maven.api.annotations.Nullable; -import org.apache.maven.api.services.model.RootLocator; import org.apache.maven.cling.logging.Slf4jConfiguration; import org.apache.maven.execution.MavenExecutionRequest; import org.codehaus.plexus.interpolation.AbstractValueSource; @@ -41,7 +37,8 @@ import static java.util.Objects.requireNonNull; /** - * Various utilities. + * Various mostly internal utilities used in org.apache.maven.cling.invoker and its subpackages. + * Not documented, tested, or intended for external uses. */ public final class InvokerUtils { private InvokerUtils() {} @@ -150,22 +147,4 @@ public static int toPlexusLoggingLevel(Slf4jConfiguration.Level level) { }; } - @Nullable - public static Path findRoot(Path topDirectory) { - requireNonNull(topDirectory, "topDirectory"); - Path rootDirectory = - ServiceLoader.load(RootLocator.class).iterator().next().findRoot(topDirectory); - if (rootDirectory != null) { - return getCanonicalPath(rootDirectory); - } - return null; - } - - @Nonnull - public static Path findMandatoryRoot(Path topDirectory) { - requireNonNull(topDirectory, "topDirectory"); - return getCanonicalPath(Optional.ofNullable( - ServiceLoader.load(RootLocator.class).iterator().next().findMandatoryRoot(topDirectory)) - .orElseThrow()); - } } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 555abad8a96..0415ea4bbb6 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -25,6 +25,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.ServiceLoader; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -33,6 +35,7 @@ import org.apache.maven.Maven; import org.apache.maven.api.Constants; import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.cli.InvokerException; import org.apache.maven.api.cli.InvokerRequest; @@ -45,6 +48,7 @@ import org.apache.maven.api.services.ToolchainsBuilderRequest; import org.apache.maven.api.services.ToolchainsBuilderResult; import org.apache.maven.api.services.model.ModelProcessor; +import org.apache.maven.api.services.model.RootLocator; import org.apache.maven.cling.event.ExecutionEventLogger; import org.apache.maven.cling.invoker.InvokerUtils; import org.apache.maven.cling.invoker.LookupContext; @@ -72,6 +76,7 @@ import org.eclipse.aether.transfer.TransferListener; import static java.util.Comparator.comparing; +import static java.util.Objects.requireNonNull; /** * The Maven invoker, that expects whole Maven on classpath and invokes it. @@ -85,6 +90,14 @@ public MavenInvoker(Lookup protoLookup, @Nullable Consumer contex super(protoLookup, contextConsumer); } + @Nonnull + private static Path findMandatoryRoot( Path topDirectory ) { + requireNonNull(topDirectory, "topDirectory"); + return InvokerUtils.getCanonicalPath( Optional.ofNullable( + ServiceLoader.load( RootLocator.class).iterator().next().findMandatoryRoot(topDirectory)) + .orElseThrow()); + } + @Override protected MavenContext createContext(InvokerRequest invokerRequest) throws InvokerException { return new MavenContext(invokerRequest); @@ -252,7 +265,7 @@ protected void populateRequest(MavenContext context, Lookup lookup, MavenExecuti // project present, but we could not determine rootDirectory: extra work needed if (context.invokerRequest.rootDirectory().isEmpty()) { - Path rootDirectory = InvokerUtils.findMandatoryRoot(context.invokerRequest.topDirectory()); + Path rootDirectory = findMandatoryRoot(context.invokerRequest.topDirectory()); request.setMultiModuleProjectDirectory(rootDirectory.toFile()); request.setRootDirectory(rootDirectory); } From ad1bf16b13b1fb1da559a64ad1550dc97385054a Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 11 Jan 2025 08:25:09 -0500 Subject: [PATCH 4/7] wip --- .../maven/cling/invoker/BaseParser.java | 19 ++++++-- .../maven/cling/invoker/InvokerUtils.java | 44 +------------------ .../maven/cling/invoker/LookupInvoker.java | 22 +++++++++- .../maven/cling/invoker/mvn/MavenInvoker.java | 6 +-- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java index 99c2595cf63..c24f18cbe9b 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import org.apache.maven.api.Constants; +import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.cli.InvokerRequest; import org.apache.maven.api.cli.Options; @@ -56,16 +57,15 @@ import static org.apache.maven.cling.invoker.InvokerUtils.getCanonicalPath; import static org.apache.maven.cling.invoker.InvokerUtils.or; import static org.apache.maven.cling.invoker.InvokerUtils.prefix; -import static org.apache.maven.cling.invoker.InvokerUtils.stripLeadingAndTrailingQuotes; import static org.apache.maven.cling.invoker.InvokerUtils.toMap; public abstract class BaseParser implements Parser { @Nullable - private static Path findRoot( Path topDirectory ) { + private static Path findRoot(Path topDirectory) { requireNonNull(topDirectory, "topDirectory"); Path rootDirectory = - ServiceLoader.load( RootLocator.class).iterator().next().findRoot(topDirectory); + ServiceLoader.load(RootLocator.class).iterator().next().findRoot(topDirectory); if (rootDirectory != null) { return getCanonicalPath(rootDirectory); } @@ -197,6 +197,19 @@ protected void mayOverrideDirectorySystemProperty(LocalContext context, String j } } + @Nonnull + private static String stripLeadingAndTrailingQuotes(String str) { + requireNonNull(str, "str"); + final int length = str.length(); + if (length > 1 + && str.startsWith("\"") + && str.endsWith("\"") + && str.substring(1, length - 1).indexOf('"') == -1) { + str = str.substring(1, length - 1); + } + return str; + } + protected Path getTopDirectory(LocalContext context) throws ParserException { // We need to locate the top level project which may be pointed at using // the -f/--file option. diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index 5cbb4c0e258..5fa72f45795 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -32,30 +32,16 @@ import org.codehaus.plexus.interpolation.AbstractValueSource; import org.codehaus.plexus.interpolation.BasicInterpolator; import org.codehaus.plexus.interpolation.StringSearchInterpolator; -import org.codehaus.plexus.logging.Logger; import static java.util.Objects.requireNonNull; /** - * Various mostly internal utilities used in org.apache.maven.cling.invoker and its subpackages. + * Various internal utilities used in org.apache.maven.cling.invoker and its subpackages. * Not documented, tested, or intended for external uses. */ public final class InvokerUtils { private InvokerUtils() {} - @Nonnull - public static String stripLeadingAndTrailingQuotes(String str) { - requireNonNull(str, "str"); - final int length = str.length(); - if (length > 1 - && str.startsWith("\"") - && str.endsWith("\"") - && str.substring(1, length - 1).indexOf('"') == -1) { - str = str.substring(1, length - 1); - } - return str; - } - @Nonnull public static Path getCanonicalPath(Path path) { requireNonNull(path, "path"); @@ -76,16 +62,6 @@ public static Map toMap(Properties properties) { return map; } - @Nonnull - public static Properties toProperties(Map properties) { - requireNonNull(properties, "properties"); - Properties map = new Properties(); - for (String key : properties.keySet()) { - map.put(key, properties.get(key)); - } - return map; - } - @Nonnull public static BasicInterpolator createInterpolator(Collection> properties) { StringSearchInterpolator interpolator = new StringSearchInterpolator(); @@ -129,22 +105,4 @@ public static Function or(Function... callbacks) }; } - public static int toMavenExecutionRequestLoggingLevel(Slf4jConfiguration.Level level) { - requireNonNull(level, "level"); - return switch (level) { - case DEBUG -> MavenExecutionRequest.LOGGING_LEVEL_DEBUG; - case INFO -> MavenExecutionRequest.LOGGING_LEVEL_INFO; - case ERROR -> MavenExecutionRequest.LOGGING_LEVEL_ERROR; - }; - } - - public static int toPlexusLoggingLevel(Slf4jConfiguration.Level level) { - requireNonNull(level, "level"); - return switch (level) { - case DEBUG -> Logger.LEVEL_DEBUG; - case INFO -> Logger.LEVEL_INFO; - case ERROR -> Logger.LEVEL_ERROR; - }; - } - } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index f2bd6e06398..de988f62e3e 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -35,6 +35,7 @@ import org.apache.maven.api.Constants; import org.apache.maven.api.ProtoSession; +import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; import org.apache.maven.api.cli.Invoker; import org.apache.maven.api.cli.InvokerException; @@ -87,8 +88,6 @@ import org.slf4j.spi.LocationAwareLogger; import static java.util.Objects.requireNonNull; -import static org.apache.maven.cling.invoker.InvokerUtils.toMavenExecutionRequestLoggingLevel; -import static org.apache.maven.cling.invoker.InvokerUtils.toProperties; /** * Lookup invoker implementation, that boots up DI container. @@ -462,6 +461,15 @@ protected void init(C context) throws Exception { context.eventSpyDispatcher.init(() -> data); } + private static Properties toProperties(Map properties) { + requireNonNull(properties, "properties"); + Properties map = new Properties(); + for (String key : properties.keySet()) { + map.put(key, properties.get(key)); + } + return map; + } + protected void postCommands(C context) throws Exception { InvokerRequest invokerRequest = context.invokerRequest; Logger logger = context.logger; @@ -674,6 +682,16 @@ protected Path localRepositoryPath(C context) { .resolve("repository"); } + @Nonnull + private static int toMavenExecutionRequestLoggingLevel(Slf4jConfiguration.Level level) { + requireNonNull(level, "level"); + return switch (level) { + case DEBUG -> MavenExecutionRequest.LOGGING_LEVEL_DEBUG; + case INFO -> MavenExecutionRequest.LOGGING_LEVEL_INFO; + case ERROR -> MavenExecutionRequest.LOGGING_LEVEL_ERROR; + }; + } + protected void populateRequest(C context, Lookup lookup, MavenExecutionRequest request) throws Exception { populateRequestFromSettings(request, context.effectiveSettings); diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 0415ea4bbb6..81655659126 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -91,10 +91,10 @@ public MavenInvoker(Lookup protoLookup, @Nullable Consumer contex } @Nonnull - private static Path findMandatoryRoot( Path topDirectory ) { + private static Path findMandatoryRoot(Path topDirectory) { requireNonNull(topDirectory, "topDirectory"); - return InvokerUtils.getCanonicalPath( Optional.ofNullable( - ServiceLoader.load( RootLocator.class).iterator().next().findMandatoryRoot(topDirectory)) + return InvokerUtils.getCanonicalPath(Optional.ofNullable( + ServiceLoader.load(RootLocator.class).iterator().next().findMandatoryRoot(topDirectory)) .orElseThrow()); } From 40d2375cd9ac9dcc45c0f64424d232115b7a3643 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 11 Jan 2025 08:29:22 -0500 Subject: [PATCH 5/7] Reduce public API surface --- .../apache/maven/cling/invoker/InvokerUtils.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index 5fa72f45795..1d16f5d6f7d 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -28,10 +28,10 @@ import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.cling.logging.Slf4jConfiguration; -import org.apache.maven.execution.MavenExecutionRequest; import org.codehaus.plexus.interpolation.AbstractValueSource; import org.codehaus.plexus.interpolation.BasicInterpolator; import org.codehaus.plexus.interpolation.StringSearchInterpolator; +import org.codehaus.plexus.logging.Logger; import static java.util.Objects.requireNonNull; @@ -53,7 +53,7 @@ public static Path getCanonicalPath(Path path) { } @Nonnull - public static Map toMap(Properties properties) { + static Map toMap(Properties properties) { requireNonNull(properties, "properties"); HashMap map = new HashMap<>(); for (String key : properties.stringPropertyNames()) { @@ -105,4 +105,13 @@ public static Function or(Function... callbacks) }; } + @Nonnull + static int toPlexusLoggingLevel(Slf4jConfiguration.Level level) { + requireNonNull(level, "level"); + return switch (level) { + case DEBUG -> Logger.LEVEL_DEBUG; + case INFO -> Logger.LEVEL_INFO; + case ERROR -> Logger.LEVEL_ERROR; + }; + } } From 12d16f8cbbedce7213895cc8f4ac5833c5c56ed2 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 14 Jan 2025 07:23:48 -0500 Subject: [PATCH 6/7] merge master --- .../org/apache/maven/cling/invoker/InvokerUtils.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java index eb9658a0989..367b5e4d573 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/InvokerUtils.java @@ -82,15 +82,6 @@ public Object getValue(String expression) { return interpolator; } - public static Properties toProperties(Map properties) { - requireNonNull(properties, "properties"); - Properties map = new Properties(); - for (String key : properties.keySet()) { - map.put(key, properties.get(key)); - } - return map; - } - @Nonnull public static Interpolator createInterpolator() { return new DefaultInterpolator(); From 92e71ce141b15c9203d50cefd4cce7ed3cbea8da Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 14 Jan 2025 07:32:52 -0500 Subject: [PATCH 7/7] remove null check of non null argument --- .../main/java/org/apache/maven/cling/invoker/BaseParser.java | 1 + .../java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java index a864ee34b06..abe99eaada8 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java @@ -64,6 +64,7 @@ public abstract class BaseParser implements Parser { @Nullable private static Path findRoot(Path topDirectory) { + // TODO is this OK? Tracing through the code it looks like topDirectory is nullable requireNonNull(topDirectory, "topDirectory"); Path rootDirectory = ServiceLoader.load(RootLocator.class).iterator().next().findRoot(topDirectory); diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index 81655659126..43e01178bd7 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -76,7 +76,6 @@ import org.eclipse.aether.transfer.TransferListener; import static java.util.Comparator.comparing; -import static java.util.Objects.requireNonNull; /** * The Maven invoker, that expects whole Maven on classpath and invokes it. @@ -91,8 +90,7 @@ public MavenInvoker(Lookup protoLookup, @Nullable Consumer contex } @Nonnull - private static Path findMandatoryRoot(Path topDirectory) { - requireNonNull(topDirectory, "topDirectory"); + private static Path findMandatoryRoot(@Nonnull Path topDirectory) { return InvokerUtils.getCanonicalPath(Optional.ofNullable( ServiceLoader.load(RootLocator.class).iterator().next().findMandatoryRoot(topDirectory)) .orElseThrow());