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