View Javadoc
1   package org.apache.maven.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.api.Artifact;
23  import org.apache.maven.api.RemoteRepository;
24  import org.apache.maven.api.annotations.Nonnull;
25  import javax.inject.Inject;
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import java.util.Collection;
30  
31  import org.apache.maven.api.services.ArtifactDeployer;
32  import org.apache.maven.api.services.ArtifactDeployerException;
33  import org.apache.maven.api.services.ArtifactDeployerRequest;
34  import org.eclipse.aether.RepositorySystem;
35  import org.eclipse.aether.deployment.DeployRequest;
36  import org.eclipse.aether.deployment.DeployResult;
37  import org.eclipse.aether.deployment.DeploymentException;
38  
39  import static org.apache.maven.internal.impl.Utils.cast;
40  import static org.apache.maven.internal.impl.Utils.nonNull;
41  
42  /**
43   * Implementation of {@link ArtifactDeployer} service.
44   */
45  @Named
46  @Singleton
47  public class DefaultArtifactDeployer implements ArtifactDeployer
48  {
49      private final @Nonnull RepositorySystem repositorySystem;
50  
51      @Inject
52      DefaultArtifactDeployer( @Nonnull RepositorySystem repositorySystem )
53      {
54          this.repositorySystem = nonNull( repositorySystem, "repositorySystem can not be null" );
55      }
56  
57      @Override
58      public void deploy( @Nonnull ArtifactDeployerRequest request )
59      {
60          nonNull( request, "request can not be null" );
61          DefaultSession session = cast( DefaultSession.class, request.getSession(),
62                  "request.session should be a " + DefaultSession.class );
63          Collection<Artifact> artifacts = nonNull( request.getArtifacts(), "request.artifacts can not be null" );
64          RemoteRepository repository = nonNull( request.getRepository(), "request.repository can not be null" );
65          try
66          {
67              DeployRequest deployRequest = new DeployRequest()
68                      .setRepository( session.toRepository( repository ) )
69                      .setArtifacts( session.toArtifacts( artifacts ) );
70  
71              DeployResult result = repositorySystem.deploy( session.getSession(), deployRequest );
72          }
73          catch ( DeploymentException e )
74          {
75              throw new ArtifactDeployerException( "Unable to deploy artifacts", e );
76          }
77      }
78  }