ikxeno commented on code in PR #13276: URL: https://github.com/apache/maven/pull/13276#discussion_r4116922519
########## impl/maven-cli/src/main/java/org/apache/maven/cling/MavenValCling.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.cli.Invoker; +import org.apache.maven.api.cli.Parser; +import org.apache.maven.api.cli.ParserRequest; +import org.apache.maven.cling.invoker.ProtoLookup; +import org.apache.maven.cling.invoker.mvnval.ValidateInvoker; +import org.apache.maven.cling.invoker.mvnval.ValidateParser; +import org.codehaus.plexus.classworlds.ClassWorld; + +/** + * Maven POM validation CLI "new-gen". + */ +public class MavenValCling extends ClingSupport { + /** + * "Normal" Java entry point. Note: Maven uses ClassWorld Launcher and this entry point is NOT used under normal + * circumstances. + */ + public static void main(String[] args) throws IOException { + int exitCode = new MavenValCling().run(args, null, null, null, false); + System.exit(exitCode); + } + + /** + * ClassWorld Launcher "enhanced" entry point: returning exitCode and accepts Class World. + */ + public static int main(String[] args, ClassWorld world) throws IOException { + return new MavenValCling(world).run(args, null, null, null, false); + } + + /** + * ClassWorld Launcher "embedded" entry point: returning exitCode and accepts Class World and streams. + */ + public static int main( + String[] args, + ClassWorld world, + @Nullable InputStream stdIn, + @Nullable OutputStream stdOut, + @Nullable OutputStream stdErr) + throws IOException { + return new MavenValCling(world).run(args, stdIn, stdOut, stdErr, true); + } + + public MavenValCling() { + super(); + } + + public MavenValCling(ClassWorld classWorld) { + super(classWorld); + } + + @Override + protected Invoker createInvoker() { + return new ValidateInvoker( + ProtoLookup.builder().addMapping(ClassWorld.class, classWorld).build(), null); Review Comment: Right, and worse: `maven.ext.class.path` in `.mvn/maven-user.properties` was read too, so 6781eef stands up no container at all. ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnval/CommonsCliValidateOptions.java: ########## @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnval; + +import java.util.List; +import java.util.Optional; +import java.util.function.Consumer; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.cli.Options; +import org.apache.maven.api.cli.ParserRequest; +import org.apache.maven.api.cli.mvnval.ValidateOptions; +import org.apache.maven.cling.invoker.CommonsCliOptions; + +/** + * Implementation of {@link ValidateOptions} (base + mvnval). + */ +public class CommonsCliValidateOptions extends CommonsCliOptions implements ValidateOptions { + + public static CommonsCliValidateOptions parse(String[] args) throws ParseException { + CLIManager cliManager = new CLIManager(); + CommonsCliValidateOptions options = + new CommonsCliValidateOptions(Options.SOURCE_CLI, cliManager, cliManager.parse(args)); + // Reject a bad --format here, so the user gets a usage error rather than a failure + // after the container has been built. + try { + options.format().ifPresent(OutputFormat::parse); + } catch (IllegalArgumentException e) { + throw new ParseException(e.getMessage()); + } + return options; + } Review Comment: Correct, the check moved into the invoker and `mvnval --format bogus` exits 2 now. ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnval/ValidateParser.java: ########## @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnval; + +import org.apache.commons.cli.ParseException; +import org.apache.maven.api.cli.Options; +import org.apache.maven.cling.invoker.BaseParser; + +public class ValidateParser extends BaseParser { + + @Override + protected Options parseCliOptions(LocalContext context) { + try { + return CommonsCliValidateOptions.parse(context.parserRequest.args().toArray(new String[0])); + } catch (ParseException e) { + throw new IllegalArgumentException("Failed to parse command line options: " + e.getMessage(), e); Review Comment: Same one, fixed in 1bee3a8. ########## api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilder.java: ########## @@ -38,6 +38,33 @@ public interface ModelBuilder extends Service { interface ModelBuilderSession { ModelBuilderResult build(ModelBuilderRequest request) throws ModelBuilderException; + + /** + * Reads and validates the raw model, without inheritance, interpolation or profile injection. + * <p> + * Unlike {@link #build(ModelBuilderRequest)} this does not throw when validation reports + * errors: the result carries errors and warnings alike, so a caller that only wants to + * report on a POM can see warnings that come with no error. A problem severe enough that + * the model cannot be read at all is still thrown. + * <p> + * Reading one raw model can need others: a subproject may leave out its parent version, + * which is then taken from the parent's own file model. An implementation may therefore + * read files besides the one the request names, up to the project root. Nothing is + * resolved from a repository and nothing is downloaded. + * <p> + * Only the source, the file model, the raw model and the problems are populated on the + * result. The accessors that describe a fully built model, such as + * {@link ModelBuilderResult#getEffectiveModel()}, are not. + * + * @param request the request containing the parameters for reading the model + * @return the result, carrying the raw model and the problems collected + * @throws ModelBuilderException if the model cannot be read at all + * @throws UnsupportedOperationException if this implementation does not support it + * @since 4.1.0 + */ + default ModelBuilderResult buildRaw(ModelBuilderRequest request) throws ModelBuilderException { + throw new UnsupportedOperationException(getClass().getName() + " does not support reading a raw model"); + } Review Comment: A default only breaks implementors that call it, an abstract method breaks all of them, which is why it is a default. ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnval/ValidateInvoker.java: ########## @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnval; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.apache.maven.api.Session; +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.cli.InvokerRequest; +import org.apache.maven.api.cli.mvnval.ValidateOptions; +import org.apache.maven.api.services.Lookup; +import org.apache.maven.api.services.ModelBuilder; +import org.apache.maven.api.services.ModelBuilderException; +import org.apache.maven.api.services.ModelBuilderRequest; +import org.apache.maven.api.services.ModelBuilderResult; +import org.apache.maven.api.services.ModelProblem; +import org.apache.maven.api.services.Sources; +import org.apache.maven.cling.invoker.CoreExtensionSelector; +import org.apache.maven.cling.invoker.LookupContext; +import org.apache.maven.cling.invoker.LookupInvoker; +import org.apache.maven.impl.standalone.ApiRunner; + +/** + * Validates POM files without building them. + * <p> + * Validation stops after {@link ModelBuilder.ModelBuilderSession#buildRaw}, so no parent is + * resolved and the verdict depends on the files alone, never on the network. The problems it can + * reach are therefore a subset: a dependency whose version comes from the parent's + * {@code dependencyManagement} is only checked later, in {@code validateEffectiveModel}. + */ +public class ValidateInvoker extends LookupInvoker<ValidateContext> { + + public static final int OK = 0; + public static final int ERROR = 1; + + /** Bad user input, as in {@code mvnenc} and {@code mvnup}. */ + public static final int BAD_OPERATION = 2; + + /** + * Nothing was rejected, but something was reported. Deliberately not 2: the CLI itself exits + * with 2 when a tool fails in a way it does not handle, and a gate must be able to tell "this + * POM has warnings" from "mvnval broke". + */ + public static final int WARNINGS = 3; + + public ValidateInvoker(Lookup protoLookup, @Nullable Consumer<LookupContext> contextConsumer) { + super(protoLookup, contextConsumer); + } + + @Override + protected ValidateContext createContext(InvokerRequest invokerRequest) { + return new ValidateContext( + invokerRequest, (ValidateOptions) invokerRequest.options().orElse(null)); + } + + @Override + protected int execute(ValidateContext context) throws Exception { + OutputFormat format; + try { + // The parser checks --format too, but options are interpolated after parsing, so + // ${...} can still turn into anything by the time it gets here. + format = context.options().format().map(OutputFormat::parse).orElse(OutputFormat.TEXT); + } catch (IllegalArgumentException e) { + context.logger.error(e.getMessage()); + return BAD_OPERATION; + } + + List<Report> reports = validate(resolvePoms(context), createSession()); Review Comment: The comment was wrong and has been reworded. ########## impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java: ########## @@ -1053,14 +1097,17 @@ private void loadFromRoot(Path root, Path top) { loadFilePom(executor, top, root, Set.of(), r); } if (result.getFileModel() == null && !Objects.equals(top, root)) { - logger.warn( - "The top project ({}) cannot be found in the reactor from root project ({}). " - + "Make sure the root directory is correct (a missing '.mvn' directory in the root " - + "project is the most common cause) and the project is correctly included " - + "in the reactor (missing activated profiles, command line options, etc.). For this " - + "build, the top project will be used as the root project.", - top, - root); + // Only a warning when a build is what was asked for. A caller reading one POM has + // no reactor to get wrong, and the retry below is routine there, not a misconfiguration. + (reportReactorLoadFailures ? logger.atWarn() : logger.atDebug()) + .log( + "The top project ({}) cannot be found in the reactor from root project ({}). " + + "Make sure the root directory is correct (a missing '.mvn' directory in the root " + + "project is the most common cause) and the project is correctly included " + + "in the reactor (missing activated profiles, command line options, etc.). For this " + + "build, the top project will be used as the root project.", Review Comment: Fair, `derive()` copies the flag now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
