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