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