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