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