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 = 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 transferMetadata = 
84                                      (org.apache.maven.shared.transfer.metadata.ArtifactMetadata) metadata;
85                      
86                      request.addMetadata( new Maven31MetadataBridge( metadata ).setFile( transferMetadata.getFile() ) );
87                  }
88              }
89          }
90          
91          // install
92          try
93          {
94              repositorySystem.install( session, request );
95          }
96          catch ( InstallationException e )
97          {
98              throw new ArtifactInstallerException( e.getMessage(), e );
99          }
100     }
101 }