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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.api.Constants;
32  import org.apache.maven.artifact.InvalidRepositoryException;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  import org.apache.maven.bridge.MavenRepositorySystem;
35  import org.apache.maven.settings.Mirror;
36  import org.apache.maven.settings.Proxy;
37  import org.apache.maven.settings.Repository;
38  import org.apache.maven.settings.Server;
39  import org.apache.maven.settings.Settings;
40  import org.apache.maven.settings.SettingsUtils;
41  import org.apache.maven.toolchain.model.PersistedToolchains;
42  import org.apache.maven.toolchain.model.ToolchainModel;
43  
44  /**
45   * Assists in populating an execution request for invocation of Maven.
46   */
47  @Named
48  @Singleton
49  public class DefaultMavenExecutionRequestPopulator implements MavenExecutionRequestPopulator {
50  
51      private final MavenRepositorySystem repositorySystem;
52  
53      @Inject
54      public DefaultMavenExecutionRequestPopulator(MavenRepositorySystem repositorySystem) {
55          this.repositorySystem = repositorySystem;
56      }
57  
58      @Override
59      public MavenExecutionRequest populateFromToolchains(MavenExecutionRequest request, PersistedToolchains toolchains)
60              throws MavenExecutionRequestPopulationException {
61          if (toolchains != null) {
62              Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<>(2);
63  
64              for (ToolchainModel model : toolchains.getToolchains()) {
65                  if (!groupedToolchains.containsKey(model.getType())) {
66                      groupedToolchains.put(model.getType(), new ArrayList<>());
67                  }
68  
69                  groupedToolchains.get(model.getType()).add(model);
70              }
71  
72              request.setToolchains(groupedToolchains);
73          }
74          return request;
75      }
76  
77      @Override
78      public MavenExecutionRequest populateDefaults(MavenExecutionRequest request)
79              throws MavenExecutionRequestPopulationException {
80          baseDirectory(request);
81  
82          localRepository(request);
83  
84          populateDefaultPluginGroups(request);
85  
86          return request;
87      }
88  
89      //
90      //
91      //
92  
93      private void populateDefaultPluginGroups(MavenExecutionRequest request) {
94          request.addPluginGroup("org.apache.maven.plugins");
95          request.addPluginGroup("org.codehaus.mojo");
96      }
97  
98      private void localRepository(MavenExecutionRequest request) throws MavenExecutionRequestPopulationException {
99          // ------------------------------------------------------------------------
100         // Local Repository
101         //
102         // 1. Use a value has been passed in via the configuration
103         // 2. Use value in the resultant settings
104         // 3. Use default value
105         // ------------------------------------------------------------------------
106 
107         if (request.getLocalRepository() == null) {
108             request.setLocalRepository(createLocalRepository(request));
109         }
110 
111         if (request.getLocalRepositoryPath() == null) {
112             request.setLocalRepositoryPath(new File(request.getLocalRepository().getBasedir()).getAbsoluteFile());
113         }
114     }
115 
116     // ------------------------------------------------------------------------
117     // Artifact Transfer Mechanism
118     // ------------------------------------------------------------------------
119 
120     private ArtifactRepository createLocalRepository(MavenExecutionRequest request)
121             throws MavenExecutionRequestPopulationException {
122         String localRepositoryPath = null;
123 
124         if (request.getLocalRepositoryPath() != null) {
125             localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath();
126         }
127 
128         if (localRepositoryPath == null || localRepositoryPath.isEmpty()) {
129             String path = request.getUserProperties().getProperty(Constants.MAVEN_USER_CONF);
130             if (path == null) {
131                 path = request.getSystemProperties().getProperty("user.home") + File.separator + ".m2";
132             }
133             localRepositoryPath = new File(path, "repository").getAbsolutePath();
134         }
135 
136         try {
137             return repositorySystem.createLocalRepository(new File(localRepositoryPath));
138         } catch (Exception e) {
139             throw new MavenExecutionRequestPopulationException("Cannot create local repository.", e);
140         }
141     }
142 
143     private void baseDirectory(MavenExecutionRequest request) {
144         if (request.getBaseDirectory() == null && request.getPom() != null) {
145             request.setBaseDirectory(request.getPom().getAbsoluteFile().getParentFile());
146         }
147     }
148 
149     /*if_not[MAVEN4]*/
150 
151     @Override
152     @Deprecated
153     public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
154             throws MavenExecutionRequestPopulationException {
155         if (settings == null) {
156             return request;
157         }
158 
159         request.setOffline(settings.isOffline());
160 
161         request.setInteractiveMode(settings.isInteractiveMode());
162 
163         request.setPluginGroups(settings.getPluginGroups());
164 
165         request.setLocalRepositoryPath(settings.getLocalRepository());
166 
167         for (Server server : settings.getServers()) {
168             server = server.clone();
169 
170             request.addServer(server);
171         }
172 
173         //  <proxies>
174         //    <proxy>
175         //      <active>true</active>
176         //      <protocol>http</protocol>
177         //      <host>proxy.somewhere.com</host>
178         //      <port>8080</port>
179         //      <username>proxyuser</username>
180         //      <password>somepassword</password>
181         //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
182         //    </proxy>
183         //  </proxies>
184 
185         for (Proxy proxy : settings.getProxies()) {
186             if (!proxy.isActive()) {
187                 continue;
188             }
189 
190             proxy = proxy.clone();
191 
192             request.addProxy(proxy);
193         }
194 
195         // <mirrors>
196         //   <mirror>
197         //     <id>nexus</id>
198         //     <mirrorOf>*</mirrorOf>
199         //     <url>http://repository.sonatype.org/content/groups/public</url>
200         //   </mirror>
201         // </mirrors>
202 
203         for (Mirror mirror : settings.getMirrors()) {
204             mirror = mirror.clone();
205 
206             request.addMirror(mirror);
207         }
208 
209         request.setActiveProfiles(settings.getActiveProfiles());
210 
211         for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
212             request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile));
213 
214             if (settings.getActiveProfiles().contains(rawProfile.getId())) {
215                 List<Repository> remoteRepositories = rawProfile.getRepositories();
216                 for (Repository remoteRepository : remoteRepositories) {
217                     try {
218                         request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
219                     } catch (InvalidRepositoryException e) {
220                         // do nothing for now
221                     }
222                 }
223 
224                 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
225                 for (Repository pluginRepo : pluginRepositories) {
226                     try {
227                         request.addPluginArtifactRepository(MavenRepositorySystem.buildArtifactRepository(pluginRepo));
228                     } catch (InvalidRepositoryException e) {
229                         // do nothing for now
230                     }
231                 }
232             }
233         }
234 
235         return request;
236     }
237 
238     /*end[MAVEN4]*/
239 
240 }