View Javadoc
1   package org.apache.maven.internal.aether;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.RepositoryUtils;
23  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
24  import org.apache.maven.bridge.MavenRepositorySystem;
25  import org.apache.maven.eventspy.internal.EventSpyDispatcher;
26  import org.apache.maven.execution.MavenExecutionRequest;
27  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
28  import org.apache.maven.rtinfo.RuntimeInformation;
29  import org.apache.maven.settings.Mirror;
30  import org.apache.maven.settings.Proxy;
31  import org.apache.maven.settings.Server;
32  import org.apache.maven.settings.building.SettingsProblem;
33  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
34  import org.apache.maven.settings.crypto.SettingsDecrypter;
35  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
36  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
37  import org.codehaus.plexus.logging.Logger;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  import org.eclipse.aether.ConfigurationProperties;
40  import org.eclipse.aether.DefaultRepositorySystemSession;
41  import org.eclipse.aether.RepositorySystem;
42  import org.eclipse.aether.repository.LocalRepository;
43  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
44  import org.eclipse.aether.repository.RepositoryPolicy;
45  import org.eclipse.aether.repository.WorkspaceReader;
46  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
47  import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
48  import org.eclipse.aether.util.repository.AuthenticationBuilder;
49  import org.eclipse.aether.util.repository.DefaultAuthenticationSelector;
50  import org.eclipse.aether.util.repository.DefaultMirrorSelector;
51  import org.eclipse.aether.util.repository.DefaultProxySelector;
52  import org.eclipse.aether.util.repository.SimpleResolutionErrorPolicy;
53  import org.eclipse.sisu.Nullable;
54  
55  import javax.inject.Inject;
56  import javax.inject.Named;
57  import java.util.LinkedHashMap;
58  import java.util.Map;
59  
60  /**
61   * @since 3.3.0
62   */
63  @Named
64  public class DefaultRepositorySystemSessionFactory
65  {
66      @Inject
67      private Logger logger;
68  
69      @Inject
70      private ArtifactHandlerManager artifactHandlerManager;
71  
72      @Inject
73      private RepositorySystem repoSystem;
74  
75      @Inject
76      @Nullable
77      @Named( "simple" )
78      private LocalRepositoryManagerFactory simpleLocalRepoMgrFactory;
79  
80      @Inject
81      @Nullable
82      @Named( "ide" )
83      private WorkspaceReader workspaceRepository;
84  
85      @Inject
86      private SettingsDecrypter settingsDecrypter;
87  
88      @Inject
89      private EventSpyDispatcher eventSpyDispatcher;
90  
91      @Inject
92      MavenRepositorySystem mavenRepositorySystem;
93  
94      @Inject
95      private RuntimeInformation runtimeInformation;
96  
97      public DefaultRepositorySystemSession newRepositorySession( MavenExecutionRequest request )
98      {
99          DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
100 
101         session.setCache( request.getRepositoryCache() );
102 
103         Map<Object, Object> configProps = new LinkedHashMap<>();
104         configProps.put( ConfigurationProperties.USER_AGENT, getUserAgent() );
105         configProps.put( ConfigurationProperties.INTERACTIVE, request.isInteractiveMode() );
106         configProps.put( "maven.startTime", request.getStartTime() );
107         configProps.putAll( request.getSystemProperties() );
108         configProps.putAll( request.getUserProperties() );
109 
110         session.setOffline( request.isOffline() );
111         session.setChecksumPolicy( request.getGlobalChecksumPolicy() );
112         if ( request.isNoSnapshotUpdates() )
113         {
114             session.setUpdatePolicy( RepositoryPolicy.UPDATE_POLICY_NEVER );
115         }
116         else if ( request.isUpdateSnapshots() )
117         {
118             session.setUpdatePolicy( RepositoryPolicy.UPDATE_POLICY_ALWAYS );
119         }
120         else
121         {
122             session.setUpdatePolicy( null );
123         }
124 
125         int errorPolicy = 0;
126         errorPolicy |= request.isCacheNotFound() ? ResolutionErrorPolicy.CACHE_NOT_FOUND
127             : ResolutionErrorPolicy.CACHE_DISABLED;
128         errorPolicy |= request.isCacheTransferError() ? ResolutionErrorPolicy.CACHE_TRANSFER_ERROR
129             : ResolutionErrorPolicy.CACHE_DISABLED;
130         session.setResolutionErrorPolicy(
131             new SimpleResolutionErrorPolicy( errorPolicy, errorPolicy | ResolutionErrorPolicy.CACHE_NOT_FOUND ) );
132 
133         session.setArtifactTypeRegistry( RepositoryUtils.newArtifactTypeRegistry( artifactHandlerManager ) );
134 
135         LocalRepository localRepo = new LocalRepository( request.getLocalRepository().getBasedir() );
136 
137         if ( request.isUseLegacyLocalRepository() )
138         {
139             try
140             {
141                 session.setLocalRepositoryManager( simpleLocalRepoMgrFactory.newInstance( session, localRepo ) );
142                 logger.info( "Disabling enhanced local repository: using legacy is strongly discouraged to ensure"
143                                  + " build reproducibility." );
144 
145             }
146             catch ( NoLocalRepositoryManagerException e )
147             {
148                 logger.error( "Failed to configure legacy local repository: falling back to default" );
149                 session.setLocalRepositoryManager( repoSystem.newLocalRepositoryManager( session, localRepo ) );
150             }
151         }
152         else
153         {
154             session.setLocalRepositoryManager( repoSystem.newLocalRepositoryManager( session, localRepo ) );
155         }
156 
157         if ( request.getWorkspaceReader() != null )
158         {
159             session.setWorkspaceReader( request.getWorkspaceReader() );
160         }
161         else
162         {
163             session.setWorkspaceReader( workspaceRepository );
164         }
165 
166         DefaultSettingsDecryptionRequest decrypt = new DefaultSettingsDecryptionRequest();
167         decrypt.setProxies( request.getProxies() );
168         decrypt.setServers( request.getServers() );
169         SettingsDecryptionResult decrypted = settingsDecrypter.decrypt( decrypt );
170 
171         if ( logger.isDebugEnabled() )
172         {
173             for ( SettingsProblem problem : decrypted.getProblems() )
174             {
175                 logger.debug( problem.getMessage(), problem.getException() );
176             }
177         }
178 
179         DefaultMirrorSelector mirrorSelector = new DefaultMirrorSelector();
180         for ( Mirror mirror : request.getMirrors() )
181         {
182             mirrorSelector.add( mirror.getId(), mirror.getUrl(), mirror.getLayout(), false, mirror.isBlocked(),
183                                 mirror.getMirrorOf(), mirror.getMirrorOfLayouts() );
184         }
185         session.setMirrorSelector( mirrorSelector );
186 
187         DefaultProxySelector proxySelector = new DefaultProxySelector();
188         for ( Proxy proxy : decrypted.getProxies() )
189         {
190             AuthenticationBuilder authBuilder = new AuthenticationBuilder();
191             authBuilder.addUsername( proxy.getUsername() ).addPassword( proxy.getPassword() );
192             proxySelector.add(
193                 new org.eclipse.aether.repository.Proxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(),
194                                                          authBuilder.build() ), proxy.getNonProxyHosts() );
195         }
196         session.setProxySelector( proxySelector );
197 
198         DefaultAuthenticationSelector authSelector = new DefaultAuthenticationSelector();
199         for ( Server server : decrypted.getServers() )
200         {
201             AuthenticationBuilder authBuilder = new AuthenticationBuilder();
202             authBuilder.addUsername( server.getUsername() ).addPassword( server.getPassword() );
203             authBuilder.addPrivateKey( server.getPrivateKey(), server.getPassphrase() );
204             authSelector.add( server.getId(), authBuilder.build() );
205 
206             if ( server.getConfiguration() != null )
207             {
208                 Xpp3Dom dom = (Xpp3Dom) server.getConfiguration();
209                 for ( int i = dom.getChildCount() - 1; i >= 0; i-- )
210                 {
211                     Xpp3Dom child = dom.getChild( i );
212                     if ( "wagonProvider".equals( child.getName() ) )
213                     {
214                         dom.removeChild( i );
215                     }
216                 }
217 
218                 XmlPlexusConfiguration config = new XmlPlexusConfiguration( dom );
219                 configProps.put( "aether.connector.wagon.config." + server.getId(), config );
220             }
221 
222             configProps.put( "aether.connector.perms.fileMode." + server.getId(), server.getFilePermissions() );
223             configProps.put( "aether.connector.perms.dirMode." + server.getId(), server.getDirectoryPermissions() );
224         }
225         session.setAuthenticationSelector( authSelector );
226 
227         session.setTransferListener( request.getTransferListener() );
228 
229         session.setRepositoryListener( eventSpyDispatcher.chainListener( new LoggingRepositoryListener( logger ) ) );
230 
231         session.setUserProperties( request.getUserProperties() );
232         session.setSystemProperties( request.getSystemProperties() );
233         session.setConfigProperties( configProps );
234 
235         mavenRepositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() );
236         mavenRepositorySystem.injectProxy( session, request.getRemoteRepositories() );
237         mavenRepositorySystem.injectAuthentication( session, request.getRemoteRepositories() );
238 
239         mavenRepositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() );
240         mavenRepositorySystem.injectProxy( session, request.getPluginArtifactRepositories() );
241         mavenRepositorySystem.injectAuthentication( session, request.getPluginArtifactRepositories() );
242 
243         return session;
244     }
245 
246     private String getUserAgent()
247     {
248         String version = runtimeInformation.getMavenVersion();
249         version = version.isEmpty() ? version : "/" + version;
250         return "Apache-Maven" + version + " (Java " + System.getProperty( "java.version" ) + "; "
251             + System.getProperty( "os.name" ) + " " + System.getProperty( "os.version" ) + ")";
252     }
253 
254 }