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.execution;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.settings.Mirror;
26  import org.apache.maven.settings.Profile;
27  import org.apache.maven.settings.Proxy;
28  import org.apache.maven.settings.RuntimeInfo;
29  import org.apache.maven.settings.Server;
30  import org.apache.maven.settings.Settings;
31  import org.apache.maven.settings.SettingsUtils;
32  
33  /**
34   * Adapt a {@link MavenExecutionRequest} to a {@link Settings} object for use in the Maven core.
35   * We want to make sure that what is ask for in the execution request overrides what is in the settings.
36   * The CLI feeds into an execution request so if a particular value is present in the execution request
37   * then we will take that over the value coming from the user settings.
38   *
39   * @author Jason van Zyl
40   */
41  class SettingsAdapter extends Settings {
42  
43      private MavenExecutionRequest request;
44  
45      private RuntimeInfo runtimeInfo;
46  
47      SettingsAdapter(MavenExecutionRequest request) {
48          this.request = request;
49  
50          /*
51           * NOTE: Plugins like maven-release-plugin query the path to the settings.xml to pass it into a forked Maven and
52           * the CLI will fail when called with a non-existing settings, so be sure to only point at actual files. Having
53           * a null file should be harmless as this case matches general Maven 2.x behavior...
54           */
55          File userSettings = request.getUserSettingsFile();
56          this.runtimeInfo = new RuntimeInfo((userSettings != null && userSettings.isFile()) ? userSettings : null);
57      }
58  
59      @Override
60      public String getLocalRepository() {
61          if (request.getLocalRepositoryPath() != null) {
62              return request.getLocalRepositoryPath().getAbsolutePath();
63          }
64  
65          return null;
66      }
67  
68      @Override
69      public boolean isInteractiveMode() {
70          return request.isInteractiveMode();
71      }
72  
73      @Override
74      public boolean isOffline() {
75          return request.isOffline();
76      }
77  
78      @Override
79      public List<Proxy> getProxies() {
80          return request.getProxies();
81      }
82  
83      @Override
84      public List<Server> getServers() {
85          return request.getServers();
86      }
87  
88      @Override
89      public List<Mirror> getMirrors() {
90          return request.getMirrors();
91      }
92  
93      @Override
94      public List<Profile> getProfiles() {
95          List<Profile> result = new ArrayList<>();
96          for (org.apache.maven.model.Profile profile : request.getProfiles()) {
97              result.add(SettingsUtils.convertToSettingsProfile(profile));
98          }
99          return result;
100     }
101 
102     @Override
103     public List<String> getActiveProfiles() {
104         return request.getActiveProfiles();
105     }
106 
107     @Override
108     public List<String> getPluginGroups() {
109         return request.getPluginGroups();
110     }
111 }