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