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