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