001package org.apache.maven.repository; 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 022import java.io.File; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.maven.artifact.Artifact; 030import org.apache.maven.artifact.DefaultArtifact; 031import org.apache.maven.artifact.InvalidRepositoryException; 032import org.apache.maven.artifact.factory.ArtifactFactory; 033import org.apache.maven.artifact.repository.ArtifactRepository; 034import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; 035import org.apache.maven.artifact.repository.MavenArtifactRepository; 036import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 037import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; 038import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; 039import org.apache.maven.artifact.resolver.ArtifactResolutionResult; 040import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 041import org.apache.maven.artifact.versioning.VersionRange; 042import org.apache.maven.model.Dependency; 043import org.apache.maven.model.Model; 044import org.apache.maven.model.Plugin; 045import org.apache.maven.model.Repository; 046import org.apache.maven.model.io.ModelReader; 047import org.apache.maven.project.artifact.ArtifactWithDependencies; 048import org.apache.maven.settings.Mirror; 049import org.apache.maven.settings.Proxy; 050import org.apache.maven.settings.Server; 051import org.codehaus.plexus.component.annotations.Component; 052import org.codehaus.plexus.component.annotations.Requirement; 053import org.codehaus.plexus.util.FileUtils; 054import org.codehaus.plexus.util.StringUtils; 055import org.eclipse.aether.RepositorySystemSession; 056 057/** 058 * @author Benjamin Bentmann 059 */ 060@Component( role = RepositorySystem.class ) 061public class TestRepositorySystem 062 implements RepositorySystem 063{ 064 065 @Requirement 066 private ModelReader modelReader; 067 068 @Requirement 069 private ArtifactFactory artifactFactory; 070 071 public ArtifactRepository buildArtifactRepository( Repository repository ) 072 throws InvalidRepositoryException 073 { 074 return new MavenArtifactRepository( repository.getId(), repository.getUrl(), new DefaultRepositoryLayout(), 075 new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() ); 076 } 077 078 public Artifact createArtifact( String groupId, String artifactId, String version, String packaging ) 079 { 080 return createArtifact( groupId, artifactId, version, null, packaging ); 081 } 082 083 public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) 084 { 085 return new DefaultArtifact( groupId, artifactId, version, scope, type, null, new TestArtifactHandler( type ) ); 086 } 087 088 public ArtifactRepository createArtifactRepository( String id, String url, 089 ArtifactRepositoryLayout repositoryLayout, 090 ArtifactRepositoryPolicy snapshots, 091 ArtifactRepositoryPolicy releases ) 092 { 093 return new MavenArtifactRepository( id, url, repositoryLayout, snapshots, releases ); 094 } 095 096 public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type, 097 String classifier ) 098 { 099 return new DefaultArtifact( groupId, artifactId, version, null, type, classifier, 100 new TestArtifactHandler( type ) ); 101 } 102 103 public ArtifactRepository createDefaultLocalRepository() 104 throws InvalidRepositoryException 105 { 106 return createLocalRepository( new File( System.getProperty( "basedir", "" ), "target/local-repo" ).getAbsoluteFile() ); 107 } 108 109 public ArtifactRepository createDefaultRemoteRepository() 110 throws InvalidRepositoryException 111 { 112 return new MavenArtifactRepository( DEFAULT_REMOTE_REPO_ID, "file://" 113 + new File( System.getProperty( "basedir", "" ), "src/test/remote-repo" ).toURI().getPath(), 114 new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), 115 new ArtifactRepositoryPolicy() ); 116 } 117 118 public Artifact createDependencyArtifact( Dependency dependency ) 119 { 120 Artifact artifact = 121 new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), 122 dependency.getScope(), dependency.getType(), dependency.getClassifier(), 123 new TestArtifactHandler( dependency.getType() ) ); 124 125 if ( Artifact.SCOPE_SYSTEM.equals( dependency.getScope() ) ) 126 { 127 artifact.setFile( new File( dependency.getSystemPath() ) ); 128 artifact.setResolved( true ); 129 } 130 131 return artifact; 132 } 133 134 public ArtifactRepository createLocalRepository( File localRepository ) 135 throws InvalidRepositoryException 136 { 137 return new MavenArtifactRepository( DEFAULT_LOCAL_REPO_ID, "file://" + localRepository.toURI().getPath(), 138 new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), 139 new ArtifactRepositoryPolicy() ); 140 } 141 142 public Artifact createPluginArtifact( Plugin plugin ) 143 { 144 VersionRange versionRange; 145 try 146 { 147 String version = plugin.getVersion(); 148 if ( StringUtils.isEmpty( version ) ) 149 { 150 version = "RELEASE"; 151 } 152 versionRange = VersionRange.createFromVersionSpec( version ); 153 } 154 catch ( InvalidVersionSpecificationException e ) 155 { 156 return null; 157 } 158 159 return artifactFactory.createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), versionRange ); 160 } 161 162 public Artifact createProjectArtifact( String groupId, String artifactId, String version ) 163 { 164 return createArtifact( groupId, artifactId, version, "pom" ); 165 } 166 167 public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories ) 168 { 169 return repositories; 170 } 171 172 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors ) 173 { 174 return null; 175 } 176 177 public void injectAuthentication( List<ArtifactRepository> repositories, List<Server> servers ) 178 { 179 } 180 181 public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors ) 182 { 183 } 184 185 public void injectProxy( List<ArtifactRepository> repositories, List<Proxy> proxies ) 186 { 187 } 188 189 public void publish( ArtifactRepository repository, File source, String remotePath, 190 ArtifactTransferListener transferListener ) 191 throws ArtifactTransferFailedException 192 { 193 // TODO Auto-generated method stub 194 195 } 196 197 public ArtifactResolutionResult resolve( ArtifactResolutionRequest request ) 198 { 199 ArtifactResolutionResult result = new ArtifactResolutionResult(); 200 201 if ( request.isResolveRoot() ) 202 { 203 try 204 { 205 resolve( request.getArtifact(), request ); 206 result.addArtifact( request.getArtifact() ); 207 } 208 catch ( IOException e ) 209 { 210 result.addMissingArtifact( request.getArtifact() ); 211 } 212 } 213 214 if ( request.isResolveTransitively() ) 215 { 216 Map<String, Artifact> artifacts = new LinkedHashMap<String, Artifact>(); 217 218 if ( request.getArtifactDependencies() != null ) 219 { 220 for ( Artifact artifact : request.getArtifactDependencies() ) 221 { 222 artifacts.put( artifact.getDependencyConflictId(), artifact ); 223 } 224 } 225 226 List<Dependency> dependencies = new ArrayList<Dependency>(); 227 if ( request.getArtifact() instanceof ArtifactWithDependencies ) 228 { 229 dependencies = ( (ArtifactWithDependencies) request.getArtifact() ).getDependencies(); 230 } 231 else 232 { 233 Artifact pomArtifact = 234 createProjectArtifact( request.getArtifact().getGroupId(), request.getArtifact().getArtifactId(), 235 request.getArtifact().getVersion() ); 236 File pomFile = 237 new File( request.getLocalRepository().getBasedir(), 238 request.getLocalRepository().pathOf( pomArtifact ) ); 239 240 try 241 { 242 Model model = modelReader.read( pomFile, null ); 243 244 dependencies = model.getDependencies(); 245 } 246 catch ( IOException e ) 247 { 248 e.printStackTrace(); 249 } 250 } 251 252 for ( Dependency dependency : dependencies ) 253 { 254 Artifact artifact = createDependencyArtifact( dependency ); 255 if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) ) 256 { 257 artifacts.put( artifact.getDependencyConflictId(), artifact ); 258 } 259 } 260 261 for ( Artifact artifact : artifacts.values() ) 262 { 263 try 264 { 265 resolve( artifact, request ); 266 result.addArtifact( artifact ); 267 } 268 catch ( IOException e ) 269 { 270 result.addMissingArtifact( artifact ); 271 } 272 } 273 } 274 275 return result; 276 } 277 278 private void resolve( Artifact artifact, ArtifactResolutionRequest request ) 279 throws IOException 280 { 281 if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) 282 { 283 return; 284 } 285 286 ArtifactRepository localRepo = request.getLocalRepository(); 287 288 File localFile = new File( localRepo.getBasedir(), localRepo.pathOf( artifact ) ); 289 290 artifact.setFile( localFile ); 291 292 if ( !localFile.exists() ) 293 { 294 if ( request.getRemoteRepositories().isEmpty() ) 295 { 296 throw new IOException( localFile + " does not exist and no remote repositories are configured" ); 297 } 298 299 ArtifactRepository remoteRepo = request.getRemoteRepositories().get( 0 ); 300 301 File remoteFile = new File( remoteRepo.getBasedir(), remoteRepo.pathOf( artifact ) ); 302 303 FileUtils.copyFile( remoteFile, localFile ); 304 } 305 306 artifact.setResolved( true ); 307 } 308 309 public void retrieve( ArtifactRepository repository, File destination, String remotePath, 310 ArtifactTransferListener transferListener ) 311 throws ArtifactTransferFailedException, ArtifactDoesNotExistException 312 { 313 // TODO Auto-generated method stub 314 315 } 316 317 public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories ) 318 { 319 } 320 321 public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories ) 322 { 323 } 324 325 public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories ) 326 { 327 } 328 329}