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