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