1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.fromDependencies;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.plugin.LegacySupport;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
34 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
35 import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
36 import org.apache.maven.project.MavenProject;
37
38 public class TestCopyDependenciesMojo extends AbstractDependencyMojoTestCase {
39
40 CopyDependenciesMojo mojo;
41
42 @Override
43 protected void setUp() throws Exception {
44
45 super.setUp("copy-dependencies", true, false);
46
47 File testPom = new File(getBasedir(), "target/test-classes/unit/copy-dependencies-test/plugin-config.xml");
48 mojo = (CopyDependenciesMojo) lookupMojo("copy-dependencies", testPom);
49 mojo.outputDirectory = new File(this.testDir, "outputDirectory");
50
51
52 assertNotNull(mojo);
53 assertNotNull(mojo.getProject());
54 MavenProject project = mojo.getProject();
55
56 MavenSession session = newMavenSession(project);
57 setVariableValueToObject(mojo, "session", session);
58
59 LegacySupport legacySupport = lookup(LegacySupport.class);
60 legacySupport.setSession(session);
61 installLocalRepository(legacySupport);
62
63 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
64 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
65 artifacts.addAll(directArtifacts);
66
67 project.setArtifacts(artifacts);
68 project.setDependencyArtifacts(directArtifacts);
69 mojo.markersDirectory = new File(this.testDir, "markers");
70
71 ArtifactHandlerManager manager = lookup(ArtifactHandlerManager.class);
72 setVariableValueToObject(mojo, "artifactHandlerManager", manager);
73 }
74
75 public void assertNoMarkerFile(Artifact artifact) throws MojoExecutionException {
76 DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler(artifact, mojo.markersDirectory);
77 assertFalse(handle.isMarkerSet());
78 }
79
80 public void testCopyFile() throws MojoExecutionException, IOException {
81 File src = File.createTempFile("copy", null);
82
83 File dest = new File(mojo.outputDirectory, "toMe.jar");
84
85 assertFalse(dest.exists());
86
87 copyFile(mojo, src, dest);
88 assertTrue(dest.exists());
89 }
90
91
92
93
94
95
96 public void testMojo() throws Exception {
97 mojo.execute();
98 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
99 for (Artifact artifact : artifacts) {
100 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
101 File file = new File(mojo.outputDirectory, fileName);
102 assertTrue(file.exists());
103
104
105 assertNoMarkerFile(artifact);
106 }
107 }
108
109 public void testStripVersion() throws Exception {
110 mojo.stripVersion = true;
111 mojo.execute();
112
113 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
114 for (Artifact artifact : artifacts) {
115 String fileName = DependencyUtil.getFormattedFileName(artifact, true);
116 File file = new File(mojo.outputDirectory, fileName);
117 assertTrue(file.exists());
118 }
119 }
120
121 public void testStripClassifier() throws Exception {
122 mojo.stripClassifier = true;
123 mojo.execute();
124
125 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
126 for (Artifact artifact : artifacts) {
127 String fileName = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
128 File file = new File(mojo.outputDirectory, fileName);
129 assertTrue(file.exists());
130 }
131 }
132
133 public void testUseBaseVersion() throws Exception {
134 mojo.useBaseVersion = true;
135 mojo.execute();
136
137 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
138 for (Artifact artifact : artifacts) {
139 String fileName = DependencyUtil.getFormattedFileName(artifact, false, false, true);
140 File file = new File(mojo.outputDirectory, fileName);
141 assertTrue(file.exists());
142 }
143 }
144
145 public void testNoTransitive() throws Exception {
146 mojo.excludeTransitive = true;
147 mojo.execute();
148
149 Set<Artifact> artifacts = mojo.getProject().getDependencyArtifacts();
150 for (Artifact artifact : artifacts) {
151 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
152 File file = new File(mojo.outputDirectory, fileName);
153 assertTrue(file.exists());
154 }
155 }
156
157 public void testExcludeType() throws Exception {
158 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
159 mojo.getProject().setDependencyArtifacts(new HashSet<>());
160 mojo.excludeTypes = "jar";
161 mojo.execute();
162
163 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
164 for (Artifact artifact : artifacts) {
165 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
166 File file = new File(mojo.outputDirectory, fileName);
167 assertEquals(artifact.getType().equalsIgnoreCase("jar"), !file.exists());
168 }
169 }
170
171 public void testIncludeType() throws Exception {
172 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
173 mojo.getProject().setDependencyArtifacts(new HashSet<>());
174
175 mojo.includeTypes = "jar";
176 mojo.excludeTypes = "jar";
177
178
179 mojo.execute();
180
181 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
182 for (Artifact artifact : artifacts) {
183 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
184 File file = new File(mojo.outputDirectory, fileName);
185 assertFalse(file.exists());
186 }
187
188 mojo.excludeTypes = "";
189 mojo.execute();
190
191 artifacts = mojo.getProject().getArtifacts();
192 for (Artifact artifact : artifacts) {
193 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
194 File file = new File(mojo.outputDirectory, fileName);
195 assertEquals(artifact.getType().equalsIgnoreCase("jar"), file.exists());
196 }
197 }
198
199 public void testExcludeArtifactId() throws Exception {
200 mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
201 mojo.getProject().setDependencyArtifacts(new HashSet<>());
202 mojo.excludeArtifactIds = "one";
203 mojo.execute();
204
205 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
206 for (Artifact artifact : artifacts) {
207 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
208 File file = new File(mojo.outputDirectory, fileName);
209 assertEquals(artifact.getArtifactId().equals("one"), !file.exists());
210 }
211 }
212
213 public void testIncludeArtifactId() throws Exception {
214 mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
215 mojo.getProject().setDependencyArtifacts(new HashSet<>());
216
217 mojo.includeArtifactIds = "one";
218 mojo.excludeArtifactIds = "one";
219
220
221 mojo.execute();
222
223 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
224 for (Artifact artifact : artifacts) {
225 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
226 File file = new File(mojo.outputDirectory, fileName);
227 assertFalse(file.exists());
228 }
229
230 mojo.excludeArtifactIds = "";
231 mojo.execute();
232
233 artifacts = mojo.getProject().getArtifacts();
234 for (Artifact artifact : artifacts) {
235 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
236 File file = new File(mojo.outputDirectory, fileName);
237 assertEquals(artifact.getArtifactId().equals("one"), file.exists());
238 }
239 }
240
241 public void testIncludeGroupId() throws Exception {
242 mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
243 mojo.getProject().setDependencyArtifacts(new HashSet<>());
244 mojo.includeGroupIds = "one";
245 mojo.excludeGroupIds = "one";
246
247
248 mojo.execute();
249
250 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
251 for (Artifact artifact : artifacts) {
252 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
253 File file = new File(mojo.outputDirectory, fileName);
254 assertFalse(file.exists());
255 }
256
257 mojo.excludeGroupIds = "";
258 mojo.execute();
259
260 artifacts = mojo.getProject().getArtifacts();
261 for (Artifact artifact : artifacts) {
262 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
263 File file = new File(mojo.outputDirectory, fileName);
264 assertEquals(artifact.getGroupId().equals("one"), file.exists());
265 }
266 }
267
268 public void testExcludeGroupId() throws Exception {
269 mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
270 mojo.getProject().setDependencyArtifacts(new HashSet<>());
271 mojo.excludeGroupIds = "one";
272 mojo.execute();
273
274 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
275 for (Artifact artifact : artifacts) {
276 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
277 File file = new File(mojo.outputDirectory, fileName);
278
279 assertEquals(artifact.getGroupId().equals("one"), !file.exists());
280 }
281 }
282
283 public void testExcludeMultipleGroupIds() throws Exception {
284 mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
285 mojo.getProject().setDependencyArtifacts(new HashSet<>());
286 mojo.excludeGroupIds = "one,two";
287 mojo.execute();
288
289 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
290 for (Artifact artifact : artifacts) {
291 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
292 File file = new File(mojo.outputDirectory, fileName);
293
294 assertEquals(
295 artifact.getGroupId().equals("one") || artifact.getGroupId().equals("two"), !file.exists());
296 }
297 }
298
299 public void testExcludeClassifier() throws Exception {
300 mojo.getProject().setArtifacts(stubFactory.getClassifiedArtifacts());
301 mojo.getProject().setDependencyArtifacts(new HashSet<>());
302 mojo.excludeClassifiers = "one";
303 mojo.execute();
304
305 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
306 for (Artifact artifact : artifacts) {
307 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
308 File file = new File(mojo.outputDirectory, fileName);
309 assertEquals(artifact.getClassifier().equals("one"), !file.exists());
310 }
311 }
312
313 public void testIncludeClassifier() throws Exception {
314 mojo.getProject().setArtifacts(stubFactory.getClassifiedArtifacts());
315 mojo.getProject().setDependencyArtifacts(new HashSet<>());
316
317 mojo.includeClassifiers = "one";
318 mojo.excludeClassifiers = "one";
319
320
321 mojo.execute();
322
323 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
324 for (Artifact artifact : artifacts) {
325 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
326 File file = new File(mojo.outputDirectory, fileName);
327 assertFalse(file.exists());
328 }
329
330 mojo.excludeClassifiers = "";
331 mojo.execute();
332
333 artifacts = mojo.getProject().getArtifacts();
334 for (Artifact artifact : artifacts) {
335 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
336 File file = new File(mojo.outputDirectory, fileName);
337 assertEquals(artifact.getClassifier().equals("one"), file.exists());
338 }
339 }
340
341 public void testSubPerType() throws Exception {
342 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
343 mojo.getProject().setDependencyArtifacts(new HashSet<>());
344 mojo.useSubDirectoryPerType = true;
345 mojo.execute();
346
347 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
348 for (Artifact artifact : artifacts) {
349 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
350 File folder = DependencyUtil.getFormattedOutputDirectory(
351 false, true, false, false, false, false, mojo.outputDirectory, artifact);
352 File file = new File(folder, fileName);
353 assertTrue(file.exists());
354 }
355 }
356
357 public void testCDMClassifier() throws Exception {
358 dotestClassifierType("jdk14", null);
359 }
360
361 public void testCDMType() throws Exception {
362 dotestClassifierType(null, "sources");
363 }
364
365 public void testCDMClassifierType() throws Exception {
366 dotestClassifierType("jdk14", "sources");
367 }
368
369 public void dotestClassifierType(String testClassifier, String testType) throws Exception {
370 mojo.classifier = testClassifier;
371 mojo.type = testType;
372
373 for (Artifact artifact : mojo.getProject().getArtifacts()) {
374 String type = testType != null ? testType : artifact.getType();
375
376 stubFactory.createArtifact(
377 artifact.getGroupId(),
378 artifact.getArtifactId(),
379 artifact.getVersion(),
380 artifact.getScope(),
381 type,
382 testClassifier);
383 }
384
385 mojo.execute();
386
387 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
388 for (Artifact artifact : artifacts) {
389 String useClassifier = artifact.getClassifier();
390 String useType = artifact.getType();
391
392 if (testClassifier != null && !testClassifier.isEmpty()) {
393 useClassifier = "-" + testClassifier;
394
395 if (testType != null && !testType.isEmpty()) {
396 useType = testType;
397 }
398 }
399 String fileName = artifact.getArtifactId() + "-" + artifact.getVersion() + useClassifier + "." + useType;
400 File file = new File(mojo.outputDirectory, fileName);
401
402 if (!file.exists()) {
403 fail("Can't find:" + file.getAbsolutePath());
404 }
405
406
407 assertNoMarkerFile(artifact);
408 }
409 }
410
411 public void testArtifactResolutionException() throws MojoFailureException {
412 dotestArtifactExceptions();
413 }
414
415 public void dotestArtifactExceptions() throws MojoFailureException {
416 mojo.classifier = "jdk";
417 mojo.type = "java-sources";
418
419 try {
420 mojo.execute();
421 fail("ExpectedException");
422 } catch (MojoExecutionException e) {
423
424 }
425 }
426
427
428
429
430
431
432
433 public void testDontOverWriteRelease()
434 throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
435
436 Set<Artifact> artifacts = new HashSet<>();
437 Artifact release = stubFactory.getReleaseArtifact();
438 assertTrue(release.getFile().setLastModified(System.currentTimeMillis() - 2000));
439
440 artifacts.add(release);
441
442 mojo.getProject().setArtifacts(artifacts);
443 mojo.getProject().setDependencyArtifacts(artifacts);
444
445 mojo.overWriteIfNewer = false;
446
447 mojo.execute();
448
449 File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(release, false));
450
451 Thread.sleep(100);
452
453 long time = System.currentTimeMillis() + 1000;
454 time = time - (time % 1000);
455 assertTrue(copiedFile.setLastModified(time));
456 Thread.sleep(100);
457
458 mojo.execute();
459
460 assertEquals(time, copiedFile.lastModified());
461 }
462
463 public void testOverWriteRelease() throws MojoExecutionException, IOException, MojoFailureException {
464
465 Set<Artifact> artifacts = new HashSet<>();
466 Artifact release = stubFactory.getReleaseArtifact();
467
468 assertTrue(release.getFile().setLastModified(1000L));
469 assertEquals(1000L, release.getFile().lastModified());
470
471 artifacts.add(release);
472
473 mojo.getProject().setArtifacts(artifacts);
474 mojo.getProject().setDependencyArtifacts(artifacts);
475
476 mojo.overWriteReleases = true;
477 mojo.overWriteIfNewer = false;
478
479 mojo.execute();
480
481 File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(release, false));
482
483 assertTrue(copiedFile.setLastModified(2000L));
484 assertEquals(2000L, copiedFile.lastModified());
485
486 mojo.execute();
487
488 long timeCopyNow = copiedFile.lastModified();
489 assertEquals(1000L, timeCopyNow);
490 }
491
492 public void testDontOverWriteSnap() throws MojoExecutionException, IOException, MojoFailureException {
493
494 Set<Artifact> artifacts = new HashSet<>();
495 Artifact snap = stubFactory.getSnapshotArtifact();
496 assertTrue(snap.getFile().setLastModified(1000L));
497 assertEquals(1000L, snap.getFile().lastModified());
498
499 artifacts.add(snap);
500
501 mojo.getProject().setArtifacts(artifacts);
502 mojo.getProject().setDependencyArtifacts(artifacts);
503
504 mojo.overWriteReleases = false;
505 mojo.overWriteSnapshots = false;
506 mojo.overWriteIfNewer = false;
507
508 mojo.execute();
509
510 File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(snap, false));
511
512 assertTrue(copiedFile.setLastModified(2000L));
513 assertEquals(2000L, copiedFile.lastModified());
514
515 mojo.execute();
516
517 long timeCopyNow = copiedFile.lastModified();
518 assertEquals(2000L, timeCopyNow);
519 }
520
521 public void testOverWriteSnap() throws MojoExecutionException, IOException, MojoFailureException {
522
523 Set<Artifact> artifacts = new HashSet<>();
524 Artifact snap = stubFactory.getSnapshotArtifact();
525 assertTrue(snap.getFile().setLastModified(1000L));
526 assertEquals(1000L, snap.getFile().lastModified());
527
528 artifacts.add(snap);
529
530 mojo.getProject().setArtifacts(artifacts);
531 mojo.getProject().setDependencyArtifacts(artifacts);
532
533 mojo.overWriteReleases = false;
534 mojo.overWriteSnapshots = true;
535 mojo.overWriteIfNewer = false;
536
537 mojo.execute();
538
539 File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(snap, false));
540
541 assertTrue(copiedFile.setLastModified(2000L));
542 assertEquals(2000L, copiedFile.lastModified());
543
544 mojo.execute();
545
546 long timeCopyNow = copiedFile.lastModified();
547 assertEquals(1000L, timeCopyNow);
548 }
549
550 public void testGetDependencies() throws MojoExecutionException {
551 assertEquals(
552 mojo.getResolvedDependencies(true).toString(),
553 mojo.getDependencySets(true).getResolvedDependencies().toString());
554 }
555
556 public void testExcludeProvidedScope() throws Exception {
557 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
558 mojo.getProject().setDependencyArtifacts(new HashSet<>());
559 mojo.excludeScope = "provided";
560
561
562 mojo.execute();
563
564 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
565 for (Artifact artifact : artifacts) {
566 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
567 File file = new File(mojo.outputDirectory, fileName);
568 assertEquals(artifact.getScope().equals("provided"), !file.exists());
569 file.delete();
570 assertFalse(file.exists());
571 }
572 }
573
574 public void testExcludeSystemScope() throws Exception {
575 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
576 mojo.getProject().setDependencyArtifacts(new HashSet<>());
577 mojo.excludeScope = "system";
578
579
580 mojo.execute();
581
582 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
583 for (Artifact artifact : artifacts) {
584 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
585 File file = new File(mojo.outputDirectory, fileName);
586 assertEquals(artifact.getScope().equals("system"), !file.exists());
587 file.delete();
588 assertFalse(file.exists());
589 }
590 }
591
592 public void testExcludeCompileScope() throws Exception {
593 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
594 mojo.getProject().setDependencyArtifacts(new HashSet<>());
595 mojo.excludeScope = "compile";
596 mojo.execute();
597 ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
598
599 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
600 for (Artifact artifact : artifacts) {
601 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
602 File file = new File(mojo.outputDirectory, fileName);
603
604 assertEquals(!saf.include(artifact), file.exists());
605 }
606 }
607
608 public void testExcludeTestScope() throws IOException, MojoFailureException {
609 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
610 mojo.getProject().setDependencyArtifacts(new HashSet<>());
611 mojo.excludeScope = "test";
612
613 try {
614 mojo.execute();
615 fail("expected an exception");
616 } catch (MojoExecutionException e) {
617
618 }
619 }
620
621 public void testExcludeRuntimeScope() throws Exception {
622 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
623 mojo.getProject().setDependencyArtifacts(new HashSet<>());
624 mojo.excludeScope = "runtime";
625 mojo.execute();
626 ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
627
628 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
629 for (Artifact artifact : artifacts) {
630 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
631 File file = new File(mojo.outputDirectory, fileName);
632
633 assertEquals(!saf.include(artifact), file.exists());
634 }
635 }
636
637 public void testCopyPom() throws Exception {
638 mojo.setCopyPom(true);
639
640 Set<Artifact> set = new HashSet<>();
641 set.add(stubFactory.createArtifact("org.apache.maven", "maven-artifact", "2.0.7", Artifact.SCOPE_COMPILE));
642 stubFactory.createArtifact("org.apache.maven", "maven-artifact", "2.0.7", Artifact.SCOPE_COMPILE, "pom", null);
643 mojo.getProject().setArtifacts(set);
644 mojo.execute();
645
646 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
647 for (Artifact artifact : artifacts) {
648 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
649 File file = new File(mojo.outputDirectory, fileName.substring(0, fileName.length() - 4) + ".pom");
650 assertTrue(file + " doesn't exist", file.exists());
651 }
652 }
653
654 public void testPrependGroupId() throws Exception {
655 mojo.prependGroupId = true;
656 mojo.execute();
657
658 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
659 for (Artifact artifact : artifacts) {
660 String fileName = DependencyUtil.getFormattedFileName(artifact, false, true);
661 File file = new File(mojo.outputDirectory, fileName);
662 assertTrue(file.exists());
663 }
664 }
665 }