View Javadoc
1   package org.apache.maven.shared.repository.internal;
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 java.io.File;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.project.DefaultProjectBuildingRequest;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.shared.artifact.ArtifactCoordinate;
30  import org.apache.maven.shared.repository.RepositoryManager;
31  import org.apache.maven.shared.repository.RepositoryManagerException;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.eclipse.aether.DefaultRepositoryCache;
35  import org.eclipse.aether.DefaultRepositorySystemSession;
36  import org.eclipse.aether.RepositorySystem;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.artifact.Artifact;
39  import org.eclipse.aether.artifact.DefaultArtifact;
40  import org.eclipse.aether.metadata.DefaultMetadata;
41  import org.eclipse.aether.metadata.Metadata;
42  import org.eclipse.aether.metadata.Metadata.Nature;
43  import org.eclipse.aether.repository.LocalRepository;
44  import org.eclipse.aether.repository.LocalRepositoryManager;
45  
46  /**
47   * 
48   */
49  @Component( role = RepositoryManager.class, hint = "maven31" )
50  public class Maven31RepositoryManager
51      implements RepositoryManager
52  {
53      @Requirement
54      private RepositorySystem repositorySystem;
55  
56      @Requirement
57      private ArtifactHandlerManager artifactHandlerManager;
58  
59      @Override
60      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest,
61                                             org.apache.maven.artifact.Artifact mavenArtifact )
62      {
63          Artifact aetherArtifact;
64          RepositorySystemSession session;
65  
66          // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException
67          try
68          {
69              aetherArtifact = (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
70                                                          org.apache.maven.artifact.Artifact.class, mavenArtifact );
71  
72              session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
73          }
74          catch ( RepositoryManagerException e )
75          {
76              throw new RuntimeException( e.getMessage(), e );
77          }
78  
79          return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact );
80      }
81  
82      @Override
83      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
84      {
85          Artifact aetherArtifact;
86  
87          RepositorySystemSession session;
88  
89          // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException
90          try
91          {
92              aetherArtifact = toArtifact( coordinate );
93  
94              session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
95          }
96          catch ( RepositoryManagerException e )
97          {
98              throw new RuntimeException( e.getMessage(), e );
99          }
100 
101         return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact );
102     }
103     
104     @Override
105     public String getPathForLocalMetadata( ProjectBuildingRequest buildingRequest, ArtifactMetadata metadata )
106     {
107         Metadata aetherMetadata =
108             new DefaultMetadata( metadata.getGroupId(),
109                                  metadata.storedInGroupDirectory() ? null : metadata.getArtifactId(),
110                                  metadata.storedInArtifactVersionDirectory() ? metadata.getBaseVersion() : null,
111                                  "maven-metadata.xml", Nature.RELEASE_OR_SNAPSHOT );
112 
113         RepositorySystemSession session;
114         try
115         {
116             session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
117         }
118         catch ( RepositoryManagerException e )
119         {
120             throw new RuntimeException( e.getMessage(), e );
121         }
122         return session.getLocalRepositoryManager().getPathForLocalMetadata( aetherMetadata );
123     }
124 
125     @Override
126     public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir )
127     {
128         ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest );
129 
130         RepositorySystemSession session;
131         try
132         {
133             session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
134         }
135         catch ( RepositoryManagerException e )
136         {
137             throw new RuntimeException( e.getMessage(), e );
138         }
139 
140         // "clone" session and replace localRepository
141         DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
142 
143         // Clear cache, since we're using a new local repository
144         newSession.setCache( new DefaultRepositoryCache() );
145 
146         // keep same repositoryType
147         String repositoryType = resolveRepositoryType( session.getLocalRepository() );
148 
149         LocalRepositoryManager localRepositoryManager =
150             repositorySystem.newLocalRepositoryManager( newSession, new LocalRepository( basedir, repositoryType ) );
151 
152         newSession.setLocalRepositoryManager( localRepositoryManager );
153 
154         try
155         {
156             Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession );
157         }
158         catch ( RepositoryManagerException e )
159         {
160             throw new RuntimeException( e.getMessage(), e );
161         }
162 
163         return newRequest;
164     }
165 
166     @Override
167     public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest )
168     {
169         RepositorySystemSession session;
170         try
171         {
172             session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
173         }
174         catch ( RepositoryManagerException e )
175         {
176             throw new RuntimeException( e.getMessage(), e );
177         }
178 
179         return session.getLocalRepository().getBasedir();
180     }
181 
182     /**
183      * @param localRepository {@link LocalRepository}
184      * @return the resolved type.
185      */
186     protected String resolveRepositoryType( LocalRepository localRepository )
187     {
188         String repositoryType;
189         if ( "enhanced".equals( localRepository.getContentType() ) )
190         {
191             repositoryType = "default";
192         }
193         else
194         {
195             // this should be "simple"
196             repositoryType = localRepository.getContentType();
197         }
198         return repositoryType;
199     }
200 
201     /**
202      * @param coordinate {@link ArtifactCoordinate}
203      * @return {@link Artifact}
204      */
205     protected Artifact toArtifact( ArtifactCoordinate coordinate )
206     {
207         if ( coordinate == null )
208         {
209             return null;
210         }
211 
212         Artifact result =
213             new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(),
214                                  coordinate.getExtension(), coordinate.getVersion() );
215 
216         return result;
217     }
218 }