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