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.rad;
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
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.eclipse.Constants;
29 import org.apache.maven.plugin.eclipse.Messages;
30 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
31 import org.apache.maven.plugin.ide.IdeDependency;
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 public class RadWebSettingsWriter
44 extends AbstractEclipseWriter
45 {
46
47 private static final String COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE =
48 "com.ibm.etools.siteedit.wizards.projectfeature.WebSiteFeature";
49
50 private static final String WEBSETTINGS_CONTEXT_ROOT = "context-root";
51
52 private static final String WEBSETTINGS_FEATURE = "feature";
53
54 private static final String WEBSETTINGS_FEATURE_ID = "feature-id";
55
56 private static final String WEBSETTINGS_FEATURES = "features";
57
58 private static final String WEBSETTINGS_FILENAME = ".websettings";
59
60 private static final String WEBSETTINGS_JSP_LEVEL = "jsp-level";
61
62 private static final String WEBSETTINGS_PROJECT_TYPE = "project-type";
63
64 private static final String WEBSETTINGS_TEMPLATEFEATURE = "templatefeature";
65
66 private static final String WEBSETTINGS_VERSION = "version";
67
68 private static final String WEBSETTINGS_WEBCONTENT = "webcontent";
69
70 private static final String WEBSETTINGS_WEBSETTINGS = "websettings";
71
72 private static final String WEBSETTINGS_LIBMODULES = "lib-modules";
73
74 private static final String WEBSETTINGS_LIBMODULE = "lib-module";
75
76 private static final String WEBSETTINGS_LM_JAR = "jar";
77
78 private static final String WEBSETTINGS_LM_PROJECT = "project";
79
80
81
82
83 private String warContextRoot;
84
85
86
87
88
89
90 public RadWebSettingsWriter( String warContextRoot )
91 {
92 this.warContextRoot = warContextRoot;
93 }
94
95
96
97
98
99
100 public void write()
101 throws MojoExecutionException
102 {
103 Writer w;
104 if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( config.getPackaging() ) )
105 {
106 try
107 {
108 w =
109 new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
110 WEBSETTINGS_FILENAME ) ), "UTF-8" );
111 }
112 catch ( IOException ex )
113 {
114 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
115 }
116
117 XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
118 writeModuleTypeFacetCore( writer );
119 IOUtil.close( w );
120 }
121 }
122
123
124
125
126
127
128
129 private void writeModuleTypeFacetCore( XMLWriter writer )
130 throws MojoExecutionException
131 {
132 writer.startElement( WEBSETTINGS_WEBSETTINGS );
133 writer.addAttribute( WEBSETTINGS_VERSION, "600" );
134 writer.startElement( WEBSETTINGS_WEBCONTENT );
135
136
137 File warSourceDirectory =
138 new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
139 "warSourceDirectory",
140 config.getProject().getBasedir() + "/src/main/webapp" ) );
141 String webContentDir =
142 IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
143
144 writer.writeText( webContentDir );
145
146 writer.endElement();
147 writer.startElement( WEBSETTINGS_CONTEXT_ROOT );
148 writer.writeText( getContextRoot( warContextRoot ) );
149 writer.endElement();
150 writer.startElement( WEBSETTINGS_PROJECT_TYPE );
151 writer.writeText( "J2EE" );
152 writer.endElement();
153 writer.startElement( WEBSETTINGS_JSP_LEVEL );
154 String jspVersion;
155 if ( config.getJeeVersion() != null )
156 {
157 jspVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJspVersion();
158 }
159 else
160 {
161 jspVersion = JeeUtils.resolveJspVersion( config.getProject() );
162 }
163 writer.writeText( jspVersion );
164 writer.endElement();
165 writer.startElement( WEBSETTINGS_FEATURES );
166 writer.startElement( WEBSETTINGS_FEATURE );
167 writer.startElement( WEBSETTINGS_FEATURE_ID );
168 writer.writeText( WEBSETTINGS_TEMPLATEFEATURE );
169 writer.endElement();
170 writer.endElement();
171 writer.startElement( WEBSETTINGS_FEATURE );
172 writer.startElement( WEBSETTINGS_FEATURE_ID );
173 writer.writeText( COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE );
174 writer.endElement();
175 writer.endElement();
176 writer.endElement();
177
178
179 writer.startElement( WEBSETTINGS_LIBMODULES );
180
181
182 IdeDependency[] deps = config.getDeps();
183 if ( deps != null )
184 {
185 for (final IdeDependency dependency : deps) {
186 log.debug("RadWebSettingsWriter: checking dependency " + dependency.toString());
187
188 if (dependency.isReferencedProject() && !dependency.isTestDependency() && !dependency.isProvided()) {
189 log.debug("RadWebSettingsWriter: dependency " + dependency.toString()
190 + " selected for inclusion as lib-module");
191
192 String depName = dependency.getEclipseProjectName();
193 String depJar = dependency.getArtifactId() + ".jar";
194
195 writer.startElement(WEBSETTINGS_LIBMODULE);
196
197 writer.startElement(WEBSETTINGS_LM_JAR);
198 writer.writeText(depJar);
199 writer.endElement();
200
201 writer.startElement(WEBSETTINGS_LM_PROJECT);
202 writer.writeText(depName);
203 writer.endElement();
204
205 writer.endElement();
206 }
207 }
208 }
209
210 writer.endElement();
211 writer.endElement();
212
213 }
214
215
216
217
218
219
220
221 private String getContextRoot( String warContextRoot )
222 {
223 if ( warContextRoot == null || warContextRoot.length() == 0 )
224 {
225 return config.getProject().getArtifactId();
226 }
227 else
228 {
229 return warContextRoot;
230 }
231 }
232
233 }