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 = Invoker.invoke( RepositoryUtils.class, "toArtifact",
64                                         org.apache.maven.artifact.Artifact.class, mavenArtifact );
65              request.addArtifact( mainArtifact );
66  
67              for ( ArtifactMetadata metadata : mavenArtifact.getMetadataList() )
68              {
69                  if ( metadata instanceof ProjectArtifactMetadata )
70                  {
71                      Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
72                      pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
73                      request.addArtifact( pomArtifact );
74                  }
75                  else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
76                  metadata instanceof ArtifactRepositoryMetadata )
77                  {
78                      // eaten, handled by repo system
79                  }
80                  else if ( metadata instanceof org.apache.maven.shared.transfer.metadata.ArtifactMetadata )
81                  {
82                      org.apache.maven.shared.transfer.metadata.ArtifactMetadata transferMedata =
83                                      ( org.apache.maven.shared.transfer.metadata.ArtifactMetadata ) metadata;
84                      
85                      request.addMetadata( new Maven30MetadataBridge( metadata ).setFile( transferMedata.getFile() ) );
86                  }
87              }
88          }
89  
90  //        if ( localRepository != null )
91  //        {
92  //            buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepository );
93  //        }
94          
95          // install
96          try
97          {
98              repositorySystem.install( session, request );
99          }
100         catch ( InstallationException e )
101         {
102             throw new ArtifactInstallerException( e.getMessage(), e );
103         }
104     }
105 }