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