View Javadoc
1   package org.apache.maven.shared.transfer.artifact.deploy.internal;
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 java.util.Collection;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
28  import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
29  import org.codehaus.plexus.PlexusConstants;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.context.Context;
34  import org.codehaus.plexus.context.ContextException;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36  
37  /**
38   *
39   */
40  @Component( role = ArtifactDeployer.class )
41  class DefaultArtifactDeployer implements ArtifactDeployer, Contextualizable
42  {
43  
44      private PlexusContainer container;
45  
46      @Override
47      public void deploy( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
48              throws ArtifactDeployerException
49      {
50          validateParameters( request, mavenArtifacts );
51  
52          try
53          {
54              getMavenArtifactDeployer( request ).deploy( mavenArtifacts );
55          }
56          catch ( ComponentLookupException e )
57          {
58              throw new ArtifactDeployerException( e.getMessage(), e );
59          }
60      }
61  
62      @Override
63      public void deploy( ProjectBuildingRequest request, ArtifactRepository remoteRepository,
64              Collection<Artifact> mavenArtifacts ) throws ArtifactDeployerException
65      {
66          validateParameters( request, mavenArtifacts );
67          try
68          {
69              getMavenArtifactDeployer( request ).deploy( remoteRepository, mavenArtifacts );
70          }
71          catch ( ComponentLookupException e )
72          {
73              throw new ArtifactDeployerException( e.getMessage(), e );
74          }
75      }
76  
77      private void validateParameters( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
78      {
79          if ( request == null )
80          {
81              throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
82          }
83          if ( mavenArtifacts == null )
84          {
85              throw new IllegalArgumentException( "The parameter mavenArtifacts is not allowed to be null." );
86          }
87          if ( mavenArtifacts.isEmpty() )
88          {
89              throw new IllegalArgumentException( "The collection mavenArtifacts is not allowed to be empty." );
90          }
91      }
92  
93      /**
94       * @return true if the current Maven version is Maven 3.1.
95       */
96      private boolean isMaven31()
97      {
98          try
99          {
100             // Maven 3.1 specific
101             Thread.currentThread().getContextClassLoader().loadClass( "org.eclipse.aether.artifact.Artifact" );
102             return true;
103         }
104         catch ( ClassNotFoundException e )
105         {
106             return false;
107         }
108     }
109 
110     /**
111      * Injects the Plexus content.
112      *
113      * @param context Plexus context to inject.
114      * @throws ContextException if the PlexusContainer could not be located.
115      */
116     public void contextualize( Context context ) throws ContextException
117     {
118         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
119     }
120 
121     private MavenArtifactDeployer getMavenArtifactDeployer( ProjectBuildingRequest buildingRequest )
122             throws ComponentLookupException, ArtifactDeployerException
123     {
124         if ( isMaven31() )
125         {
126             org.eclipse.aether.RepositorySystem repositorySystem = container.lookup(
127                     org.eclipse.aether.RepositorySystem.class );
128 
129             org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
130                     "getRepositorySession" );
131 
132             return new Maven31ArtifactDeployer( repositorySystem, session );
133         }
134         else
135         {
136             org.sonatype.aether.RepositorySystem repositorySystem = container.lookup(
137                     org.sonatype.aether.RepositorySystem.class );
138 
139             org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
140                     "getRepositorySession" );
141 
142             return new Maven30ArtifactDeployer( repositorySystem, session );
143         }
144     }
145 }