View Javadoc

1   package org.apache.maven.plugin.dependency;
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.net.MalformedURLException;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.installer.ArtifactInstallationException;
29  import org.apache.maven.artifact.installer.ArtifactInstaller;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
35  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
36  import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter;
37  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
38  
39  /**
40   * Goal that copies the project dependencies from the repository to a defined
41   * location.
42   *
43   * @goal copy-dependencies
44   * @requiresDependencyResolution test
45   * @phase process-sources
46   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
47   * @version $Id: CopyDependenciesMojo.java 1081021 2011-03-13 00:17:39Z hboutemy $
48   * @since 1.0
49   */
50  public class CopyDependenciesMojo
51      extends AbstractFromDependenciesMojo
52  {
53  
54      /**
55       * @component
56       */
57      protected ArtifactInstaller installer;
58  
59      /**
60       * @component
61       */
62      protected ArtifactRepositoryFactory repositoryFactory;
63  
64      /**
65       * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
66       */
67      private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
68  
69      /**
70       * Main entry into mojo. Gets the list of dependencies and iterates through
71       * calling copyArtifact.
72       *
73       * @throws MojoExecutionException
74       *             with a message if an error occurs.
75       *
76       * @see #getDependencies
77       * @see #copyArtifact(Artifact, boolean)
78       */
79      public void execute()
80          throws MojoExecutionException
81      {
82          DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
83          Set<Artifact> artifacts = dss.getResolvedDependencies();
84  
85          if ( !useRepositoryLayout )
86          {
87              for ( Artifact artifact : artifacts )
88              {
89  	    		copyArtifact( artifact, this.stripVersion, this.prependGroupId );
90              }
91          }
92          else
93          {
94              try
95              {
96                  ArtifactRepository targetRepository = repositoryFactory.createDeploymentArtifactRepository(
97                          "local",
98                          outputDirectory.toURL().toExternalForm(),
99                          (ArtifactRepositoryLayout) repositoryLayouts.get( "default" ),
100                         false /*uniqueVersion*/ );
101                 for ( Artifact artifact : artifacts )
102                 {
103                     installArtifact( artifact, targetRepository );
104                 }
105             }
106             catch ( MalformedURLException e )
107             {
108                 throw new MojoExecutionException( "Could not create outputDirectory repository", e );
109             }
110         }
111 
112         Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
113         for ( Artifact artifact : skippedArtifacts )
114         {
115             getLog().info( artifact.getFile().getName() + " already exists in destination." );
116         }
117 
118         if ( isCopyPom() )
119         {
120             copyPoms( getOutputDirectory(), artifacts, this.stripVersion );
121             copyPoms( getOutputDirectory(), skippedArtifacts, this.stripVersion );  // Artifacts that already exist may not already have poms.
122         }
123     }
124 
125     private void installArtifact( Artifact artifact, ArtifactRepository targetRepository )
126     {
127         try
128         {
129             if ( "pom".equals( artifact.getType() ) )
130             {
131                 installer.install( artifact.getFile(), artifact, targetRepository );
132                 installBaseSnapshot( artifact, targetRepository );
133             }
134             else
135             {
136                 installer.install( artifact.getFile(), artifact, targetRepository );
137                 installBaseSnapshot( artifact, targetRepository );
138 
139                 if ( isCopyPom() )
140                 {
141                     Artifact pomArtifact = getResolvedPomArtifact( artifact );
142                     if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
143                     {
144                         installer.install( pomArtifact.getFile(), pomArtifact, targetRepository );
145                         installBaseSnapshot( pomArtifact, targetRepository );
146                     }
147                 }
148             }
149         }
150         catch ( ArtifactInstallationException e )
151         {
152             getLog().info( e.getMessage() );
153         }
154     }
155 
156     private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository )
157             throws ArtifactInstallationException
158     {
159         if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) )
160         {
161             Artifact baseArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
162                     artifact.getBaseVersion(), artifact.getScope(), artifact.getType() );
163             installer.install( artifact.getFile(), baseArtifact, targetRepository );
164         }
165     }
166 
167     /**
168      * Copies the Artifact after building the destination file name if
169      * overridden. This method also checks if the classifier is set and adds it
170      * to the destination file name if needed.
171      *
172      * @param artifact
173      *            representing the object to be copied.
174      * @param removeVersion
175      *            specifies if the version should be removed from the file name
176      *            when copying.
177      * @param prependGroupId
178      *            specifies if the groupId should be prepend to the file while copying.
179      * @throws MojoExecutionException
180      *             with a message if an error occurs.
181      *
182      * @see DependencyUtil#copyFile(File, File, Log)
183      * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
184      */
185     protected void copyArtifact( Artifact artifact, boolean removeVersion, boolean prependGroupId )
186         throws MojoExecutionException
187     {
188 
189         String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion, prependGroupId);
190 
191         File destDir;
192         destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType, useSubDirectoryPerArtifact,
193                                                               useRepositoryLayout, stripVersion, outputDirectory,
194                                                               artifact );
195         File destFile = new File( destDir, destFileName );
196 
197         copyFile( artifact.getFile(), destFile );
198     }
199 
200     /**
201      * Copy the pom files associated with the artifacts.
202      */
203     public void copyPoms( File destDir, Set<Artifact> artifacts, boolean removeVersion )
204         throws MojoExecutionException
205 
206     {
207         for ( Artifact artifact : artifacts )
208         {
209             Artifact pomArtifact = getResolvedPomArtifact( artifact );
210 
211             // Copy the pom
212             if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
213             {
214                 File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion,
215                                                                                            prependGroupId) );
216                 if ( ! pomDestFile.exists() )
217                 {
218                     copyFile( pomArtifact.getFile(), pomDestFile );
219                 }
220             }
221         }
222     }
223 
224     protected Artifact getResolvedPomArtifact( Artifact artifact )
225     {
226         Artifact pomArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
227                                                             artifact.getVersion(), "", "pom" );
228         // Resolve the pom artifact using repos
229         try
230         {
231             this.resolver.resolve( pomArtifact, this.remoteRepos, this.getLocal() );
232         }
233         catch ( Exception e )
234         {
235             getLog().info( e.getMessage() );
236         }
237         return pomArtifact;
238     }
239 
240     protected ArtifactsFilter getMarkedArtifactFilter()
241     {
242         return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
243                                    this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType, this.useSubDirectoryPerScope,
244                                    this.useRepositoryLayout, this.stripVersion, this.outputDirectory );
245     }
246 }