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 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.Map.Entry;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.eclipse.Constants;
30 import org.apache.maven.plugin.eclipse.Messages;
31 import org.apache.maven.plugin.ide.IdeUtils;
32 import org.apache.maven.plugin.ide.JeeUtils;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35 import org.codehaus.plexus.util.xml.XMLWriter;
36
37
38
39
40
41
42
43
44 public class EclipseWtpFacetsWriter
45 extends AbstractWtpResourceWriter
46 {
47
48 private static final String FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR = "com.ibm.websphere.coexistence.ear";
49
50 private static final String FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR = "com.ibm.websphere.extended.ear";
51
52 private static final String FACET_JST_EAR = "jst.ear";
53
54 private static final String FACET_JST_UTILITY = "jst.utility";
55
56 private static final String FACET_JST_EJB = "jst.ejb";
57
58 private static final String FACET_JST_WEB = "jst.web";
59
60 private static final String FACET_JST_JAVA = "jst.java";
61
62 private static final String ATTR_VERSION = "version";
63
64 private static final String ELT_INSTALLED = "installed";
65
66 private static final String ATTR_FACET = "facet";
67
68 private static final String ELT_FIXED = "fixed";
69
70 private static final String ELT_FACETED_PROJECT = "faceted-project";
71
72
73
74
75 private static final String DIR_WTP_SETTINGS = ".settings";
76
77
78
79
80 private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml";
81
82
83
84
85 public void write()
86 throws MojoExecutionException
87 {
88
89
90 File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
91 settingsDir.mkdirs();
92
93 Writer w;
94
95 String packaging = config.getPackaging();
96
97
98 try
99 {
100 w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
101 }
102 catch ( IOException ex )
103 {
104 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
105 }
106 XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
107 writeModuleTypeFacetCore( writer, packaging );
108 IOUtil.close( w );
109 }
110
111
112
113
114
115
116
117 private void writeModuleTypeFacetCore( XMLWriter writer, String packaging )
118 {
119 writer.startElement( ELT_FACETED_PROJECT );
120 if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
121 {
122 writeFacetFixedElement( writer, FACET_JST_JAVA );
123 writeFacetFixedElement( writer, FACET_JST_WEB );
124 String servletVersion;
125 if ( config.getJeeVersion() != null )
126 {
127 servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion();
128 }
129 else
130 {
131 servletVersion = JeeUtils.resolveServletVersion( config.getProject() );
132 }
133 writeFacetInstalledElement( writer, FACET_JST_WEB, servletVersion );
134 writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) );
135 }
136 else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
137 {
138 writeFacetFixedElement( writer, FACET_JST_JAVA );
139 writeFacetFixedElement( writer, FACET_JST_EJB );
140
141 String ejbVersion;
142 if ( config.getJeeVersion() != null )
143 {
144 ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion();
145 }
146 else
147 {
148 ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() );
149 }
150 writeFacetInstalledElement( writer, FACET_JST_EJB, ejbVersion );
151 writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) );
152 }
153 else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
154 {
155 if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null )
156 {
157 writer.startElement( "runtime" );
158 writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() );
159 writer.endElement();
160
161 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR,
162 this.config.getWorkspaceConfiguration().getWebsphereVersion() );
163 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR,
164 this.config.getWorkspaceConfiguration().getWebsphereVersion() );
165
166 }
167 writeFacetFixedElement( writer, FACET_JST_EAR );
168 String jeeVersion;
169 if ( config.getJeeVersion() != null )
170 {
171 jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
172 }
173 else
174 {
175 jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() );
176 }
177 writeFacetInstalledElement( writer, FACET_JST_EAR, jeeVersion );
178
179 }
180 else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) )
181 {
182 writeFacetFixedElement( writer, FACET_JST_JAVA );
183 writeFacetFixedElement( writer, FACET_JST_UTILITY );
184 writeFacetInstalledElement( writer, FACET_JST_UTILITY, "1.0" );
185 writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) );
186
187 }
188
189 writeAdditionalProjectFacets( writer );
190
191 writer.endElement();
192 }
193
194
195
196
197
198
199
200
201 private void writeFacetFixedElement( XMLWriter writer, String facetName )
202 {
203 writer.startElement( ELT_FIXED );
204 writer.addAttribute( ATTR_FACET, facetName );
205 writer.endElement();
206 }
207
208
209
210
211
212
213
214
215
216
217 private void writeFacetInstalledElement( XMLWriter writer, String facetName, String facetVersion )
218 {
219 writer.startElement( ELT_INSTALLED );
220 writer.addAttribute( ATTR_FACET, facetName );
221 writer.addAttribute( ATTR_VERSION, facetVersion );
222 writer.endElement();
223 }
224
225
226
227
228
229
230
231 private void writeAdditionalProjectFacets( XMLWriter writer )
232 {
233 if ( config.getProjectFacets() == null )
234 {
235 return;
236 }
237
238 for (Object o : config.getProjectFacets().entrySet()) {
239 Entry facetEntry = (Entry) o;
240
241 writer.startElement(ELT_INSTALLED);
242 writer.addAttribute(ATTR_FACET, (String) facetEntry.getKey());
243 writer.addAttribute(ATTR_VERSION, (String) facetEntry.getValue());
244 writer.endElement();
245 }
246 }
247
248 }