001 package org.apache.maven.execution; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.io.File; 023 import java.util.HashSet; 024 import java.util.List; 025 import java.util.Set; 026 027 import org.apache.maven.artifact.InvalidRepositoryException; 028 import org.apache.maven.artifact.repository.ArtifactRepository; 029 import org.apache.maven.repository.RepositorySystem; 030 import org.apache.maven.settings.Mirror; 031 import org.apache.maven.settings.Proxy; 032 import org.apache.maven.settings.Server; 033 import org.apache.maven.settings.Settings; 034 import org.apache.maven.settings.SettingsUtils; 035 import org.codehaus.plexus.component.annotations.Component; 036 import org.codehaus.plexus.component.annotations.Requirement; 037 import org.codehaus.plexus.util.StringUtils; 038 039 @Component( role = MavenExecutionRequestPopulator.class ) 040 public class DefaultMavenExecutionRequestPopulator 041 implements MavenExecutionRequestPopulator 042 { 043 044 @Requirement 045 private RepositorySystem repositorySystem; 046 047 public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings ) 048 throws MavenExecutionRequestPopulationException 049 { 050 if ( settings == null ) 051 { 052 return request; 053 } 054 055 request.setOffline( settings.isOffline() ); 056 057 request.setInteractiveMode( settings.isInteractiveMode() ); 058 059 request.setPluginGroups( settings.getPluginGroups() ); 060 061 request.setLocalRepositoryPath( settings.getLocalRepository() ); 062 063 for ( Server server : settings.getServers() ) 064 { 065 server = server.clone(); 066 067 request.addServer( server ); 068 } 069 070 // <proxies> 071 // <proxy> 072 // <active>true</active> 073 // <protocol>http</protocol> 074 // <host>proxy.somewhere.com</host> 075 // <port>8080</port> 076 // <username>proxyuser</username> 077 // <password>somepassword</password> 078 // <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts> 079 // </proxy> 080 // </proxies> 081 082 for ( Proxy proxy : settings.getProxies() ) 083 { 084 if ( !proxy.isActive() ) 085 { 086 continue; 087 } 088 089 proxy = proxy.clone(); 090 091 request.addProxy( proxy ); 092 } 093 094 // <mirrors> 095 // <mirror> 096 // <id>nexus</id> 097 // <mirrorOf>*</mirrorOf> 098 // <url>http://repository.sonatype.org/content/groups/public</url> 099 // </mirror> 100 // </mirrors> 101 102 for ( Mirror mirror : settings.getMirrors() ) 103 { 104 mirror = mirror.clone(); 105 106 request.addMirror( mirror ); 107 } 108 109 request.setActiveProfiles( settings.getActiveProfiles() ); 110 111 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() ) 112 { 113 request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) ); 114 } 115 116 return request; 117 } 118 119 private void populateDefaultPluginGroups( MavenExecutionRequest request ) 120 { 121 request.addPluginGroup( "org.apache.maven.plugins" ); 122 request.addPluginGroup( "org.codehaus.mojo" ); 123 } 124 125 private void injectDefaultRepositories( MavenExecutionRequest request ) 126 throws MavenExecutionRequestPopulationException 127 { 128 Set<String> definedRepositories = getRepoIds( request.getRemoteRepositories() ); 129 130 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) ) 131 { 132 try 133 { 134 request.addRemoteRepository( repositorySystem.createDefaultRemoteRepository() ); 135 } 136 catch ( InvalidRepositoryException e ) 137 { 138 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e ); 139 } 140 } 141 } 142 143 private void injectDefaultPluginRepositories( MavenExecutionRequest request ) 144 throws MavenExecutionRequestPopulationException 145 { 146 Set<String> definedRepositories = getRepoIds( request.getPluginArtifactRepositories() ); 147 148 if ( !definedRepositories.contains( RepositorySystem.DEFAULT_REMOTE_REPO_ID ) ) 149 { 150 try 151 { 152 request.addPluginArtifactRepository( repositorySystem.createDefaultRemoteRepository() ); 153 } 154 catch ( InvalidRepositoryException e ) 155 { 156 throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository.", e ); 157 } 158 } 159 } 160 161 private Set<String> getRepoIds( List<ArtifactRepository> repositories ) 162 { 163 Set<String> repoIds = new HashSet<String>(); 164 165 if ( repositories != null ) 166 { 167 for ( ArtifactRepository repository : repositories ) 168 { 169 repoIds.add( repository.getId() ); 170 } 171 } 172 173 return repoIds; 174 } 175 176 private void processRepositoriesInSettings( MavenExecutionRequest request ) 177 throws MavenExecutionRequestPopulationException 178 { 179 repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() ); 180 repositorySystem.injectProxy( request.getRemoteRepositories(), request.getProxies() ); 181 repositorySystem.injectAuthentication( request.getRemoteRepositories(), request.getServers() ); 182 183 request.setRemoteRepositories( repositorySystem.getEffectiveRepositories( request.getRemoteRepositories() ) ); 184 185 repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() ); 186 repositorySystem.injectProxy( request.getPluginArtifactRepositories(), request.getProxies() ); 187 repositorySystem.injectAuthentication( request.getPluginArtifactRepositories(), request.getServers() ); 188 189 request.setPluginArtifactRepositories( repositorySystem.getEffectiveRepositories( request.getPluginArtifactRepositories() ) ); 190 } 191 192 private void localRepository( MavenExecutionRequest request ) 193 throws MavenExecutionRequestPopulationException 194 { 195 // ------------------------------------------------------------------------ 196 // Local Repository 197 // 198 // 1. Use a value has been passed in via the configuration 199 // 2. Use value in the resultant settings 200 // 3. Use default value 201 // ------------------------------------------------------------------------ 202 203 if ( request.getLocalRepository() == null ) 204 { 205 request.setLocalRepository( createLocalRepository( request ) ); 206 } 207 208 if ( request.getLocalRepositoryPath() == null ) 209 { 210 request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() ); 211 } 212 } 213 214 // ------------------------------------------------------------------------ 215 // Artifact Transfer Mechanism 216 // ------------------------------------------------------------------------ 217 218 public ArtifactRepository createLocalRepository( MavenExecutionRequest request ) 219 throws MavenExecutionRequestPopulationException 220 { 221 String localRepositoryPath = null; 222 223 if ( request.getLocalRepositoryPath() != null ) 224 { 225 localRepositoryPath = request.getLocalRepositoryPath().getAbsolutePath(); 226 } 227 228 if ( StringUtils.isEmpty( localRepositoryPath ) ) 229 { 230 localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath(); 231 } 232 233 try 234 { 235 return repositorySystem.createLocalRepository( new File( localRepositoryPath ) ); 236 } 237 catch ( InvalidRepositoryException e ) 238 { 239 throw new MavenExecutionRequestPopulationException( "Cannot create local repository.", e ); 240 } 241 } 242 243 private void baseDirectory( MavenExecutionRequest request ) 244 { 245 if ( request.getBaseDirectory() == null ) 246 { 247 if ( request.getPom() != null ) 248 { 249 request.setBaseDirectory( request.getPom().getAbsoluteFile().getParentFile() ); 250 } 251 } 252 } 253 254 public MavenExecutionRequest populateDefaults( MavenExecutionRequest request ) 255 throws MavenExecutionRequestPopulationException 256 { 257 baseDirectory( request ); 258 259 localRepository( request ); 260 261 populateDefaultPluginGroups( request ); 262 263 injectDefaultRepositories( request ); 264 265 injectDefaultPluginRepositories( request ); 266 267 processRepositoriesInSettings( request ); 268 269 return request; 270 } 271 272 }