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