1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.plugin.eclipse.writers.wtp; |
20 | |
|
21 | |
import java.io.File; |
22 | |
|
23 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
24 | |
import org.apache.maven.plugin.MojoExecutionException; |
25 | |
import org.apache.maven.plugin.eclipse.Constants; |
26 | |
import org.apache.maven.plugin.eclipse.Messages; |
27 | |
import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter; |
28 | |
import org.apache.maven.plugin.ide.IdeDependency; |
29 | |
import org.apache.maven.plugin.ide.IdeUtils; |
30 | |
import org.apache.maven.plugin.ide.JeeUtils; |
31 | |
import org.apache.maven.project.MavenProject; |
32 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 2 | public abstract class AbstractWtpResourceWriter |
41 | |
extends AbstractEclipseWriter |
42 | |
{ |
43 | |
|
44 | |
private static final String ELT_DEPENDENCY_OBJECT = "dependent-object"; |
45 | |
|
46 | |
private static final String ELT_DEPENDENCY_TYPE = "dependency-type"; |
47 | |
|
48 | |
private static final String ATTR_HANDLE = "handle"; |
49 | |
|
50 | |
private static final String ELT_DEPENDENT_MODULE = "dependent-module"; |
51 | |
|
52 | |
protected static final String ATTR_VALUE = "value"; |
53 | |
|
54 | |
protected static final String ATTR_NAME = "name"; |
55 | |
|
56 | |
protected static final String ELT_PROPERTY = "property"; |
57 | |
|
58 | |
protected static final String ELT_VERSION = "version"; |
59 | |
|
60 | |
protected static final String ATTR_MODULE_TYPE_ID = "module-type-id"; |
61 | |
|
62 | |
protected static final String ATTR_SOURCE_PATH = "source-path"; |
63 | |
|
64 | |
protected static final String ATTR_DEPLOY_PATH = "deploy-path"; |
65 | |
|
66 | |
protected static final String ELT_WB_RESOURCE = "wb-resource"; |
67 | |
|
68 | |
protected static final String ELT_MODULE_TYPE = "module-type"; |
69 | |
|
70 | |
protected static final String ATTR_DEPLOY_NAME = "deploy-name"; |
71 | |
|
72 | |
protected static final String ELT_WB_MODULE = "wb-module"; |
73 | |
|
74 | |
protected static final String ATTR_MODULE_ID = "id"; |
75 | |
|
76 | |
protected static final String ATTR_PROJECT_VERSION = "project-version"; |
77 | |
|
78 | |
protected static final String ELT_PROJECT_MODULES = "project-modules"; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
protected void writeModuleTypeAccordingToPackaging( MavenProject project, XMLWriter writer, |
86 | |
File buildOutputDirectory ) |
87 | |
throws MojoExecutionException |
88 | |
{ |
89 | 0 | if ( Constants.PROJECT_PACKAGING_WAR.equals( config.getPackaging() ) ) |
90 | |
{ |
91 | 0 | writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.web" ); |
92 | |
|
93 | 0 | writer.startElement( ELT_VERSION ); |
94 | |
String servletVersion; |
95 | 0 | if ( config.getJeeVersion() != null ) |
96 | |
{ |
97 | 0 | servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion(); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | servletVersion = JeeUtils.resolveServletVersion( config.getProject() ); |
102 | |
} |
103 | 0 | writer.writeText( servletVersion ); |
104 | 0 | writer.endElement(); |
105 | |
|
106 | 0 | String contextRoot = config.getContextName(); |
107 | |
|
108 | 0 | writer.startElement( ELT_PROPERTY ); |
109 | 0 | writer.addAttribute( ATTR_NAME, "context-root" ); |
110 | 0 | writer.addAttribute( ATTR_VALUE, contextRoot ); |
111 | 0 | writer.endElement(); |
112 | 0 | } |
113 | 0 | else if ( Constants.PROJECT_PACKAGING_EJB.equals( config.getPackaging() ) ) |
114 | |
{ |
115 | 0 | writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ejb" ); |
116 | |
|
117 | 0 | writer.startElement( ELT_VERSION ); |
118 | |
|
119 | |
String ejbVersion; |
120 | 0 | if ( config.getJeeVersion() != null ) |
121 | |
{ |
122 | 0 | ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion(); |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | 0 | ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() ); |
127 | |
} |
128 | 0 | writer.writeText( ejbVersion ); |
129 | |
|
130 | 0 | writer.endElement(); |
131 | |
|
132 | 0 | writer.startElement( ELT_PROPERTY ); |
133 | 0 | writer.addAttribute( ATTR_NAME, "java-output-path" ); |
134 | 0 | writer.addAttribute( ATTR_VALUE, "/" + |
135 | |
IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) ); |
136 | 0 | writer.endElement(); |
137 | |
|
138 | 0 | } |
139 | 0 | else if ( Constants.PROJECT_PACKAGING_EAR.equals( config.getPackaging() ) ) |
140 | |
{ |
141 | 0 | writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ear" ); |
142 | |
|
143 | 0 | writer.startElement( ELT_VERSION ); |
144 | |
String jeeVersion; |
145 | 0 | if ( config.getJeeVersion() != null ) |
146 | |
{ |
147 | 0 | jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion(); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() ); |
152 | |
} |
153 | 0 | writer.writeText( jeeVersion ); |
154 | 0 | writer.endElement(); |
155 | 0 | } |
156 | |
else |
157 | |
{ |
158 | |
|
159 | 0 | writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.utility" ); |
160 | |
|
161 | 0 | writer.startElement( ELT_PROPERTY ); |
162 | 0 | writer.addAttribute( ATTR_NAME, "java-output-path" ); |
163 | 0 | writer.addAttribute( ATTR_VALUE, "/" + |
164 | |
IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) ); |
165 | 0 | writer.endElement(); |
166 | |
} |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
protected void addDependency( XMLWriter writer, IdeDependency dep, ArtifactRepository localRepository, |
179 | |
File basedir, String deployPath ) |
180 | |
throws MojoExecutionException |
181 | |
{ |
182 | |
String handle; |
183 | 4 | String dependentObject = null; |
184 | |
String archiveName; |
185 | |
|
186 | |
|
187 | 4 | if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() ) |
188 | |
|| Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) ) |
189 | |
{ |
190 | 2 | deployPath = "/"; |
191 | |
} |
192 | |
|
193 | 4 | if ( dep.isReferencedProject() ) |
194 | |
{ |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | 4 | handle = "module:/resource/" + dep.getEclipseProjectName() + "/" + dep.getEclipseProjectName(); |
201 | |
|
202 | 4 | String archiveExtension = dep.getType(); |
203 | 4 | if ( Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) ) |
204 | |
{ |
205 | 2 | dependentObject = "EjbModule_"; |
206 | |
|
207 | 2 | archiveExtension = Constants.PROJECT_PACKAGING_JAR; |
208 | |
} |
209 | 2 | else if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() ) ) |
210 | |
{ |
211 | 0 | dependentObject = "WebModule_"; |
212 | |
} |
213 | 4 | archiveName = dep.getEclipseProjectName() + "." + archiveExtension; |
214 | 4 | } |
215 | |
else |
216 | |
{ |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | 0 | File artifactPath = dep.getFile(); |
223 | |
|
224 | 0 | if ( artifactPath == null ) |
225 | |
{ |
226 | 0 | log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); |
227 | 0 | return; |
228 | |
} |
229 | |
|
230 | 0 | String fullPath = artifactPath.getPath(); |
231 | 0 | File repoFile = new File( fullPath ); |
232 | |
|
233 | 0 | if ( dep.isSystemScoped() ) |
234 | |
{ |
235 | 0 | handle = "module:/classpath/lib/" |
236 | |
+ IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false ); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 0 | File localRepositoryFile = new File( localRepository.getBasedir() ); |
241 | 0 | String relativePath = IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, repoFile, false ); |
242 | |
|
243 | 0 | if ( !new File( relativePath ).isAbsolute() ) |
244 | |
{ |
245 | 0 | handle = "module:/classpath/var/M2_REPO/" |
246 | |
+ relativePath; |
247 | |
} |
248 | |
else |
249 | |
{ |
250 | 0 | handle = "module:/classpath/lib/" |
251 | |
+ IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false ); |
252 | |
} |
253 | |
} |
254 | 0 | if ( Constants.PROJECT_PACKAGING_EAR.equals( this.config.getPackaging() ) && !"/".equals( deployPath ) ) |
255 | |
{ |
256 | |
|
257 | |
|
258 | |
|
259 | 0 | archiveName = "../" + artifactPath.getName(); |
260 | |
} |
261 | |
else |
262 | |
{ |
263 | 0 | archiveName = artifactPath.getName(); |
264 | |
} |
265 | |
} |
266 | |
|
267 | 4 | writer.startElement( ELT_DEPENDENT_MODULE ); |
268 | |
|
269 | 4 | writer.addAttribute( "archiveName", archiveName ); |
270 | |
|
271 | 4 | writer.addAttribute( ATTR_DEPLOY_PATH, deployPath ); |
272 | 4 | writer.addAttribute( ATTR_HANDLE, handle ); |
273 | |
|
274 | 4 | if ( dependentObject != null && config.getWtpVersion() >= 2.0f ) |
275 | |
{ |
276 | 0 | writer.startElement( ELT_DEPENDENCY_OBJECT ); |
277 | 0 | writer.writeText( dependentObject + System.identityHashCode( dep ) ); |
278 | 0 | writer.endElement(); |
279 | |
} |
280 | |
|
281 | 4 | writer.startElement( ELT_DEPENDENCY_TYPE ); |
282 | 4 | writer.writeText( "uses" ); |
283 | 4 | writer.endElement(); |
284 | |
|
285 | 4 | writer.endElement(); |
286 | 4 | } |
287 | |
|
288 | |
protected void writeWarOrEarResources( XMLWriter writer, MavenProject project, ArtifactRepository localRepository ) |
289 | |
throws MojoExecutionException |
290 | |
{ |
291 | |
|
292 | 2 | String deployDir = |
293 | |
IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN, "defaultLibBundleDir", |
294 | |
"/" ); |
295 | |
|
296 | 2 | if ( project.getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) ) |
297 | |
{ |
298 | 0 | deployDir = "/WEB-INF/lib"; |
299 | |
} |
300 | |
|
301 | 6 | for ( int j = 0; j < config.getDeps().length; j++ ) |
302 | |
{ |
303 | 4 | IdeDependency dep = config.getDeps()[j]; |
304 | 4 | String type = dep.getType(); |
305 | |
|
306 | |
|
307 | |
|
308 | 4 | if ( ( !dep.isTestDependency() && !dep.isProvided() && !dep.isSystemScopedOutsideProject( project ) ) |
309 | |
&& ( Constants.PROJECT_PACKAGING_JAR.equals( type ) || Constants.PROJECT_PACKAGING_EJB.equals( type ) |
310 | |
|| "ejb-client".equals( type ) || Constants.PROJECT_PACKAGING_WAR.equals( type ) ) ) |
311 | |
{ |
312 | 4 | addDependency( writer, dep, localRepository, config.getProject().getBasedir(), deployDir ); |
313 | |
} |
314 | |
} |
315 | 2 | } |
316 | |
|
317 | |
} |