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
42      implements ArtifactDeployer, Contextualizable
43  {
44  
45      private PlexusContainer container;
46  
47      @Override
48      public void deploy( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
49          throws ArtifactDeployerException
50      {
51          validateParameters( request, mavenArtifacts );
52  
53          try
54          {
55              getMavenArtifactDeployer( request ).deploy( mavenArtifacts );
56          }
57          catch ( ComponentLookupException e )
58          {
59              throw new ArtifactDeployerException( e.getMessage(), e );
60          }
61      }
62  
63      @Override
64      public void deploy( ProjectBuildingRequest request, ArtifactRepository remoteRepository,
65                          Collection<Artifact> mavenArtifacts )
66          throws ArtifactDeployerException
67      {
68          validateParameters( request, mavenArtifacts );
69          try
70          {
71              getMavenArtifactDeployer( request ).deploy( remoteRepository, mavenArtifacts );
72          }
73          catch ( ComponentLookupException e )
74          {
75              throw new ArtifactDeployerException( e.getMessage(), e );
76          }
77      }
78  
79      private void validateParameters( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
80      {
81          if ( request == null )
82          {
83              throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
84          }
85          if ( mavenArtifacts == null )
86          {
87              throw new IllegalArgumentException( "The parameter mavenArtifacts is not allowed to be null." );
88          }
89          if ( mavenArtifacts.isEmpty() )
90          {
91              throw new IllegalArgumentException( "The collection mavenArtifacts is not allowed to be empty." );
92          }
93      }
94  
95      /**
96       * @return true if the current Maven version is Maven 3.1.
97       */
98      private boolean isMaven31()
99      {
100         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
101     }
102 
103     private boolean canFindCoreClass( String className )
104     {
105         try
106         {
107             Thread.currentThread().getContextClassLoader().loadClass( className );
108 
109             return true;
110         }
111         catch ( ClassNotFoundException e )
112         {
113             return false;
114         }
115     }
116 
117     /**
118      * Injects the Plexus content.
119      *
120      * @param context Plexus context to inject.
121      * @throws ContextException if the PlexusContainer could not be located.
122      */
123     public void contextualize( Context context )
124         throws ContextException
125     {
126         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
127     }
128     
129     private MavenArtifactDeployer getMavenArtifactDeployer( ProjectBuildingRequest buildingRequest )
130         throws ComponentLookupException, ArtifactDeployerException
131     {
132         if ( isMaven31() )
133         {
134             org.eclipse.aether.RepositorySystem repositorySystem =
135                             container.lookup( org.eclipse.aether.RepositorySystem.class );
136             
137             org.eclipse.aether.RepositorySystemSession session =
138                 (org.eclipse.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
139             
140             return new Maven31ArtifactDeployer( repositorySystem, session );
141         }
142         else
143         {
144             org.sonatype.aether.RepositorySystem repositorySystem =
145                             container.lookup( org.sonatype.aether.RepositorySystem.class );
146             
147             org.sonatype.aether.RepositorySystemSession session =
148                 (org.sonatype.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
149             
150             return new Maven30ArtifactDeployer( repositorySystem, session );
151         }
152     }
153 }