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