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