View Javadoc
1   package org.apache.maven.shared.artifact.install.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  import java.util.Collection;
24  
25  import org.apache.maven.RepositoryUtils;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
30  import org.apache.maven.shared.artifact.install.ArtifactInstaller;
31  import org.apache.maven.shared.artifact.install.ArtifactInstallerException;
32  import org.apache.maven.shared.repository.RepositoryManager;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.annotations.Requirement;
35  import org.eclipse.aether.RepositorySystem;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.artifact.Artifact;
38  import org.eclipse.aether.installation.InstallRequest;
39  import org.eclipse.aether.installation.InstallationException;
40  import org.eclipse.aether.util.artifact.SubArtifact;
41  
42  /**
43   * 
44   */
45  @Component( role = ArtifactInstaller.class, hint = "maven31" )
46  public class Maven31ArtifactInstaller
47      implements ArtifactInstaller
48  {
49  
50      @Requirement
51      private RepositorySystem repositorySystem;
52  
53      @Requirement
54      private RepositoryManager repositoryManager;
55  
56      @Override
57      public void install( ProjectBuildingRequest buildingRequest,
58                           Collection<org.apache.maven.artifact.Artifact> mavenArtifacts )
59                               throws ArtifactInstallerException
60      {
61          install( buildingRequest, null, mavenArtifacts );
62      }
63      
64      @Override
65      public void install( ProjectBuildingRequest buildingRequest, File localRepository,
66                           Collection<org.apache.maven.artifact.Artifact> mavenArtifacts )
67                               throws ArtifactInstallerException
68      {
69          // prepare installRequest
70          InstallRequest request = new InstallRequest();
71  
72          // transform artifacts
73          for ( org.apache.maven.artifact.Artifact mavenArtifact : mavenArtifacts )
74          {
75              Artifact mainArtifact =
76                  (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
77                                             org.apache.maven.artifact.Artifact.class, mavenArtifact );
78              request.addArtifact( mainArtifact );
79  
80              for ( ArtifactMetadata metadata : mavenArtifact.getMetadataList() )
81              {
82                  if ( metadata instanceof ProjectArtifactMetadata )
83                  {
84                      Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
85                      pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
86                      request.addArtifact( pomArtifact );
87                  }
88                  else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
89                  metadata instanceof ArtifactRepositoryMetadata )
90                  {
91                      // eaten, handled by repo system
92                  }
93                  else
94                  {
95                      // request.addMetadata( new MetadataBridge( metadata ) );
96                  }
97              }
98          }
99          
100         if ( localRepository != null )
101         {
102             buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepository );
103         }
104 
105         RepositorySystemSession session =
106             (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
107 
108         // install
109         try
110         {
111             repositorySystem.install( session, request );
112         }
113         catch ( InstallationException e )
114         {
115             throw new ArtifactInstallerException( e.getMessage(), e );
116         }
117     }
118 }