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