View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.installer;
20  
21  import java.io.File;
22  
23  import org.apache.maven.RepositoryUtils;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.metadata.ArtifactMetadata;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
28  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
29  import org.apache.maven.artifact.repository.metadata.MetadataBridge;
30  import org.apache.maven.artifact.repository.metadata.Snapshot;
31  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
32  import org.apache.maven.artifact.repository.metadata.Versioning;
33  import org.apache.maven.plugin.LegacySupport;
34  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
35  import org.codehaus.plexus.component.annotations.Component;
36  import org.codehaus.plexus.component.annotations.Requirement;
37  import org.codehaus.plexus.logging.AbstractLogEnabled;
38  import org.eclipse.aether.RepositorySystem;
39  import org.eclipse.aether.RepositorySystemSession;
40  import org.eclipse.aether.RequestTrace;
41  import org.eclipse.aether.installation.InstallRequest;
42  import org.eclipse.aether.installation.InstallationException;
43  import org.eclipse.aether.util.artifact.SubArtifact;
44  
45  /**
46   * @author Jason van Zyl
47   */
48  @Component(role = ArtifactInstaller.class)
49  public class DefaultArtifactInstaller extends AbstractLogEnabled implements ArtifactInstaller {
50  
51      @Requirement
52      private RepositorySystem repoSystem;
53  
54      @Requirement
55      private LegacySupport legacySupport;
56  
57      /** @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly. */
58      @Deprecated
59      public void install(String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository)
60              throws ArtifactInstallationException {
61          String extension = artifact.getArtifactHandler().getExtension();
62          File source = new File(basedir, finalName + "." + extension);
63  
64          install(source, artifact, localRepository);
65      }
66  
67      public void install(File source, Artifact artifact, ArtifactRepository localRepository)
68              throws ArtifactInstallationException {
69          RepositorySystemSession session =
70                  LegacyLocalRepositoryManager.overlay(localRepository, legacySupport.getRepositorySession(), repoSystem);
71  
72          InstallRequest request = new InstallRequest();
73  
74          request.setTrace(RequestTrace.newChild(null, legacySupport.getSession().getCurrentProject()));
75  
76          org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact(artifact);
77          mainArtifact = mainArtifact.setFile(source);
78          request.addArtifact(mainArtifact);
79  
80          for (ArtifactMetadata metadata : artifact.getMetadataList()) {
81              if (metadata instanceof ProjectArtifactMetadata) {
82                  org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact(mainArtifact, "", "pom");
83                  pomArtifact = pomArtifact.setFile(((ProjectArtifactMetadata) metadata).getFile());
84                  request.addArtifact(pomArtifact);
85              } else if (metadata instanceof SnapshotArtifactRepositoryMetadata
86                      || metadata instanceof ArtifactRepositoryMetadata) {
87                  // eaten, handled by repo system
88              } else {
89                  request.addMetadata(new MetadataBridge(metadata));
90              }
91          }
92  
93          try {
94              repoSystem.install(session, request);
95          } catch (InstallationException e) {
96              throw new ArtifactInstallationException(e.getMessage(), e);
97          }
98  
99          /*
100          * NOTE: Not used by Maven core, only here to provide backward-compat with plugins like the Install Plugin.
101          */
102 
103         if (artifact.isSnapshot()) {
104             Snapshot snapshot = new Snapshot();
105             snapshot.setLocalCopy(true);
106             artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
107         }
108 
109         Versioning versioning = new Versioning();
110         // TODO Should this be changed for MNG-6754 too?
111         versioning.updateTimestamp();
112         versioning.addVersion(artifact.getBaseVersion());
113         if (artifact.isRelease()) {
114             versioning.setRelease(artifact.getBaseVersion());
115         }
116         artifact.addMetadata(new ArtifactRepositoryMetadata(artifact, versioning));
117     }
118 }