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.execution.MavenSession; |
29 | |
import org.apache.maven.plugin.ear.EarModule; |
30 | |
import org.apache.maven.project.MavenProject; |
31 | |
import org.codehaus.plexus.archiver.jar.Manifest; |
32 | |
import org.codehaus.plexus.archiver.jar.ManifestException; |
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<EarModule> earModules; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public EarMavenArchiver( List<EarModule> earModules ) |
56 | 3 | { |
57 | 3 | this.earModules = earModules; |
58 | 3 | } |
59 | |
|
60 | |
|
61 | |
public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config ) |
62 | |
throws ManifestException, DependencyResolutionRequiredException |
63 | |
{ |
64 | 0 | return this.getManifest( null, project, config ); |
65 | |
} |
66 | |
|
67 | |
public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config ) |
68 | |
throws ManifestException, DependencyResolutionRequiredException |
69 | |
{ |
70 | 0 | final Manifest manifest = super.getManifest( session, project, config ); |
71 | 0 | if ( config.getManifest().isAddClasspath() ) |
72 | |
{ |
73 | 0 | String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() ); |
74 | |
|
75 | |
|
76 | 0 | final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config ); |
77 | 0 | if ( userSuppliedClassPathEntry != null ) |
78 | |
{ |
79 | 0 | earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry; |
80 | |
} |
81 | |
|
82 | |
|
83 | 0 | final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY ); |
84 | 0 | if ( classPathAttr != null ) |
85 | |
{ |
86 | 0 | classPathAttr.setValue( earManifestClassPathEntry ); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry ); |
91 | 0 | manifest.addConfiguredAttribute( attr ); |
92 | |
} |
93 | |
} |
94 | 0 | return manifest; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
protected String generateClassPathEntry( String classPathPrefix ) |
105 | |
{ |
106 | 3 | final StringBuffer classpath = new StringBuffer(); |
107 | 3 | for ( final EarModule earModule : earModules ) |
108 | |
{ |
109 | 4 | if ( !earModule.isExcluded() ) |
110 | |
{ |
111 | 4 | classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " ); |
112 | |
} |
113 | |
} |
114 | 3 | return classpath.toString().trim(); |
115 | |
} |
116 | |
|
117 | |
protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config ) |
118 | |
{ |
119 | 0 | if ( config.getManifestEntries() != null ) |
120 | |
{ |
121 | |
@SuppressWarnings( "unchecked" ) |
122 | 0 | final Set<String> keys = config.getManifestEntries().keySet(); |
123 | 0 | for ( String key : keys ) |
124 | |
{ |
125 | 0 | String value = (String) config.getManifestEntries().get( key ); |
126 | 0 | if ( "Class-Path".equals( key ) && value != null ) |
127 | |
{ |
128 | 0 | return value; |
129 | |
|
130 | |
} |
131 | |
|
132 | 0 | } |
133 | |
} |
134 | 0 | return null; |
135 | |
} |
136 | |
} |