1 package org.apache.maven.plugin.ear;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService;
28 import org.apache.maven.plugin.ear.util.JavaEEVersion;
29 import org.apache.maven.project.MavenProject;
30 import org.codehaus.plexus.configuration.PlexusConfiguration;
31 import org.codehaus.plexus.configuration.PlexusConfigurationException;
32
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Set;
38
39
40
41
42
43
44
45 public abstract class AbstractEarMojo
46 extends AbstractMojo
47 {
48 public static final String APPLICATION_XML_URI = "META-INF/application.xml";
49
50 public static final String META_INF = "META-INF";
51
52 public static final String UTF_8 = "UTF-8";
53
54
55
56
57
58
59
60 protected String version;
61
62
63
64
65
66
67 protected String encoding;
68
69
70
71
72
73
74 protected String generatedDescriptorLocation;
75
76
77
78
79
80
81
82
83 protected MavenProject project;
84
85
86
87
88
89
90 private EarModule[] modules;
91
92
93
94
95
96
97 protected PlexusConfiguration artifactTypeMappings;
98
99
100
101
102
103
104 protected String defaultLibBundleDir;
105
106
107
108
109
110
111 private Boolean includeLibInApplicationXml = Boolean.FALSE;
112
113
114
115
116
117
118
119 private String fileNameMapping;
120
121
122
123
124
125
126
127 private File workDirectory;
128
129
130
131
132
133
134 private PlexusConfiguration jboss;
135
136
137
138
139
140
141
142 private String mainArtifactId = "none";
143
144 private List earModules;
145
146 private List allModules;
147
148 private JbossConfiguration jbossConfiguration;
149
150 public void execute()
151 throws MojoExecutionException, MojoFailureException
152 {
153 final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
154 getLog().debug( "Resolving artifact type mappings ..." );
155 ArtifactTypeMappingService typeMappingService;
156 try
157 {
158 typeMappingService = new ArtifactTypeMappingService();
159 typeMappingService.configure( artifactTypeMappings );
160 }
161 catch ( EarPluginException e )
162 {
163 throw new MojoExecutionException( "Failed to initialize artifact type mappings", e );
164 }
165 catch ( PlexusConfigurationException e )
166 {
167 throw new MojoExecutionException( "Invalid artifact type mappings configuration", e );
168 }
169
170 getLog().debug( "Initializing JBoss configuration if necessary ..." );
171 try
172 {
173 initializeJbossConfiguration();
174 }
175 catch ( EarPluginException e )
176 {
177 throw new MojoExecutionException( "Failed to initialize JBoss configuration", e );
178 }
179
180 getLog().debug( "Initializing ear execution context" );
181 EarExecutionContext earExecutionContext =
182 new EarExecutionContext( project, mainArtifactId, defaultLibBundleDir, jbossConfiguration, fileNameMapping,
183 typeMappingService );
184
185 getLog().debug( "Resolving ear modules ..." );
186 allModules = new ArrayList();
187 try
188 {
189 if ( modules != null && modules.length > 0 )
190 {
191
192 EarModule module = null;
193
194 for ( int i = 0; i < modules.length; i++ )
195 {
196 module = modules[i];
197 getLog().debug( "Resolving ear module[" + module + "]" );
198 module.setEarExecutionContext( earExecutionContext );
199 module.resolveArtifact( project.getArtifacts() );
200 allModules.add( module );
201 }
202 }
203
204
205 Set artifacts = project.getArtifacts();
206 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
207 {
208 Artifact artifact = (Artifact) iter.next();
209
210
211
212 if ( "pom".equals( artifact.getType() ) )
213 {
214 continue;
215 }
216
217
218
219 ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
220 if ( !isArtifactRegistered( artifact, allModules ) && !artifact.isOptional() &&
221 filter.include( artifact ) )
222 {
223 EarModule module = EarModuleFactory.newEarModule( artifact, javaEEVersion, defaultLibBundleDir,
224 includeLibInApplicationXml, typeMappingService );
225 module.setEarExecutionContext( earExecutionContext );
226 allModules.add( module );
227 }
228 }
229 }
230 catch ( EarPluginException e )
231 {
232 throw new MojoExecutionException( "Failed to initialize ear modules", e );
233 }
234
235
236 earModules = new ArrayList();
237 for ( Iterator iter = allModules.iterator(); iter.hasNext(); )
238 {
239 EarModule earModule = (EarModule) iter.next();
240 if ( earModule.isExcluded() )
241 {
242 getLog().debug( "Skipping ear module[" + earModule + "]" );
243 }
244 else
245 {
246 earModules.add( earModule );
247 }
248 }
249
250 }
251
252 protected List getModules()
253 {
254 if ( earModules == null )
255 {
256 throw new IllegalStateException( "Ear modules have not been initialized" );
257 }
258 return earModules;
259 }
260
261 protected MavenProject getProject()
262 {
263 return project;
264 }
265
266 protected File getWorkDirectory()
267 {
268 return workDirectory;
269 }
270
271 protected JbossConfiguration getJbossConfiguration()
272 {
273 return jbossConfiguration;
274 }
275
276 private static boolean isArtifactRegistered( Artifact a, List currentList )
277 {
278 Iterator i = currentList.iterator();
279 while ( i.hasNext() )
280 {
281 EarModule em = (EarModule) i.next();
282 if ( em.getArtifact().equals( a ) )
283 {
284 return true;
285 }
286 }
287 return false;
288 }
289
290
291
292
293
294
295 private void initializeJbossConfiguration()
296 throws EarPluginException
297 {
298 if ( jboss == null )
299 {
300 jbossConfiguration = null;
301 }
302 else
303 {
304 try
305 {
306 String version = jboss.getChild( JbossConfiguration.VERSION ).getValue();
307 if ( version == null )
308 {
309 getLog().info( "JBoss version not set, using JBoss 4 by default" );
310 version = JbossConfiguration.VERSION_4;
311 }
312 final String securityDomain = jboss.getChild( JbossConfiguration.SECURITY_DOMAIN ).getValue();
313 final String unauthenticatedPrincipal =
314 jboss.getChild( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL ).getValue();
315
316 final PlexusConfiguration loaderRepositoryEl = jboss.getChild( JbossConfiguration.LOADER_REPOSITORY );
317 final String loaderRepository = loaderRepositoryEl.getValue();
318 final String loaderRepositoryClass =
319 loaderRepositoryEl.getAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE );
320 final PlexusConfiguration loaderRepositoryConfigEl =
321 jboss.getChild( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
322 final String loaderRepositoryConfig = loaderRepositoryConfigEl.getValue();
323 final String configParserClass =
324 loaderRepositoryConfigEl.getAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE );
325
326 final String jmxName = jboss.getChild( JbossConfiguration.JMX_NAME ).getValue();
327 final String moduleOrder = jboss.getChild( JbossConfiguration.MODULE_ORDER ).getValue();
328
329 final List dataSources = new ArrayList();
330 final PlexusConfiguration dataSourcesEl = jboss.getChild( JbossConfiguration.DATASOURCES );
331 if ( dataSourcesEl != null )
332 {
333
334 final PlexusConfiguration[] dataSourcesConfig =
335 dataSourcesEl.getChildren( JbossConfiguration.DATASOURCE );
336 for ( int i = 0; i < dataSourcesConfig.length; i++ )
337 {
338 PlexusConfiguration dataSourceConfig = dataSourcesConfig[i];
339 dataSources.add( dataSourceConfig.getValue() );
340
341 }
342 }
343 final String libraryDirectory = jboss.getChild( JbossConfiguration.LIBRARY_DIRECTORY ).getValue();
344 jbossConfiguration = new JbossConfiguration( version, securityDomain, unauthenticatedPrincipal, jmxName,
345 loaderRepository, moduleOrder, dataSources,
346 libraryDirectory, loaderRepositoryConfig,
347 loaderRepositoryClass, configParserClass );
348 }
349 catch ( PlexusConfigurationException e )
350 {
351 throw new EarPluginException( "Invalid JBoss configuration", e );
352 }
353 }
354 }
355 }