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