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