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