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