1 | |
package org.apache.maven.plugin.ear.util; |
2 | |
|
3 | |
import org.apache.maven.archiver.MavenArchiveConfiguration; |
4 | |
import org.apache.maven.archiver.MavenArchiver; |
5 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
6 | |
import org.apache.maven.plugin.ear.EarModule; |
7 | |
import org.apache.maven.project.MavenProject; |
8 | |
import org.codehaus.plexus.archiver.jar.Manifest; |
9 | |
import org.codehaus.plexus.archiver.jar.ManifestException; |
10 | |
|
11 | |
import java.util.Iterator; |
12 | |
import java.util.List; |
13 | |
import java.util.Set; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class EarMavenArchiver |
42 | |
extends MavenArchiver |
43 | |
{ |
44 | |
public static final String CLASS_PATH_KEY = "Class-Path"; |
45 | |
|
46 | |
private final List earModules; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public EarMavenArchiver( List earModules ) |
56 | 0 | { |
57 | 0 | this.earModules = earModules; |
58 | 0 | } |
59 | |
|
60 | |
public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config ) |
61 | |
throws ManifestException, DependencyResolutionRequiredException |
62 | |
{ |
63 | 0 | final Manifest manifest = super.getManifest( project, config ); |
64 | 0 | if ( config.getManifest().isAddClasspath() ) |
65 | |
{ |
66 | 0 | String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() ); |
67 | |
|
68 | |
|
69 | 0 | final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config ); |
70 | 0 | if ( userSuppliedClassPathEntry != null ) |
71 | |
{ |
72 | 0 | earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry; |
73 | |
} |
74 | |
|
75 | |
|
76 | 0 | final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY ); |
77 | 0 | if ( classPathAttr != null ) |
78 | |
{ |
79 | 0 | classPathAttr.setValue( earManifestClassPathEntry ); |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry ); |
84 | 0 | manifest.addConfiguredAttribute( attr ); |
85 | |
} |
86 | |
} |
87 | 0 | return manifest; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
protected String generateClassPathEntry( String classPathPrefix ) |
98 | |
{ |
99 | 0 | final StringBuffer classpath = new StringBuffer(); |
100 | 0 | final Iterator it = earModules.iterator(); |
101 | 0 | while ( it.hasNext() ) |
102 | |
{ |
103 | 0 | final EarModule earModule = (EarModule) it.next(); |
104 | 0 | if ( !earModule.isExcluded() ) |
105 | |
{ |
106 | 0 | classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " ); |
107 | |
} |
108 | 0 | } |
109 | 0 | return classpath.toString().trim(); |
110 | |
} |
111 | |
|
112 | |
protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config ) |
113 | |
{ |
114 | 0 | if ( config.getManifestEntries() != null ) |
115 | |
{ |
116 | 0 | final Set keys = config.getManifestEntries().keySet(); |
117 | 0 | for ( Iterator iter = keys.iterator(); iter.hasNext(); ) |
118 | |
{ |
119 | 0 | String key = (String) iter.next(); |
120 | 0 | String value = (String) config.getManifestEntries().get( key ); |
121 | 0 | if ( "Class-Path".equals( key ) && value != null ) |
122 | |
{ |
123 | 0 | return value; |
124 | |
|
125 | |
} |
126 | |
|
127 | 0 | } |
128 | |
} |
129 | 0 | return null; |
130 | |
} |
131 | |
} |