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.deployer;
20  
21  import java.io.File;
22  import java.util.Map;
23  import java.util.concurrent.ConcurrentHashMap;
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.DefaultArtifactRepository;
29  import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
30  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
31  import org.apache.maven.artifact.repository.metadata.MetadataBridge;
32  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
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.deployment.DeployRequest;
42  import org.eclipse.aether.deployment.DeployResult;
43  import org.eclipse.aether.deployment.DeploymentException;
44  import org.eclipse.aether.metadata.MergeableMetadata;
45  import org.eclipse.aether.repository.RemoteRepository;
46  import org.eclipse.aether.util.artifact.SubArtifact;
47  
48  /**
49   * DefaultArtifactDeployer
50   */
51  @Component(role = ArtifactDeployer.class, instantiationStrategy = "per-lookup")
52  public class DefaultArtifactDeployer extends AbstractLogEnabled implements ArtifactDeployer {
53  
54      @Requirement
55      private RepositorySystem repoSystem;
56  
57      @Requirement
58      private LegacySupport legacySupport;
59  
60      private Map<Object, MergeableMetadata> relatedMetadata = new ConcurrentHashMap<>();
61  
62      /**
63       * @deprecated we want to use the artifact method only, and ensure artifact.file is set
64       *             correctly.
65       */
66      @Deprecated
67      public void deploy(
68              String basedir,
69              String finalName,
70              Artifact artifact,
71              ArtifactRepository deploymentRepository,
72              ArtifactRepository localRepository)
73              throws ArtifactDeploymentException {
74          String extension = artifact.getArtifactHandler().getExtension();
75          File source = new File(basedir, finalName + "." + extension);
76          deploy(source, artifact, deploymentRepository, localRepository);
77      }
78  
79      public void deploy(
80              File source, Artifact artifact, ArtifactRepository deploymentRepository, ArtifactRepository localRepository)
81              throws ArtifactDeploymentException {
82          RepositorySystemSession session =
83                  LegacyLocalRepositoryManager.overlay(localRepository, legacySupport.getRepositorySession(), repoSystem);
84  
85          DeployRequest request = new DeployRequest();
86  
87          request.setTrace(RequestTrace.newChild(null, legacySupport.getSession().getCurrentProject()));
88  
89          org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact(artifact);
90          mainArtifact = mainArtifact.setFile(source);
91          request.addArtifact(mainArtifact);
92  
93          String versionKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
94          String snapshotKey = null;
95          if (artifact.isSnapshot()) {
96              snapshotKey = versionKey + ':' + artifact.getBaseVersion();
97              request.addMetadata(relatedMetadata.get(snapshotKey));
98          }
99          request.addMetadata(relatedMetadata.get(versionKey));
100 
101         for (ArtifactMetadata metadata : artifact.getMetadataList()) {
102             if (metadata instanceof ProjectArtifactMetadata) {
103                 org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact(mainArtifact, "", "pom");
104                 pomArtifact = pomArtifact.setFile(((ProjectArtifactMetadata) metadata).getFile());
105                 request.addArtifact(pomArtifact);
106             } else if (metadata instanceof SnapshotArtifactRepositoryMetadata
107                     || metadata instanceof ArtifactRepositoryMetadata) {
108                 // eaten, handled by repo system
109             } else {
110                 request.addMetadata(new MetadataBridge(metadata));
111             }
112         }
113 
114         RemoteRepository remoteRepo = RepositoryUtils.toRepo(deploymentRepository);
115         /*
116          * NOTE: This provides backward-compat with maven-deploy-plugin:2.4 which bypasses the repository factory when
117          * using an alternative deployment location.
118          */
119         if (deploymentRepository instanceof DefaultArtifactRepository
120                 && deploymentRepository.getAuthentication() == null) {
121             RemoteRepository.Builder builder = new RemoteRepository.Builder(remoteRepo);
122             builder.setAuthentication(session.getAuthenticationSelector().getAuthentication(remoteRepo));
123             builder.setProxy(session.getProxySelector().getProxy(remoteRepo));
124             remoteRepo = builder.build();
125         }
126         request.setRepository(remoteRepo);
127 
128         DeployResult result;
129         try {
130             result = repoSystem.deploy(session, request);
131         } catch (DeploymentException e) {
132             throw new ArtifactDeploymentException(e.getMessage(), e);
133         }
134 
135         for (Object metadata : result.getMetadata()) {
136             if (metadata.getClass().getName().endsWith(".internal.VersionsMetadata")) {
137                 relatedMetadata.put(versionKey, (MergeableMetadata) metadata);
138             }
139             if (snapshotKey != null && metadata.getClass().getName().endsWith(".internal.RemoteSnapshotMetadata")) {
140                 relatedMetadata.put(snapshotKey, (MergeableMetadata) metadata);
141             }
142         }
143 
144         artifact.setResolvedVersion(result.getArtifacts().iterator().next().getVersion());
145     }
146 }