View Javadoc
1   package org.apache.maven.artifact.installer;
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  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
29  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
30  import org.apache.maven.artifact.repository.metadata.MetadataBridge;
31  import org.apache.maven.artifact.repository.metadata.Snapshot;
32  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
33  import org.apache.maven.artifact.repository.metadata.Versioning;
34  import org.apache.maven.plugin.LegacySupport;
35  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
36  import org.codehaus.plexus.component.annotations.Component;
37  import org.codehaus.plexus.component.annotations.Requirement;
38  import org.codehaus.plexus.logging.AbstractLogEnabled;
39  import org.eclipse.aether.RepositorySystem;
40  import org.eclipse.aether.RepositorySystemSession;
41  import org.eclipse.aether.RequestTrace;
42  import org.eclipse.aether.installation.InstallRequest;
43  import org.eclipse.aether.installation.InstallationException;
44  import org.eclipse.aether.util.artifact.SubArtifact;
45  
46  /**
47   * @author Jason van Zyl
48   */
49  @Component( role = ArtifactInstaller.class )
50  public class DefaultArtifactInstaller
51      extends AbstractLogEnabled
52      implements ArtifactInstaller
53  {
54  
55      @Requirement
56      private RepositorySystem repoSystem;
57  
58      @Requirement
59      private LegacySupport legacySupport;
60  
61      /** @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly. */
62      @Deprecated
63      public void install( String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository )
64          throws ArtifactInstallationException
65      {
66          String extension = artifact.getArtifactHandler().getExtension();
67          File source = new File( basedir, finalName + "." + extension );
68  
69          install( source, artifact, localRepository );
70      }
71  
72      public void install( File source, Artifact artifact, ArtifactRepository localRepository )
73          throws ArtifactInstallationException
74      {
75          RepositorySystemSession session =
76              LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
77  
78          InstallRequest request = new InstallRequest();
79  
80          request.setTrace( RequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
81  
82          org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
83          mainArtifact = mainArtifact.setFile( source );
84          request.addArtifact( mainArtifact );
85  
86          for ( ArtifactMetadata metadata : artifact.getMetadataList() )
87          {
88              if ( metadata instanceof ProjectArtifactMetadata )
89              {
90                  org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
91                  pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
92                  request.addArtifact( pomArtifact );
93              }
94              else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
95                  || metadata instanceof ArtifactRepositoryMetadata )
96              {
97                  // eaten, handled by repo system
98              }
99              else
100             {
101                 request.addMetadata( new MetadataBridge( metadata ) );
102             }
103         }
104 
105         try
106         {
107             repoSystem.install( session, request );
108         }
109         catch ( InstallationException e )
110         {
111             throw new ArtifactInstallationException( e.getMessage(), e );
112         }
113 
114         /*
115          * NOTE: Not used by Maven core, only here to provide backward-compat with plugins like the Install Plugin.
116          */
117 
118         if ( artifact.isSnapshot() )
119         {
120             Snapshot snapshot = new Snapshot();
121             snapshot.setLocalCopy( true );
122             artifact.addMetadata( new SnapshotArtifactRepositoryMetadata( artifact, snapshot ) );
123         }
124 
125         Versioning versioning = new Versioning();
126         // TODO Should this be changed for MNG-6754 too?
127         versioning.updateTimestamp();
128         versioning.addVersion( artifact.getBaseVersion() );
129         if ( artifact.isRelease() )
130         {
131             versioning.setRelease( artifact.getBaseVersion() );
132         }
133         artifact.addMetadata( new ArtifactRepositoryMetadata( artifact, versioning ) );
134     }
135 
136 }