View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cling.invoker.mvn;
20  
21  import java.io.IOException;
22  import java.nio.charset.Charset;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.stream.Stream;
28  
29  import org.apache.maven.api.cli.Options;
30  import org.apache.maven.api.cli.ParserException;
31  import org.apache.maven.api.cli.mvn.MavenInvokerRequest;
32  import org.apache.maven.api.cli.mvn.MavenOptions;
33  import org.apache.maven.api.cli.mvn.MavenParser;
34  import org.apache.maven.cling.invoker.BaseParser;
35  
36  public abstract class BaseMavenParser<O extends MavenOptions, R extends MavenInvokerRequest<O>> extends BaseParser<O, R>
37          implements MavenParser<R> {
38  
39      @Override
40      protected List<O> parseCliOptions(LocalContext context) throws ParserException, IOException {
41          ArrayList<O> result = new ArrayList<>();
42          // CLI args
43          result.add(parseMavenCliOptions(context.parserRequest.args()));
44          // maven.config; if exists
45          Path mavenConfig = context.rootDirectory != null ? context.rootDirectory.resolve(".mvn/maven.config") : null;
46          if (mavenConfig != null && Files.isRegularFile(mavenConfig)) {
47              result.add(parseMavenConfigOptions(mavenConfig));
48          }
49          return result;
50      }
51  
52      protected O parseMavenCliOptions(List<String> args) throws ParserException {
53          return parseArgs(Options.SOURCE_CLI, args);
54      }
55  
56      protected O parseMavenConfigOptions(Path configFile) throws ParserException, IOException {
57          try (Stream<String> lines = Files.lines(configFile, Charset.defaultCharset())) {
58              List<String> args =
59                      lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#")).toList();
60              O options = parseArgs("maven.config", args);
61              if (options.goals().isPresent()) {
62                  // This file can only contain options, not args (goals or phases)
63                  throw new ParserException("Unrecognized maven.config file entries: "
64                          + options.goals().get());
65              }
66              return options;
67          }
68      }
69  
70      protected abstract O parseArgs(String source, List<String> args) throws ParserException;
71  }