View Javadoc
1   package org.apache.maven.shared.transfer.artifact.install.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.io.File;
23  import java.util.Collection;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
28  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
29  import org.apache.maven.shared.transfer.repository.RepositoryManager;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34  import org.codehaus.plexus.context.Context;
35  import org.codehaus.plexus.context.ContextException;
36  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
37  
38  /**
39   * 
40   */
41  @Component( role = ArtifactInstaller.class )
42  class DefaultArtifactInstaller
43      implements ArtifactInstaller, Contextualizable
44  {
45      private PlexusContainer container;
46      
47      private RepositoryManager repositoryManager;
48  
49      @Override
50      public void install( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
51          throws ArtifactInstallerException, IllegalArgumentException
52      {
53          validateParameters( request, mavenArtifacts );
54          try
55          {
56              getMavenArtifactInstaller( request ).install( mavenArtifacts );
57          }
58          catch ( ComponentLookupException e )
59          {
60              throw new ArtifactInstallerException( e.getMessage(), e );
61          }
62      }
63  
64      @Override
65      public void install( ProjectBuildingRequest request, File localRepositry, Collection<Artifact> mavenArtifacts )
66          throws ArtifactInstallerException
67      {
68          validateParameters( request, mavenArtifacts );
69          if ( localRepositry == null )
70          {
71              throw new IllegalArgumentException( "The parameter localRepository is not allowed to be null." );
72          }
73          if ( !localRepositry.isDirectory() )
74          {
75              throw new IllegalArgumentException( "The parameter localRepository must be a directory." );
76          }
77  
78          // TODO: Should we check for exists() ?
79          
80          // update local repo in request 
81          ProjectBuildingRequest newRequest = repositoryManager.setLocalRepositoryBasedir( request, localRepositry );
82  
83          try
84          {
85              getMavenArtifactInstaller( newRequest ).install( mavenArtifacts );
86          }
87          catch ( ComponentLookupException e )
88          {
89              throw new ArtifactInstallerException( e.getMessage(), e );
90          }
91      }
92  
93      private void validateParameters( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
94      {
95          if ( request == null )
96          {
97              throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
98          }
99          if ( mavenArtifacts == null )
100         {
101             throw new IllegalArgumentException( "The parameter mavenArtifacts is not allowed to be null." );
102         }
103         if ( mavenArtifacts.isEmpty() )
104         {
105             throw new IllegalArgumentException( "The collection mavenArtifacts is not allowed to be empty." );
106         }
107     }
108 
109     /**
110      * @return true if the current Maven version is Maven 3.1.
111      */
112     private boolean isMaven31()
113     {
114         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
115     }
116 
117     private boolean canFindCoreClass( String className )
118     {
119         try
120         {
121             Thread.currentThread().getContextClassLoader().loadClass( className );
122 
123             return true;
124         }
125         catch ( ClassNotFoundException e )
126         {
127             return false;
128         }
129     }
130 
131     /**
132      * Injects the Plexus content.
133      *
134      * @param context Plexus context to inject.
135      * @throws ContextException if the PlexusContainer could not be located.
136      */
137     public void contextualize( Context context )
138         throws ContextException
139     {
140         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
141     }
142     
143     private MavenArtifactInstaller getMavenArtifactInstaller( ProjectBuildingRequest buildingRequest )
144         throws ComponentLookupException, ArtifactInstallerException
145     {
146         if ( isMaven31() )
147         {
148             org.eclipse.aether.RepositorySystem repositorySystem =
149                             container.lookup( org.eclipse.aether.RepositorySystem.class );
150             
151             org.eclipse.aether.RepositorySystemSession session =
152                 (org.eclipse.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
153 
154             return new Maven31ArtifactInstaller( repositorySystem, session );
155         }
156         else
157         {
158             org.sonatype.aether.RepositorySystem repositorySystem =
159                             container.lookup( org.sonatype.aether.RepositorySystem.class );
160             
161             org.sonatype.aether.RepositorySystemSession session =
162                 (org.sonatype.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
163 
164             return new Maven30ArtifactInstaller( repositorySystem, session );
165         }
166     }
167 }