View Javadoc

1   package org.apache.maven.plugin.dependency.fromDependencies;
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.io.IOException;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.metadata.ArtifactMetadata;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
32  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
33  import org.apache.maven.artifact.repository.metadata.Snapshot;
34  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
36  import org.apache.maven.artifact.transform.SnapshotTransformation;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
39  import org.apache.maven.plugin.dependency.fromDependencies.CopyDependenciesMojo;
40  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
41  import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
42  import org.apache.maven.project.MavenProject;
43  import org.codehaus.plexus.util.StringUtils;
44  
45  public class TestCopyDependenciesMojo2
46      extends AbstractDependencyMojoTestCase
47  {
48  
49      CopyDependenciesMojo mojo;
50  
51      protected void setUp()
52          throws Exception
53      {
54          // required for mojo lookups to work
55          super.setUp( "copy-dependencies", true );
56  
57          File testPom = new File( getBasedir(), "target/test-classes/unit/copy-dependencies-test/plugin-config.xml" );
58          mojo = (CopyDependenciesMojo) lookupMojo( "copy-dependencies", testPom );
59          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
60          // mojo.silent = true;
61  
62          assertNotNull( mojo );
63          assertNotNull( mojo.getProject() );
64          MavenProject project = mojo.getProject();
65  
66          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
67          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
68          artifacts.addAll( directArtifacts );
69  
70          project.setArtifacts( artifacts );
71          project.setDependencyArtifacts( directArtifacts );
72          mojo.markersDirectory = new File( this.testDir, "markers" );
73  
74      }
75  
76      public void assertNoMarkerFile( Artifact artifact )
77      {
78          DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler( artifact, mojo.markersDirectory );
79          try
80          {
81              assertFalse( handle.isMarkerSet() );
82          }
83          catch ( MojoExecutionException e )
84          {
85              fail( e.getLongMessage() );
86          }
87  
88      }
89  
90      public void testCopyDependenciesMojoIncludeCompileScope()
91          throws Exception
92      {
93          mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
94          mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
95          mojo.includeScope = "compile";
96  
97          mojo.execute();
98  
99          ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
100 
101         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
102         for ( Artifact artifact : artifacts )
103         {
104             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
105             File file = new File( mojo.outputDirectory, fileName );
106 
107             assertEquals( saf.include( artifact ), file.exists() );
108         }
109     }
110 
111     public void testCopyDependenciesMojoIncludeTestScope()
112         throws Exception
113     {
114         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
115         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
116         mojo.includeScope = "test";
117 
118         mojo.execute();
119 
120         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
121 
122         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
123         for ( Artifact artifact : artifacts )
124         {
125             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
126             File file = new File( mojo.outputDirectory, fileName );
127 
128             assertEquals( saf.include( artifact ), file.exists() );
129         }
130     }
131 
132     public void testCopyDependenciesMojoIncludeRuntimeScope()
133         throws Exception
134     {
135         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
136         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
137         mojo.includeScope = "runtime";
138 
139         mojo.execute();
140 
141         ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
142 
143         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
144         for ( Artifact artifact : artifacts )
145         {
146             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
147             File file = new File( mojo.outputDirectory, fileName );
148 
149             assertEquals( saf.include( artifact ), file.exists() );
150         }
151     }
152 
153     public void testCopyDependenciesMojoIncludeprovidedScope()
154         throws Exception
155     {
156         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
157         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
158         mojo.includeScope = "provided";
159 
160         mojo.execute();
161 
162         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
163         for ( Artifact artifact : artifacts )
164         {
165             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
166             File file = new File( mojo.outputDirectory, fileName );
167 
168             assertEquals( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ), file.exists() );
169         }
170     }
171 
172     public void testCopyDependenciesMojoIncludesystemScope()
173         throws Exception
174     {
175         mojo.getProject().setArtifacts( stubFactory.getScopedArtifacts() );
176         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
177         mojo.includeScope = "system";
178 
179         mojo.execute();
180 
181         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
182         for ( Artifact artifact : artifacts )
183         {
184             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
185             File file = new File( mojo.outputDirectory, fileName );
186 
187             assertEquals( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ), file.exists() );
188         }
189     }
190 
191     public void testSubPerArtifact()
192         throws Exception
193     {
194         mojo.useSubDirectoryPerArtifact = true;
195 
196         mojo.execute();
197 
198         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
199         for ( Artifact artifact : artifacts )
200         {
201             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
202             File folder = DependencyUtil.getFormattedOutputDirectory( false, false, true, false, false, mojo.outputDirectory,
203                                                                       artifact );
204             File file = new File( folder, fileName );
205             assertTrue( file.exists() );
206         }
207     }
208 
209     public void testSubPerArtifactAndType()
210         throws Exception
211     {
212         mojo.getProject().setArtifacts( stubFactory.getTypedArtifacts() );
213         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
214         mojo.useSubDirectoryPerArtifact = true;
215         mojo.useSubDirectoryPerType = true;
216 
217         mojo.execute();
218 
219         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
220         for ( Artifact artifact : artifacts )
221         {
222             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
223             File folder = DependencyUtil.getFormattedOutputDirectory( false, true, true, false, false, mojo.outputDirectory,
224                                                                       artifact );
225             File file = new File( folder, fileName );
226             assertTrue( file.exists() );
227         }
228     }
229 
230     public void testSubPerArtifactAndScope()
231         throws Exception
232     {
233         mojo.getProject().setArtifacts( stubFactory.getTypedArtifacts() );
234         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
235         mojo.useSubDirectoryPerArtifact = true;
236         mojo.useSubDirectoryPerScope = true;
237 
238         mojo.execute();
239 
240         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
241         for ( Artifact artifact : artifacts )
242         {
243             String fileName = DependencyUtil.getFormattedFileName( artifact, false );
244             File folder = DependencyUtil.getFormattedOutputDirectory( true, false, true, false, false, mojo.outputDirectory,
245                                                                       artifact );
246             File file = new File( folder, fileName );
247             assertTrue( file.exists() );
248         }
249     }
250 
251     public void testRepositoryLayout()
252         throws Exception
253     {
254     	String baseVersion = "2.0-SNAPSHOT";
255 		String groupId = "testGroupId";
256 		String artifactId = "expanded-snapshot";
257 
258 		Artifact expandedSnapshot = createExpandedVersionArtifact( baseVersion,
259 				                                                   groupId, 
260 				                                                   artifactId,
261 				                                                   "compile",
262 				                                                   "jar",
263 				                                                   null);
264 
265         mojo.getProject().getArtifacts().add( expandedSnapshot );
266         mojo.getProject().getDependencyArtifacts().add( expandedSnapshot );
267 
268 		Artifact pomExpandedSnapshot = createExpandedVersionArtifact( baseVersion,
269 													                  groupId, 
270 													                  artifactId,
271 													                  "compile",
272 													                  "pom",
273 													                  null);
274         mojo.getProject().getArtifacts().add( pomExpandedSnapshot );
275         mojo.getProject().getDependencyArtifacts().add( pomExpandedSnapshot );
276 
277         mojo.useRepositoryLayout = true;
278         mojo.execute();
279         
280         File outputDirectory = mojo.outputDirectory;
281 		ArtifactRepository targetRepository = mojo.repositoryFactory.createDeploymentArtifactRepository( 
282         		"local", 
283         		outputDirectory.toURL().toExternalForm(), 
284                 new DefaultRepositoryLayout(),
285                 false );
286 
287         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
288         for ( Artifact artifact : artifacts )
289         {
290 			assertArtifactExists( artifact, targetRepository );
291             
292             if ( ! artifact.getBaseVersion().equals( artifact.getVersion() ) )
293             {
294                 Artifact baseArtifact =
295                     mojo.getFactory().createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
296                                                       artifact.getBaseVersion(), artifact.getScope(),
297                                                       artifact.getType() );
298     			assertArtifactExists( baseArtifact, targetRepository );
299             }
300 
301         }
302     }
303 
304 	private Artifact createExpandedVersionArtifact( String baseVersion,
305 			                                        String groupId, 
306 			                                        String artifactId,
307 			                                        String scope,
308 			                                        String type, 
309 			                                        String classifier ) 
310 			throws IOException 
311 	{
312 		Artifact expandedSnapshot = this.stubFactory.createArtifact( groupId, artifactId, baseVersion, scope, type, classifier );
313 
314     	SnapshotTransformation tr = new SnapshotTransformation();
315         Snapshot snapshot = new Snapshot();
316         snapshot.setTimestamp( tr.getDeploymentTimestamp() );
317         snapshot.setBuildNumber( 1 );
318         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( expandedSnapshot, snapshot );
319         String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
320         expandedSnapshot.setResolvedVersion( StringUtils.replace( baseVersion, Artifact.SNAPSHOT_VERSION, newVersion ) );
321         expandedSnapshot.addMetadata( metadata );
322 		return expandedSnapshot;
323 	}
324 
325 	private void assertArtifactExists( Artifact artifact, ArtifactRepository targetRepository ) {
326 		File file = new File( targetRepository.getBasedir(), 
327 							  targetRepository.getLayout().pathOf( artifact ) );
328 		assertTrue( file.exists() );
329 
330 		Collection<ArtifactMetadata> metas = artifact.getMetadataList();
331 		for ( ArtifactMetadata meta : metas )
332         {
333 			File metaFile = new File( targetRepository.getBasedir(), 
334 									  targetRepository.getLayout().pathOfLocalRepositoryMetadata( meta, targetRepository) );
335 			assertTrue( metaFile.exists() );
336         }
337 	}
338 
339     public void testSubPerArtifactRemoveVersion()
340         throws Exception
341     {
342         mojo.useSubDirectoryPerArtifact = true;
343         mojo.stripVersion = true;
344 
345         mojo.execute();
346 
347         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
348         for ( Artifact artifact : artifacts )
349         {
350             String fileName = DependencyUtil.getFormattedFileName( artifact, true );
351             File folder = DependencyUtil.getFormattedOutputDirectory( false, false, true, false, true, mojo.outputDirectory,
352                                                                       artifact );
353             File file = new File( folder, fileName );
354             assertTrue( file.exists() );
355         }
356     }
357 
358     public void testSubPerArtifactAndTypeRemoveVersion()
359         throws Exception
360     {
361         mojo.getProject().setArtifacts( stubFactory.getTypedArtifacts() );
362         mojo.getProject().setDependencyArtifacts( new HashSet<Artifact>() );
363         mojo.useSubDirectoryPerArtifact = true;
364         mojo.useSubDirectoryPerType = true;
365         mojo.stripVersion = true;
366 
367         mojo.execute();
368 
369         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
370         for ( Artifact artifact : artifacts )
371         {
372             String fileName = DependencyUtil.getFormattedFileName( artifact, true );
373             File folder = DependencyUtil.getFormattedOutputDirectory( false, true, true, false, true, mojo.outputDirectory,
374                                                                       artifact );
375             File file = new File( folder, fileName );
376             assertTrue( file.exists() );
377         }
378     }
379 
380 }