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