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