1 package org.apache.maven.plugins.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.plugins.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 public class EarMavenArchiver
41 extends MavenArchiver
42 {
43
44
45
46 public static final String CLASS_PATH_KEY = "Class-Path";
47
48 private final List<EarModule> earModules;
49
50
51
52
53
54
55 public EarMavenArchiver( List<EarModule> earModules )
56 {
57 this.earModules = earModules;
58 }
59
60
61
62
63
64
65
66
67
68 public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
69 throws ManifestException, DependencyResolutionRequiredException
70 {
71 return this.getManifest( null, project, config );
72 }
73
74
75 public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config )
76 throws ManifestException, DependencyResolutionRequiredException
77 {
78 final Manifest manifest = super.getManifest( session, project, config );
79 if ( config.getManifest().isAddClasspath() )
80 {
81 String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
82
83
84 final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
85 if ( userSuppliedClassPathEntry != null )
86 {
87 earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
88 }
89
90
91 final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
92 if ( classPathAttr != null )
93 {
94 classPathAttr.setValue( earManifestClassPathEntry );
95 }
96 else
97 {
98 final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
99 manifest.addConfiguredAttribute( attr );
100 }
101 }
102 return manifest;
103 }
104
105
106
107
108
109
110
111 protected String generateClassPathEntry( String classPathPrefix )
112 {
113 final StringBuilder classpath = new StringBuilder();
114 for ( final EarModule earModule : earModules )
115 {
116 if ( !earModule.isExcluded() )
117 {
118 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
119 }
120 }
121 return classpath.toString().trim();
122 }
123
124
125
126
127
128 protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
129 {
130 if ( config.getManifestEntries() != null )
131 {
132 final Set<String> keys = config.getManifestEntries().keySet();
133 for ( String key : keys )
134 {
135 String value = config.getManifestEntries().get( key );
136 if ( "Class-Path".equals( key ) && value != null )
137 {
138 return value;
139
140 }
141
142 }
143 }
144 return null;
145 }
146 }