1 package org.apache.maven.shared.repository.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
143 DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
144
145
146 newSession.setCache( new DefaultRepositoryCache() );
147
148
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
185
186
187 protected String resolveRepositoryType( LocalRepository localRepository )
188 {
189 return localRepository.getContentType();
190 }
191
192
193
194
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 }