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