1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package org.apache.maven.plugin.eclipse.writers; |
5 |
|
|
6 |
|
import java.io.File; |
7 |
|
import java.util.Iterator; |
8 |
|
|
9 |
|
import org.apache.maven.artifact.repository.ArtifactRepository; |
10 |
|
import org.apache.maven.model.Dependency; |
11 |
|
import org.apache.maven.plugin.MojoExecutionException; |
12 |
|
import org.apache.maven.plugin.eclipse.Messages; |
13 |
|
import org.apache.maven.plugin.ide.IdeDependency; |
14 |
|
import org.apache.maven.plugin.ide.IdeUtils; |
15 |
|
import org.apache.maven.project.MavenProject; |
16 |
|
import org.codehaus.plexus.util.StringUtils; |
17 |
|
import org.codehaus.plexus.util.xml.XMLWriter; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
5 |
public abstract class AbstractWtpResourceWriter |
25 |
|
extends AbstractEclipseWriter |
26 |
|
{ |
27 |
|
|
28 |
|
private static final String ELT_DEPENDENCY_TYPE = "dependency-type"; |
29 |
|
|
30 |
|
private static final String ATTR_HANDLE = "handle"; |
31 |
|
|
32 |
|
private static final String ELT_DEPENDENT_MODULE = "dependent-module"; |
33 |
|
|
34 |
|
protected static final String ATTR_VALUE = "value"; |
35 |
|
|
36 |
|
protected static final String ATTR_NAME = "name"; |
37 |
|
|
38 |
|
protected static final String ELT_PROPERTY = "property"; |
39 |
|
|
40 |
|
protected static final String ELT_VERSION = "version"; |
41 |
|
|
42 |
|
protected static final String ATTR_MODULE_TYPE_ID = "module-type-id"; |
43 |
|
|
44 |
|
protected static final String ATTR_SOURCE_PATH = "source-path"; |
45 |
|
|
46 |
|
protected static final String ATTR_DEPLOY_PATH = "deploy-path"; |
47 |
|
|
48 |
|
protected static final String ELT_WB_RESOURCE = "wb-resource"; |
49 |
|
|
50 |
|
protected static final String ELT_MODULE_TYPE = "module-type"; |
51 |
|
|
52 |
|
protected static final String ATTR_DEPLOY_NAME = "deploy-name"; |
53 |
|
|
54 |
|
protected static final String ELT_WB_MODULE = "wb-module"; |
55 |
|
|
56 |
|
protected static final String ATTR_MODULE_ID = "id"; |
57 |
|
|
58 |
|
protected static final String ATTR_PROJECT_VERSION = "project-version"; |
59 |
|
|
60 |
|
protected static final String ELT_PROJECT_MODULES = "project-modules"; |
61 |
|
|
62 |
|
protected static final String ARTIFACT_MAVEN_WAR_PLUGIN = "maven-war-plugin"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
protected void writeModuleTypeAccordingToPackaging( MavenProject project, XMLWriter writer, File buildOutputDirectory ) |
70 |
|
throws MojoExecutionException |
71 |
|
{ |
72 |
1 |
if ( "war".equals( project.getPackaging() ) ) |
73 |
|
{ |
74 |
0 |
writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.web" ); |
75 |
|
|
76 |
0 |
writer.startElement( ELT_VERSION ); |
77 |
|
|
78 |
0 |
writer.writeText( resolveServletVersion() ); |
79 |
0 |
writer.endElement(); |
80 |
|
|
81 |
|
|
82 |
0 |
String contextRoot = project.getArtifactId(); |
83 |
0 |
String finalName = project.getBuild().getFinalName(); |
84 |
0 |
if ( !finalName.equals( project.getArtifactId() + "-" + project.getVersion() ) ) |
85 |
|
{ |
86 |
0 |
contextRoot = finalName; |
87 |
|
} |
88 |
|
|
89 |
0 |
writer.startElement( ELT_PROPERTY ); |
90 |
0 |
writer.addAttribute( ATTR_NAME, "context-root" ); |
91 |
0 |
writer.addAttribute( ATTR_VALUE, contextRoot ); |
92 |
0 |
writer.endElement(); |
93 |
0 |
} |
94 |
1 |
else if ( "ejb".equals( config.getProject().getPackaging() ) ) |
95 |
|
{ |
96 |
0 |
writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ejb" ); |
97 |
|
|
98 |
0 |
writer.startElement( ELT_VERSION ); |
99 |
0 |
writer.writeText( resolveEjbVersion() ); |
100 |
|
|
101 |
0 |
writer.endElement(); |
102 |
|
|
103 |
0 |
writer.startElement( ELT_PROPERTY ); |
104 |
0 |
writer.addAttribute( ATTR_NAME, "java-output-path" ); |
105 |
0 |
writer.addAttribute( ATTR_VALUE, "/" + |
106 |
|
IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) ); |
107 |
0 |
writer.endElement(); |
108 |
|
|
109 |
0 |
} |
110 |
1 |
else if ( "ear".equals( config.getProject().getPackaging() ) ) |
111 |
|
{ |
112 |
0 |
writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ear" ); |
113 |
|
|
114 |
0 |
writer.startElement( ELT_VERSION ); |
115 |
0 |
writer.writeText( resolveJ2eeVersion() ); |
116 |
0 |
writer.endElement(); |
117 |
0 |
} |
118 |
|
else |
119 |
|
{ |
120 |
|
|
121 |
1 |
writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.utility" ); |
122 |
|
|
123 |
1 |
writer.startElement( ELT_PROPERTY ); |
124 |
1 |
writer.addAttribute( ATTR_NAME, "java-output-path" ); |
125 |
1 |
writer.addAttribute( ATTR_VALUE, "/" + |
126 |
|
IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) ); |
127 |
1 |
writer.endElement(); |
128 |
|
} |
129 |
1 |
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
protected void addDependency( XMLWriter writer, IdeDependency dep, ArtifactRepository localRepository, File basedir ) |
141 |
|
throws MojoExecutionException |
142 |
|
{ |
143 |
|
String handle; |
144 |
|
|
145 |
0 |
if ( dep.isReferencedProject() ) |
146 |
|
{ |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
0 |
handle = "module:/resource/" + dep.getArtifactId() + "/" + dep.getArtifactId(); |
153 |
0 |
} |
154 |
|
else |
155 |
|
{ |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
0 |
File artifactPath = dep.getFile(); |
162 |
|
|
163 |
0 |
if ( artifactPath == null ) |
164 |
|
{ |
165 |
0 |
log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); |
166 |
0 |
return; |
167 |
|
} |
168 |
|
|
169 |
0 |
String fullPath = artifactPath.getPath(); |
170 |
0 |
File repoFile = new File( fullPath ); |
171 |
|
|
172 |
0 |
if ( dep.isSystemScoped() ) |
173 |
|
{ |
174 |
0 |
handle = "module:/classpath/lib/" |
175 |
|
+ IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false ); |
176 |
0 |
} |
177 |
|
else |
178 |
|
{ |
179 |
0 |
File localRepositoryFile = new File( localRepository.getBasedir() ); |
180 |
|
|
181 |
0 |
handle = "module:/classpath/var/M2_REPO/" |
182 |
|
+ IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, repoFile, false ); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
0 |
writer.startElement( ELT_DEPENDENT_MODULE ); |
187 |
|
|
188 |
0 |
writer.addAttribute( ATTR_DEPLOY_PATH, "/WEB-INF/lib" ); |
189 |
0 |
writer.addAttribute( ATTR_HANDLE, handle ); |
190 |
|
|
191 |
0 |
writer.startElement( ELT_DEPENDENCY_TYPE ); |
192 |
0 |
writer.writeText( "uses" ); |
193 |
0 |
writer.endElement(); |
194 |
|
|
195 |
0 |
writer.endElement(); |
196 |
0 |
} |
197 |
|
|
198 |
|
protected void writeWarOrEarResources( XMLWriter writer, MavenProject project, ArtifactRepository localRepository ) |
199 |
|
throws MojoExecutionException |
200 |
|
{ |
201 |
|
|
202 |
|
|
203 |
2 |
for ( int j = 0; j < config.getDeps().length; j++ ) |
204 |
|
{ |
205 |
0 |
IdeDependency dep = config.getDeps()[j]; |
206 |
0 |
String type = dep.getType(); |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
0 |
if ( ( !dep.isTestDependency() && !dep.isProvided() ) |
211 |
|
&& ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) || "war".equals( type ) ) ) |
212 |
|
{ |
213 |
0 |
addDependency( writer, dep, localRepository, config.getProject().getBasedir() ); |
214 |
|
} |
215 |
|
} |
216 |
2 |
} |
217 |
|
|
218 |
|
protected String resolveServletVersion() |
219 |
|
{ |
220 |
1 |
String[] artifactNames = new String[] { "servlet-api", "servletapi", "geronimo-spec-servlet" }; |
221 |
|
|
222 |
1 |
String version = IdeUtils.getDependencyVersion( artifactNames, config.getProject().getArtifacts(), 3 ); |
223 |
1 |
if ( version == null ) |
224 |
|
{ |
225 |
|
|
226 |
1 |
artifactNames = new String[] { "geronimo-spec-j2ee" }; |
227 |
1 |
version = IdeUtils.getDependencyVersion( artifactNames, config.getProject().getArtifacts(), 3 ); |
228 |
1 |
if ( version != null ) |
229 |
|
{ |
230 |
0 |
String j2eeMinorVersion = StringUtils.substring( version, 2, 3 ); |
231 |
0 |
version = "2." + j2eeMinorVersion; |
232 |
|
} |
233 |
|
} |
234 |
1 |
return version == null ? "2.4" : version; |
235 |
|
} |
236 |
|
|
237 |
|
protected String resolveEjbVersion() |
238 |
|
{ |
239 |
0 |
String version = null; |
240 |
|
|
241 |
|
|
242 |
0 |
return version == null ? "2.1" : version; |
243 |
|
} |
244 |
|
|
245 |
|
protected String resolveJ2eeVersion() |
246 |
|
{ |
247 |
|
|
248 |
|
|
249 |
1 |
String version = "1.3"; |
250 |
1 |
for ( Iterator it = config.getProject().getDependencies().iterator(); it.hasNext(); ) |
251 |
|
{ |
252 |
0 |
Dependency d = (Dependency) it.next(); |
253 |
0 |
if ( "javax.j2ee".equals( d.getGroupId() ) && "j2ee".equals( d.getArtifactId() ) ) |
254 |
|
{ |
255 |
0 |
version = d.getVersion(); |
256 |
0 |
break; |
257 |
|
} |
258 |
0 |
} |
259 |
1 |
return version; |
260 |
|
} |
261 |
|
|
262 |
|
protected String resolveJavaVersion() |
263 |
|
{ |
264 |
2 |
String version = IdeUtils.getPluginSetting( config.getProject(), "maven-compiler-plugin", "target", null ); |
265 |
2 |
if ( version == null ) |
266 |
|
{ |
267 |
2 |
IdeUtils.getPluginSetting( config.getProject(), "maven-compiler-plugin", "source", null ); |
268 |
|
} |
269 |
|
|
270 |
2 |
if ( "1.5".equals( version ) || "5".equals( version ) ) |
271 |
|
{ |
272 |
0 |
version = "5.0"; |
273 |
|
} |
274 |
|
|
275 |
2 |
return version == null ? "1.4" : version; |
276 |
|
} |
277 |
|
|
278 |
|
} |