1 package org.apache.maven.archiver;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.artifact.handler.ArtifactHandler;
26 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.execution.ReactorManager;
30 import org.apache.maven.model.Build;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Organization;
33 import org.apache.maven.monitor.event.EventDispatcher;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.settings.Settings;
36 import org.codehaus.plexus.PlexusContainer;
37 import org.codehaus.plexus.archiver.jar.JarArchiver;
38 import org.codehaus.plexus.archiver.jar.ManifestException;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.codehaus.plexus.util.StringUtils;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.net.URI;
45 import java.net.URL;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Comparator;
49 import java.util.Date;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Properties;
54 import java.util.Set;
55 import java.util.TreeSet;
56 import java.util.jar.Attributes;
57 import java.util.jar.JarFile;
58 import java.util.jar.Manifest;
59
60 @SuppressWarnings( "ResultOfMethodCallIgnored" )
61 public class MavenArchiverTest
62 extends TestCase
63 {
64 static class ArtifactComparator
65 implements Comparator<Artifact>
66 {
67 public int compare( Artifact o1, Artifact o2 )
68 {
69 return o1.getArtifactId().compareTo( o2.getArtifactId() );
70 }
71
72 public boolean equals( Object o )
73 {
74 return false;
75 }
76 }
77
78 public void testGetManifestExtensionList()
79 throws Exception
80 {
81 MavenArchiver archiver = new MavenArchiver();
82
83 MavenSession session = getDummySession();
84
85 Model model = new Model();
86 model.setArtifactId( "dummy" );
87
88 MavenProject project = new MavenProject( model );
89
90 Set<Artifact> artifacts = new TreeSet<Artifact>( new ArtifactComparator() );
91 project.setArtifacts( artifacts );
92
93
94 ManifestConfiguration config = new ManifestConfiguration()
95 {
96 public boolean isAddExtensions()
97 {
98 return true;
99 }
100 };
101
102 Manifest manifest;
103
104 manifest = archiver.getManifest( session, project, config );
105
106 assertNotNull( manifest.getMainAttributes() );
107
108 for ( Map.Entry<String, Attributes> entry : manifest.getEntries().entrySet() )
109 {
110 System.out.println( entry.getKey() + " " + entry.getValue().getValue( "Extension-List" ) );
111
112 }
113
114 assertEquals( null, manifest.getMainAttributes().getValue( "Extension-List" ) );
115
116 MockArtifact artifact1 = new MockArtifact();
117 artifact1.setGroupId( "org.apache.dummy" );
118 artifact1.setArtifactId( "dummy1" );
119 artifact1.setVersion( "1.0" );
120 artifact1.setType( "dll" );
121 artifact1.setScope( "compile" );
122
123 artifacts.add( artifact1 );
124
125 manifest = archiver.getManifest( session, project, config );
126
127 assertEquals( null, manifest.getMainAttributes().getValue( "Extension-List" ) );
128
129 MockArtifact artifact2 = new MockArtifact();
130 artifact2.setGroupId( "org.apache.dummy" );
131 artifact2.setArtifactId( "dummy2" );
132 artifact2.setVersion( "1.0" );
133 artifact2.setType( "jar" );
134 artifact2.setScope( "compile" );
135
136 artifacts.add( artifact2 );
137
138 manifest = archiver.getManifest( session, project, config );
139
140 assertEquals( "dummy2", manifest.getMainAttributes().getValue( "Extension-List" ) );
141
142 MockArtifact artifact3 = new MockArtifact();
143 artifact3.setGroupId( "org.apache.dummy" );
144 artifact3.setArtifactId( "dummy3" );
145 artifact3.setVersion( "1.0" );
146 artifact3.setScope( "test" );
147 artifact3.setType( "jar" );
148
149 artifacts.add( artifact3 );
150
151 manifest = archiver.getManifest( session, project, config );
152
153 assertEquals( "dummy2", manifest.getMainAttributes().getValue( "Extension-List" ) );
154
155 MockArtifact artifact4 = new MockArtifact();
156 artifact4.setGroupId( "org.apache.dummy" );
157 artifact4.setArtifactId( "dummy4" );
158 artifact4.setVersion( "1.0" );
159 artifact4.setType( "jar" );
160 artifact4.setScope( "compile" );
161
162 artifacts.add( artifact4 );
163
164 manifest = archiver.getManifest( session, project, config );
165
166 assertEquals( "dummy2 dummy4", manifest.getMainAttributes().getValue( "Extension-List" ) );
167 }
168
169 public void testMultiClassPath()
170 throws Exception
171 {
172 final File tempFile = File.createTempFile( "maven-archiver-test-", ".jar" );
173
174 try
175 {
176 MavenArchiver archiver = new MavenArchiver();
177
178 MavenSession session = getDummySession();
179
180 Model model = new Model();
181 model.setArtifactId( "dummy" );
182
183 MavenProject project = new MavenProject( model )
184 {
185 public List getRuntimeClasspathElements()
186 {
187 return Collections.singletonList( tempFile.getAbsolutePath() );
188 }
189 };
190
191
192 ManifestConfiguration manifestConfig = new ManifestConfiguration()
193 {
194 public boolean isAddClasspath()
195 {
196 return true;
197 }
198 };
199
200 MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration();
201 archiveConfiguration.setManifest( manifestConfig );
202 archiveConfiguration.addManifestEntry( "Class-Path", "help/" );
203
204 Manifest manifest = archiver.getManifest( session, project, archiveConfiguration );
205 String classPath = manifest.getMainAttributes().getValue( "Class-Path" );
206 assertTrue( "User specified Class-Path entry was not added to manifest", classPath.contains( "help/" ) );
207 assertTrue( "Class-Path generated by addClasspath was not added to manifest",
208 classPath.contains( tempFile.getName() ) );
209 }
210 finally
211 {
212
213 tempFile.delete();
214 }
215 }
216
217 public void testRecreation()
218 throws Exception
219 {
220 File jarFile = new File( "target/test/dummy.jar" );
221 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
222
223 MavenArchiver archiver = getMavenArchiver( jarArchiver );
224
225 MavenSession session = getDummySession();
226 MavenProject project = getDummyProject();
227
228 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
229 config.setForced( false );
230
231 FileUtils.deleteDirectory( "target/maven-archiver" );
232 archiver.createArchive( session, project, config );
233 assertTrue( jarFile.exists() );
234 jarFile.setLastModified( System.currentTimeMillis() - 60000L );
235 long time = jarFile.lastModified();
236
237 List files = FileUtils.getFiles( new File( "target/maven-archiver" ), "**/**", null, true );
238 for ( Object file : files )
239 {
240 File f = (File) file;
241 f.setLastModified( time );
242 }
243
244 archiver.createArchive( session, project, config );
245 assertEquals( jarFile.lastModified(), time );
246
247 config.setForced( true );
248 archiver.createArchive( session, project, config );
249 assertTrue( jarFile.lastModified() > time );
250 }
251
252 public void testNotGenerateImplementationVersionForMANIFESTMF()
253 throws Exception
254 {
255 JarFile jar = null;
256 try
257 {
258 File jarFile = new File( "target/test/dummy.jar" );
259 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
260
261 MavenArchiver archiver = getMavenArchiver( jarArchiver );
262
263 MavenSession session = getDummySession();
264 MavenProject project = getDummyProject();
265
266 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
267 config.setForced( true );
268 config.getManifest().setAddDefaultImplementationEntries( false );
269 archiver.createArchive( session, project, config );
270 assertTrue( jarFile.exists() );
271
272 jar = new JarFile( jarFile );
273 Map entries = jar.getManifest().getMainAttributes();
274 assertFalse( entries.containsKey( Attributes.Name.IMPLEMENTATION_VERSION ) );
275 }
276 finally
277 {
278
279 if ( jar != null )
280 {
281 jar.close();
282 }
283 }
284 }
285
286 public void testGenerateImplementationVersionForMANIFESTMF()
287 throws Exception
288 {
289 JarFile jar = null;
290 try
291 {
292 File jarFile = new File( "target/test/dummy.jar" );
293 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
294
295 MavenArchiver archiver = getMavenArchiver( jarArchiver );
296
297 MavenSession session = getDummySession();
298 MavenProject project = getDummyProject();
299
300 String ls = System.getProperty( "line.separator" );
301 project.setDescription( "foo " + ls + " bar " );
302 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
303 config.setForced( true );
304 config.getManifest().setAddDefaultImplementationEntries( true );
305 config.addManifestEntry( "Description", project.getDescription() );
306 archiver.createArchive( session, project, config );
307 assertTrue( jarFile.exists() );
308
309 jar = new JarFile( jarFile );
310
311 Map entries = jar.getManifest().getMainAttributes();
312
313 assertTrue( entries.containsKey( Attributes.Name.IMPLEMENTATION_VERSION ) );
314 assertEquals( "0.1", entries.get( Attributes.Name.IMPLEMENTATION_VERSION ) );
315 }
316 finally
317 {
318
319 if ( jar != null )
320 {
321 jar.close();
322 }
323 }
324 }
325
326 private MavenArchiver getMavenArchiver( JarArchiver jarArchiver )
327 {
328 MavenArchiver archiver = new MavenArchiver();
329 archiver.setArchiver( jarArchiver );
330 archiver.setOutputFile( jarArchiver.getDestFile() );
331 return archiver;
332 }
333
334 public void testDashesInClassPath_MSHARED_134()
335 throws IOException, ManifestException, DependencyResolutionRequiredException
336 {
337 File jarFile = new File( "target/test/dummyWithDashes.jar" );
338 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
339
340 MavenArchiver archiver = getMavenArchiver( jarArchiver );
341
342 MavenSession session = getDummySession();
343 MavenProject project = getDummyProject();
344
345 Set<Artifact> artifacts =
346 getArtifacts( getMockArtifact1(), getArtifactWithDot(), getMockArtifact2(), getMockArtifact3() );
347
348 project.setArtifacts( artifacts );
349
350 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
351 config.setForced( false );
352
353 final ManifestConfiguration mftConfig = config.getManifest();
354 mftConfig.setMainClass( "org.apache.maven.Foo" );
355 mftConfig.setAddClasspath( true );
356 mftConfig.setAddExtensions( true );
357 mftConfig.setClasspathPrefix( "./lib/" );
358
359 archiver.createArchive( session, project, config );
360 assertTrue( jarFile.exists() );
361 }
362
363 public void testDashesInClassPath_MSHARED_182()
364 throws IOException, ManifestException, DependencyResolutionRequiredException
365 {
366 File jarFile = new File( "target/test/dummy.jar" );
367 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
368 MavenArchiver archiver = getMavenArchiver( jarArchiver );
369
370 MavenSession session = getDummySession();
371 MavenProject project = getDummyProject();
372
373 Set<Artifact> artifacts =
374 getArtifacts( getMockArtifact1(), getArtifactWithDot(), getMockArtifact2(), getMockArtifact3() );
375
376 project.setArtifacts( artifacts );
377
378 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
379 config.setForced( false );
380
381 final ManifestConfiguration mftConfig = config.getManifest();
382 mftConfig.setMainClass( "org.apache.maven.Foo" );
383 mftConfig.setAddClasspath( true );
384 mftConfig.setAddExtensions( true );
385 mftConfig.setClasspathPrefix( "./lib/" );
386 config.addManifestEntry( "Key1", "value1" );
387 config.addManifestEntry( "key2", "value2" );
388
389 archiver.createArchive( session, project, config );
390 assertTrue( jarFile.exists() );
391 final Attributes mainAttributes = getJarFileManifest( jarFile ).getMainAttributes();
392 assertEquals( "value1", mainAttributes.getValue( "Key1" ) );
393 assertEquals( "value2", mainAttributes.getValue( "Key2" ) );
394 }
395
396 public void testCarriageReturnInManifestEntry()
397 throws Exception
398 {
399 File jarFile = new File( "target/test/dummy.jar" );
400 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
401
402 MavenArchiver archiver = getMavenArchiver( jarArchiver );
403
404 MavenSession session = getDummySession();
405 MavenProject project = getDummyProject();
406
407 String ls = System.getProperty( "line.separator" );
408 project.setDescription( "foo " + ls + " bar " );
409 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
410 config.setForced( true );
411 config.getManifest().setAddDefaultImplementationEntries( true );
412 config.addManifestEntry( "Description", project.getDescription() );
413
414
415 archiver.createArchive( session, project, config );
416 assertTrue( jarFile.exists() );
417
418 final Manifest manifest = getJarFileManifest( jarFile );
419 Attributes attributes = manifest.getMainAttributes();
420 assertTrue( project.getDescription().indexOf( ls ) > 0 );
421 Attributes.Name description = new Attributes.Name( "Description" );
422 String value = attributes.getValue( description );
423 assertNotNull( value );
424 assertFalse( value.indexOf( ls ) > 0 );
425 }
426
427 public void testDeprecatedCreateArchiveAPI()
428 throws Exception
429 {
430 File jarFile = new File( "target/test/dummy.jar" );
431 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
432
433 MavenArchiver archiver = getMavenArchiver( jarArchiver );
434
435 MavenProject project = getDummyProject();
436 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
437 config.setForced( true );
438 config.getManifest().setAddDefaultImplementationEntries( true );
439 config.getManifest().setAddDefaultSpecificationEntries( true );
440
441
442 archiver.createArchive( project, config );
443 assertTrue( jarFile.exists() );
444 Attributes manifest = getJarFileManifest( jarFile ).getMainAttributes();
445
446 assertEquals( "Apache Maven", manifest.get( new Attributes.Name( "Created-By" ) ) );
447
448 assertEquals( "archiver test", manifest.get( Attributes.Name.SPECIFICATION_TITLE ) );
449 assertEquals( "0.1", manifest.get( Attributes.Name.SPECIFICATION_VERSION ) );
450 assertEquals( "Apache", manifest.get( Attributes.Name.SPECIFICATION_VENDOR ) );
451
452 assertEquals( "archiver test", manifest.get( Attributes.Name.IMPLEMENTATION_TITLE ) );
453 assertEquals( "0.1", manifest.get( Attributes.Name.IMPLEMENTATION_VERSION ) );
454 assertEquals( "org.apache.dummy", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR_ID ) );
455 assertEquals( "Apache", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR ) );
456
457 assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) );
458 assertEquals( System.getProperty( "user.name" ), manifest.get( new Attributes.Name( "Built-By" ) ) );
459 }
460
461 public void testManifestEntries()
462 throws Exception
463 {
464 File jarFile = new File( "target/test/dummy.jar" );
465 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
466
467 MavenArchiver archiver = getMavenArchiver( jarArchiver );
468
469 MavenSession session = getDummySession();
470 MavenProject project = getDummyProject();
471 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
472 config.setForced( true );
473 config.getManifest().setAddDefaultImplementationEntries( true );
474 config.getManifest().setAddDefaultSpecificationEntries( true );
475
476 Map<String, String> manifestEntries = new HashMap<String, String>();
477 manifestEntries.put( "foo", "bar" );
478 manifestEntries.put( "first-name", "olivier" );
479 manifestEntries.put( "keyWithEmptyValue", null );
480 config.setManifestEntries( manifestEntries );
481
482 ManifestSection manifestSection = new ManifestSection();
483 manifestSection.setName( "UserSection" );
484 manifestSection.addManifestEntry( "key", "value" );
485 List<ManifestSection> manifestSections = new ArrayList<ManifestSection>();
486 manifestSections.add( manifestSection );
487 config.setManifestSections( manifestSections );
488 config.getManifest().setMainClass( "org.apache.maven.Foo" );
489 archiver.createArchive( session, project, config );
490 assertTrue( jarFile.exists() );
491
492 final Manifest jarFileManifest = getJarFileManifest( jarFile );
493 Attributes manifest = jarFileManifest.getMainAttributes();
494
495 assertEquals( "Apache Maven 3.0.4", manifest.get( new Attributes.Name( "Created-By" ) ) );
496
497 assertEquals( "archiver test", manifest.get( Attributes.Name.SPECIFICATION_TITLE ) );
498 assertEquals( "0.1", manifest.get( Attributes.Name.SPECIFICATION_VERSION ) );
499 assertEquals( "Apache", manifest.get( Attributes.Name.SPECIFICATION_VENDOR ) );
500
501 assertEquals( "archiver test", manifest.get( Attributes.Name.IMPLEMENTATION_TITLE ) );
502 assertEquals( "0.1", manifest.get( Attributes.Name.IMPLEMENTATION_VERSION ) );
503 assertEquals( "org.apache.dummy", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR_ID ) );
504 assertEquals( "Apache", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR ) );
505
506 assertEquals( "org.apache.maven.Foo", manifest.get( Attributes.Name.MAIN_CLASS ) );
507
508 assertEquals( "bar", manifest.get( new Attributes.Name( "foo" ) ) );
509 assertEquals( "olivier", manifest.get( new Attributes.Name( "first-name" ) ) );
510
511 assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) );
512 assertEquals( System.getProperty( "user.name" ), manifest.get( new Attributes.Name( "Built-By" ) ) );
513
514 assertTrue( StringUtils.isEmpty( manifest.getValue( new Attributes.Name( "keyWithEmptyValue" ) ) ) );
515 assertTrue( manifest.containsKey( new Attributes.Name( "keyWithEmptyValue" ) ) );
516
517 manifest = jarFileManifest.getAttributes( "UserSection" );
518
519 assertEquals( "value", manifest.get( new Attributes.Name( "key" ) ) );
520 }
521
522 public void testCreatedByManifestEntryWithoutMavenVersion()
523 throws Exception
524 {
525 File jarFile = new File( "target/test/dummy.jar" );
526 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
527
528 MavenArchiver archiver = getMavenArchiver( jarArchiver );
529
530 MavenSession session = getDummySessionWithoutMavenVersion();
531 MavenProject project = getDummyProject();
532
533 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
534 config.setForced( true );
535
536 archiver.createArchive( session, project, config );
537 assertTrue( jarFile.exists() );
538
539 final Manifest manifest = getJarFileManifest( jarFile );
540 Map entries = manifest.getMainAttributes();
541
542 assertEquals( "Apache Maven", entries.get( new Attributes.Name( "Created-By" ) ) );
543 }
544
545
546
547
548 public void testManifestSections()
549 throws Exception
550 {
551 MavenArchiver archiver = new MavenArchiver();
552
553 MavenSession session = getDummySession();
554
555 MavenProject project = getDummyProject();
556 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
557
558 ManifestSection manifestSection = new ManifestSection();
559 manifestSection.setName( "SectionOne" );
560 manifestSection.addManifestEntry( "key", "value" );
561 List<ManifestSection> manifestSections = new ArrayList<ManifestSection>();
562 manifestSections.add( manifestSection );
563 config.setManifestSections( manifestSections );
564
565 Manifest manifest = archiver.getManifest( session, project, config );
566
567 Attributes section = manifest.getAttributes( "SectionOne" );
568 assertNotNull( "The section is not present in the manifest as it should be.", section );
569
570 String attribute = section.getValue( "key" );
571 assertNotNull( "The attribute we are looking for is not present in the section.", attribute );
572 assertEquals( "The value of the attribute is wrong.", "value", attribute );
573 }
574
575 @SuppressWarnings( "ResultOfMethodCallIgnored" )
576 public void testDefaultClassPathValue()
577 throws Exception
578 {
579 MavenSession session = getDummySession();
580 MavenProject project = getDummyProject();
581 File jarFile = new File( "target/test/dummy.jar" );
582 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
583
584 MavenArchiver archiver = getMavenArchiver( jarArchiver );
585
586 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
587 config.setForced( true );
588 config.getManifest().setAddDefaultImplementationEntries( true );
589 config.getManifest().setAddDefaultSpecificationEntries( true );
590 config.getManifest().setMainClass( "org.apache.maven.Foo" );
591 config.getManifest().setAddClasspath( true );
592 archiver.createArchive( session, project, config );
593 assertTrue( jarFile.exists() );
594 final Manifest manifest = getJarFileManifest( jarFile );
595 String classPath = manifest.getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
596 assertNotNull( classPath );
597 String[] classPathEntries = StringUtils.split( classPath, " " );
598 assertEquals( "dummy1-1.0.jar", classPathEntries[0] );
599 assertEquals( "dummy2-1.5.jar", classPathEntries[1] );
600 assertEquals( "dummy3-2.0.jar", classPathEntries[2] );
601 }
602
603 @SuppressWarnings( "ResultOfMethodCallIgnored" )
604 private void deleteAndAssertNotPresent( File jarFile )
605 {
606 jarFile.delete();
607 assertFalse( jarFile.exists() );
608 }
609
610 @SuppressWarnings( "ResultOfMethodCallIgnored" )
611 public void testDefaultClassPathValue_WithSnapshot()
612 throws Exception
613 {
614 MavenSession session = getDummySession();
615 MavenProject project = getDummyProjectWithSnapshot();
616 File jarFile = new File( "target/test/dummy.jar" );
617 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
618
619 MavenArchiver archiver = getMavenArchiver( jarArchiver );
620
621 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
622 config.setForced( true );
623 config.getManifest().setAddDefaultImplementationEntries( true );
624 config.getManifest().setAddDefaultSpecificationEntries( true );
625 config.getManifest().setMainClass( "org.apache.maven.Foo" );
626 config.getManifest().setAddClasspath( true );
627 archiver.createArchive( session, project, config );
628 assertTrue( jarFile.exists() );
629
630 final Manifest manifest = getJarFileManifest( jarFile );
631 String classPath = manifest.getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
632 assertNotNull( classPath );
633 String[] classPathEntries = StringUtils.split( classPath, " " );
634 assertEquals( "dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
635 assertEquals( "dummy2-1.5.jar", classPathEntries[1] );
636 assertEquals( "dummy3-2.0.jar", classPathEntries[2] );
637 }
638
639 public void testMavenRepoClassPathValue()
640 throws Exception
641 {
642 MavenSession session = getDummySession();
643 MavenProject project = getDummyProject();
644 File jarFile = new File( "target/test/dummy.jar" );
645 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
646
647 MavenArchiver archiver = getMavenArchiver( jarArchiver );
648
649 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
650 config.setForced( true );
651 config.getManifest().setAddDefaultImplementationEntries( true );
652 config.getManifest().setAddDefaultSpecificationEntries( true );
653 config.getManifest().setMainClass( "org.apache.maven.Foo" );
654 config.getManifest().setAddClasspath( true );
655 config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY );
656 archiver.createArchive( session, project, config );
657 assertTrue( jarFile.exists() );
658 Manifest manifest = archiver.getManifest( session, project, config );
659 String[] classPathEntries =
660 StringUtils.split( new String( manifest.getMainAttributes().getValue( "Class-Path" ).getBytes() ), " " );
661 assertEquals( "org/apache/dummy/dummy1/1.0/dummy1-1.0.jar", classPathEntries[0] );
662 assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
663 assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
664
665 String classPath = getJarFileManifest( jarFile ).getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
666 assertNotNull( classPath );
667 classPathEntries = StringUtils.split( classPath, " " );
668 assertEquals( "org/apache/dummy/dummy1/1.0/dummy1-1.0.jar", classPathEntries[0] );
669 assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
670 assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
671 }
672
673 public void testMavenRepoClassPathValue_WithSnapshot()
674 throws Exception
675 {
676 MavenSession session = getDummySession();
677 MavenProject project = getDummyProjectWithSnapshot();
678 File jarFile = new File( "target/test/dummy.jar" );
679 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
680
681 MavenArchiver archiver = getMavenArchiver( jarArchiver );
682
683 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
684 config.setForced( true );
685 config.getManifest().setAddDefaultImplementationEntries( true );
686 config.getManifest().setAddDefaultSpecificationEntries( true );
687 config.getManifest().setMainClass( "org.apache.maven.Foo" );
688 config.getManifest().setAddClasspath( true );
689 config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY );
690 archiver.createArchive( session, project, config );
691 assertTrue( jarFile.exists() );
692
693 Manifest manifest = archiver.getManifest( session, project, config );
694 String[] classPathEntries =
695 StringUtils.split( new String( manifest.getMainAttributes().getValue( "Class-Path" ).getBytes() ), " " );
696 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
697 assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
698 assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
699
700 String classPath = getJarFileManifest( jarFile ).getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
701 assertNotNull( classPath );
702 classPathEntries = StringUtils.split( classPath, " " );
703 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
704 assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
705 assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
706 }
707
708 public void testCustomClassPathValue()
709 throws Exception
710 {
711 MavenSession session = getDummySession();
712 MavenProject project = getDummyProject();
713 File jarFile = new File( "target/test/dummy.jar" );
714 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
715
716 MavenArchiver archiver = getMavenArchiver( jarArchiver );
717
718 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
719 config.setForced( true );
720 config.getManifest().setAddDefaultImplementationEntries( true );
721 config.getManifest().setAddDefaultSpecificationEntries( true );
722 config.getManifest().setMainClass( "org.apache.maven.Foo" );
723 config.getManifest().setAddClasspath( true );
724 config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
725 config.getManifest().setCustomClasspathLayout(
726 "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.version}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" );
727 archiver.createArchive( session, project, config );
728 assertTrue( jarFile.exists() );
729 Manifest manifest = archiver.getManifest( session, project, config );
730 String[] classPathEntries =
731 StringUtils.split( new String( manifest.getMainAttributes().getValue( "Class-Path" ).getBytes() ), " " );
732 assertEquals( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", classPathEntries[0] );
733 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
734 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
735
736 final Manifest manifest1 = getJarFileManifest( jarFile );
737 String classPath = manifest1.getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
738 assertNotNull( classPath );
739 classPathEntries = StringUtils.split( classPath, " " );
740 assertEquals( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", classPathEntries[0] );
741 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
742 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
743 }
744
745 public void testCustomClassPathValue_WithSnapshotResolvedVersion()
746 throws Exception
747 {
748 MavenSession session = getDummySession();
749 MavenProject project = getDummyProjectWithSnapshot();
750 File jarFile = new File( "target/test/dummy.jar" );
751 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
752 MavenArchiver archiver = getMavenArchiver( jarArchiver );
753
754 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
755 config.setForced( true );
756 config.getManifest().setAddDefaultImplementationEntries( true );
757 config.getManifest().setAddDefaultSpecificationEntries( true );
758 config.getManifest().setMainClass( "org.apache.maven.Foo" );
759 config.getManifest().setAddClasspath( true );
760 config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
761 config.getManifest().setCustomClasspathLayout(
762 "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" );
763 archiver.createArchive( session, project, config );
764 assertTrue( jarFile.exists() );
765
766 Manifest manifest = archiver.getManifest( session, project, config );
767 String[] classPathEntries =
768 StringUtils.split( new String( manifest.getMainAttributes().getValue( "Class-Path" ).getBytes() ), " " );
769 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar",
770 classPathEntries[0] );
771 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
772 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
773
774 String classPath = getJarFileManifest( jarFile ).getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
775 assertNotNull( classPath );
776 classPathEntries = StringUtils.split( classPath, " " );
777 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar",
778 classPathEntries[0] );
779 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
780 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
781 }
782
783 public void testCustomClassPathValue_WithSnapshotForcingBaseVersion()
784 throws Exception
785 {
786 MavenSession session = getDummySession();
787 MavenProject project = getDummyProjectWithSnapshot();
788 File jarFile = new File( "target/test/dummy.jar" );
789 JarArchiver jarArchiver = getCleanJarArciver( jarFile );
790
791 MavenArchiver archiver = getMavenArchiver( jarArchiver );
792
793 MavenArchiveConfiguration config = new MavenArchiveConfiguration();
794 config.setForced( true );
795 config.getManifest().setAddDefaultImplementationEntries( true );
796 config.getManifest().setAddDefaultSpecificationEntries( true );
797 config.getManifest().setMainClass( "org.apache.maven.Foo" );
798 config.getManifest().setAddClasspath( true );
799 config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
800 config.getManifest().setCustomClasspathLayout(
801 "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}" );
802 archiver.createArchive( session, project, config );
803 assertTrue( jarFile.exists() );
804 Manifest manifest = archiver.getManifest( session, project, config );
805 String[] classPathEntries =
806 StringUtils.split( new String( manifest.getMainAttributes().getValue( "Class-Path" ).getBytes() ), " " );
807 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", classPathEntries[0] );
808 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
809 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
810
811 String classPath = getJarFileManifest( jarFile ).getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
812 assertNotNull( classPath );
813 classPathEntries = StringUtils.split( classPath, " " );
814 assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", classPathEntries[0] );
815 assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
816 assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
817 }
818
819 private JarArchiver getCleanJarArciver( File jarFile )
820 {
821 deleteAndAssertNotPresent( jarFile );
822 JarArchiver jarArchiver = new JarArchiver();
823 jarArchiver.setDestFile( jarFile );
824 return jarArchiver;
825 }
826
827
828
829
830
831 private MavenProject getDummyProject()
832 {
833 MavenProject project = getMavenProject();
834 File pomFile = new File( "src/test/resources/pom.xml" );
835 pomFile.setLastModified( System.currentTimeMillis() - 60000L );
836 project.setFile( pomFile );
837 Build build = new Build();
838 build.setDirectory( "target" );
839 build.setOutputDirectory( "target" );
840 project.setBuild( build );
841 project.setName( "archiver test" );
842 Organization organization = new Organization();
843 organization.setName( "Apache" );
844 project.setOrganization( organization );
845 MockArtifact artifact = new MockArtifact();
846 artifact.setGroupId( "org.apache.dummy" );
847 artifact.setArtifactId( "dummy" );
848 artifact.setVersion( "0.1" );
849 artifact.setType( "jar" );
850 artifact.setArtifactHandler( new DefaultArtifactHandler( "jar" ) );
851 project.setArtifact( artifact );
852
853 Set<Artifact> artifacts = getArtifacts( getMockArtifact1Release(), getMockArtifact2(), getMockArtifact3() );
854 project.setArtifacts( artifacts );
855
856 return project;
857 }
858
859 private MavenProject getMavenProject()
860 {
861 Model model = new Model();
862 model.setGroupId( "org.apache.dummy" );
863 model.setArtifactId( "dummy" );
864 model.setVersion( "0.1" );
865
866 final MavenProject project = new MavenProject( model );
867 project.setPluginArtifacts( Collections.EMPTY_SET );
868 project.setReportArtifacts( Collections.EMPTY_SET );
869 project.setExtensionArtifacts( Collections.EMPTY_SET );
870 project.setRemoteArtifactRepositories( Collections.EMPTY_LIST );
871 project.setPluginArtifactRepositories( Collections.EMPTY_LIST );
872 return project;
873 }
874
875
876 private MockArtifact getMockArtifact3()
877 {
878 MockArtifact artifact3 = new MockArtifact();
879 artifact3.setGroupId( "org.apache.dummy.bar" );
880 artifact3.setArtifactId( "dummy3" );
881 artifact3.setVersion( "2.0" );
882 artifact3.setScope( "runtime" );
883 artifact3.setType( "jar" );
884 artifact3.setFile( getClasspathFile( artifact3.getArtifactId() + "-" + artifact3.getVersion() + ".jar" ) );
885 return artifact3;
886 }
887
888 private MavenProject getDummyProjectWithSnapshot()
889 {
890 MavenProject project = getMavenProject();
891 File pomFile = new File( "src/test/resources/pom.xml" );
892 pomFile.setLastModified( System.currentTimeMillis() - 60000L );
893 project.setFile( pomFile );
894 Build build = new Build();
895 build.setDirectory( "target" );
896 build.setOutputDirectory( "target" );
897 project.setBuild( build );
898 project.setName( "archiver test" );
899 Organization organization = new Organization();
900 organization.setName( "Apache" );
901 project.setOrganization( organization );
902
903 MockArtifact artifact = new MockArtifact();
904 artifact.setGroupId( "org.apache.dummy" );
905 artifact.setArtifactId( "dummy" );
906 artifact.setVersion( "0.1" );
907 artifact.setType( "jar" );
908 artifact.setArtifactHandler( new DefaultArtifactHandler( "jar" ) );
909 project.setArtifact( artifact );
910
911 Set<Artifact> artifacts = getArtifacts( getMockArtifact1(), getMockArtifact2(), getMockArtifact3() );
912
913 project.setArtifacts( artifacts );
914
915 return project;
916 }
917
918 private ArtifactHandler getMockArtifactHandler()
919 {
920 return new ArtifactHandler()
921 {
922
923 public String getClassifier()
924 {
925 return null;
926 }
927
928 public String getDirectory()
929 {
930 return null;
931 }
932
933 public String getExtension()
934 {
935 return "jar";
936 }
937
938 public String getLanguage()
939 {
940 return null;
941 }
942
943 public String getPackaging()
944 {
945 return null;
946 }
947
948 public boolean isAddedToClasspath()
949 {
950 return true;
951 }
952
953 public boolean isIncludesDependencies()
954 {
955 return false;
956 }
957
958 };
959 }
960
961 private MockArtifact getMockArtifact2()
962 {
963 MockArtifact artifact2 = new MockArtifact();
964 artifact2.setGroupId( "org.apache.dummy.foo" );
965 artifact2.setArtifactId( "dummy2" );
966 artifact2.setVersion( "1.5" );
967 artifact2.setType( "jar" );
968 artifact2.setScope( "runtime" );
969 artifact2.setFile( getClasspathFile( artifact2.getArtifactId() + "-" + artifact2.getVersion() + ".jar" ) );
970 return artifact2;
971 }
972
973 private MockArtifact getArtifactWithDot()
974 {
975 MockArtifact artifact2 = new MockArtifact();
976 artifact2.setGroupId( "org.apache.dummy.foo" );
977 artifact2.setArtifactId( "dummy.dot" );
978 artifact2.setVersion( "1.5" );
979 artifact2.setType( "jar" );
980 artifact2.setScope( "runtime" );
981 artifact2.setFile( getClasspathFile( artifact2.getArtifactId() + "-" + artifact2.getVersion() + ".jar" ) );
982 return artifact2;
983 }
984
985 private MockArtifact getMockArtifact1()
986 {
987 MockArtifact artifact1 = new MockArtifact();
988 artifact1.setGroupId( "org.apache.dummy" );
989 artifact1.setArtifactId( "dummy1" );
990 artifact1.setSnapshotVersion( "1.1-20081022.112233-1", "1.1-SNAPSHOT" );
991 artifact1.setType( "jar" );
992 artifact1.setScope( "runtime" );
993 artifact1.setFile( getClasspathFile( artifact1.getArtifactId() + "-" + artifact1.getVersion() + ".jar" ) );
994 return artifact1;
995 }
996
997 private MockArtifact getMockArtifact1Release()
998 {
999 MockArtifact artifact1 = new MockArtifact();
1000 artifact1.setGroupId( "org.apache.dummy" );
1001 artifact1.setArtifactId( "dummy1" );
1002 artifact1.setVersion( "1.0" );
1003 artifact1.setType( "jar" );
1004 artifact1.setScope( "runtime" );
1005 artifact1.setFile( getClasspathFile( artifact1.getArtifactId() + "-" + artifact1.getVersion() + ".jar" ) );
1006 return artifact1;
1007 }
1008
1009 private File getClasspathFile( String file )
1010 {
1011 URL resource = Thread.currentThread().getContextClassLoader().getResource( file );
1012 if ( resource == null )
1013 {
1014 fail( "Cannot retrieve java.net.URL for file: " + file + " on the current test classpath." );
1015 }
1016
1017 URI uri = new File( resource.getPath() ).toURI().normalize();
1018
1019 return new File( uri.getPath().replaceAll( "%20", " " ) );
1020 }
1021
1022 private MavenSession getDummySession()
1023 {
1024 Properties executionProperties = new Properties();
1025 executionProperties.put( "maven.version", "3.0.4" );
1026
1027 return getDummySession( executionProperties );
1028 }
1029
1030 private MavenSession getDummySessionWithoutMavenVersion()
1031 {
1032 return getDummySession( new Properties() );
1033 }
1034
1035 private MavenSession getDummySession( Properties executionProperties )
1036 {
1037 PlexusContainer container = null;
1038 Settings settings = null;
1039 ArtifactRepository localRepo = null;
1040 EventDispatcher eventDispatcher = null;
1041 ReactorManager reactorManager = null;
1042 List goals = null;
1043 String executionRootDir = null;
1044 Date startTime = new Date();
1045
1046 return new MavenSession( container, settings, localRepo, eventDispatcher, reactorManager, goals,
1047 executionRootDir, executionProperties, startTime );
1048 }
1049
1050 private Set<Artifact> getArtifacts( Artifact... artifacts )
1051 {
1052 final ArtifactHandler mockArtifactHandler = getMockArtifactHandler();
1053 Set<Artifact> result = new TreeSet<Artifact>( new ArtifactComparator() );
1054 for ( Artifact artifact : artifacts )
1055 {
1056 artifact.setArtifactHandler( mockArtifactHandler );
1057 result.add( artifact );
1058 }
1059 return result;
1060 }
1061
1062 public Manifest getJarFileManifest( File jarFile )
1063 throws IOException
1064 {
1065 JarFile jar = null;
1066 try
1067 {
1068 jar = new JarFile( jarFile );
1069 return jar.getManifest();
1070 }
1071 finally
1072 {
1073 if ( jar != null )
1074 {
1075 jar.close();
1076 }
1077 }
1078
1079 }
1080 }