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.commons.cli.ParseException;
30  import org.apache.maven.api.cli.Options;
31  import org.apache.maven.api.cli.mvn.MavenOptions;
32  import org.apache.maven.cling.invoker.BaseParser;
33  
34  public class MavenParser extends BaseParser {
35  
36      @Override
37      protected List<Options> parseCliOptions(LocalContext context) {
38          ArrayList<Options> result = new ArrayList<>();
39          // CLI args
40          MavenOptions cliOptions = parseMavenCliOptions(context.parserRequest.args());
41          result.add(cliOptions);
42          // atFile option
43          if (cliOptions.atFile().isPresent()) {
44              Path file = context.cwd.resolve(cliOptions.atFile().orElseThrow());
45              if (Files.isRegularFile(file)) {
46                  result.add(parseMavenAtFileOptions(file));
47              } else {
48                  throw new IllegalArgumentException("Specified file does not exists (" + file + ")");
49              }
50          }
51          // maven.config; if exists
52          Path mavenConfig = context.rootDirectory != null ? context.rootDirectory.resolve(".mvn/maven.config") : null;
53          if (mavenConfig != null && Files.isRegularFile(mavenConfig)) {
54              result.add(parseMavenConfigOptions(mavenConfig));
55          }
56          return result;
57      }
58  
59      protected MavenOptions parseMavenCliOptions(List<String> args) {
60          try {
61              return parseArgs(Options.SOURCE_CLI, args);
62          } catch (ParseException e) {
63              throw new IllegalArgumentException("Failed to parse CLI arguments: " + e.getMessage(), e.getCause());
64          }
65      }
66  
67      protected MavenOptions parseMavenAtFileOptions(Path atFile) {
68          try (Stream<String> lines = Files.lines(atFile, Charset.defaultCharset())) {
69              List<String> args =
70                      lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#")).toList();
71              return parseArgs("atFile", args);
72          } catch (ParseException e) {
73              throw new IllegalArgumentException(
74                      "Failed to parse arguments from file (" + atFile + "): " + e.getMessage(), e.getCause());
75          } catch (IOException e) {
76              throw new IllegalStateException("Error reading config file: " + atFile, e);
77          }
78      }
79  
80      protected MavenOptions parseMavenConfigOptions(Path configFile) {
81          try (Stream<String> lines = Files.lines(configFile, Charset.defaultCharset())) {
82              List<String> args =
83                      lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#")).toList();
84              MavenOptions options = parseArgs("maven.config", args);
85              if (options.goals().isPresent()) {
86                  // This file can only contain options, not args (goals or phases)
87                  throw new IllegalArgumentException("Unrecognized entries in maven.config (" + configFile + ") file: "
88                          + options.goals().get());
89              }
90              return options;
91          } catch (ParseException e) {
92              throw new IllegalArgumentException(
93                      "Failed to parse arguments from maven.config file (" + configFile + "): " + e.getMessage(),
94                      e.getCause());
95          } catch (IOException e) {
96              throw new IllegalStateException("Error reading config file: " + configFile, e);
97          }
98      }
99  
100     protected MavenOptions parseArgs(String source, List<String> args) throws ParseException {
101         return CommonsCliMavenOptions.parse(source, args.toArray(new String[0]));
102     }
103 
104     @Override
105     protected MavenOptions emptyOptions() {
106         try {
107             return CommonsCliMavenOptions.parse(Options.SOURCE_CLI, new String[0]);
108         } catch (ParseException e) {
109             throw new IllegalArgumentException(e);
110         }
111     }
112 
113     @Override
114     protected MavenInvokerRequest getInvokerRequest(LocalContext context) {
115         return new MavenInvokerRequest(
116                 context.parserRequest,
117                 context.parsingFailed,
118                 context.cwd,
119                 context.installationDirectory,
120                 context.userHomeDirectory,
121                 context.userProperties,
122                 context.systemProperties,
123                 context.topDirectory,
124                 context.rootDirectory,
125                 context.extensions,
126                 (MavenOptions) context.options);
127     }
128 
129     @Override
130     @SuppressWarnings({"unchecked", "rawtypes"})
131     protected MavenOptions assembleOptions(List<Options> parsedOptions) {
132         return LayeredMavenOptions.layerMavenOptions((List) parsedOptions);
133     }
134 }