1 package org.codehaus.mojo.gwt.eclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.commons.io.FileUtils;
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.model.Resource;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.plugins.annotations.Component;
38 import org.apache.maven.plugins.annotations.Execute;
39 import org.apache.maven.plugins.annotations.LifecyclePhase;
40 import org.apache.maven.plugins.annotations.Mojo;
41 import org.apache.maven.plugins.annotations.Parameter;
42 import org.apache.maven.plugins.annotations.ResolutionScope;
43 import org.apache.maven.project.MavenProject;
44 import org.codehaus.mojo.gwt.AbstractGwtModuleMojo;
45 import org.codehaus.mojo.gwt.utils.GwtModuleReaderException;
46 import org.codehaus.plexus.util.StringUtils;
47 import org.codehaus.plexus.util.WriterFactory;
48
49 import freemarker.template.Configuration;
50 import freemarker.template.Template;
51 import freemarker.template.TemplateException;
52
53
54
55
56
57
58
59 @Mojo(name = "eclipse", requiresDependencyResolution = ResolutionScope.COMPILE)
60 @Execute(phase = LifecyclePhase.GENERATE_RESOURCES)
61 public class EclipseMojo
62 extends AbstractGwtModuleMojo
63 {
64 @Component
65 private EclipseUtil eclipseUtil;
66
67
68
69
70
71
72
73
74 @Parameter(property = "gwt.extraJvmArgs", defaultValue = "-Xmx512m")
75 private String extraJvmArgs;
76
77
78
79
80 @Parameter(defaultValue = "${executedProject}", readonly = true)
81 private MavenProject executedProject;
82
83
84
85
86 @Parameter(defaultValue = "${project.build.outputDirectory}", required = true, readonly = true)
87 private File buildOutputDirectory;
88
89
90
91
92 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}")
93 private File hostedWebapp;
94
95
96
97
98 @Parameter
99 private String additionalPageParameters;
100
101
102
103
104 @Parameter(defaultValue = "false", property = "gwt.noserver")
105 private boolean noserver;
106
107
108
109
110 @Parameter(defaultValue = "8080", property = "gwt.port")
111 private int port;
112
113
114
115
116
117
118 @Parameter(property = "gwt.whitelist")
119 private String whitelist;
120
121
122
123
124
125
126 @Parameter(property = "gwt.blacklist")
127 private String blacklist;
128
129
130
131
132
133
134 @Parameter(property = "gwt.bindAddress")
135 private String bindAddress;
136
137
138
139
140
141
142 @Parameter(defaultValue = "true", property = "use.google.eclipse.plugin")
143 private boolean useGoogleEclipsePlugin;
144
145
146
147
148 public void setAdditionalPageParameters( String parameters )
149 {
150
151 this.additionalPageParameters = StringUtils.replace( parameters, "&", "&" );
152 }
153
154
155
156
157
158
159 public void execute()
160 throws MojoExecutionException, MojoFailureException
161 {
162 if ( !noserver )
163 {
164
165 setupExplodedWar();
166 }
167 else
168 {
169 getLog().info( "noServer is set! Skipping exploding war file..." );
170 }
171
172 for ( String module : getModules() )
173 {
174 createLaunchConfigurationForHostedModeBrowser( module );
175 }
176 }
177
178 protected void setupExplodedWar()
179 throws MojoExecutionException
180 {
181 try
182 {
183 File classes = new File( hostedWebapp, "WEB-INF/classes" );
184 if ( !buildOutputDirectory.getAbsolutePath().equals( classes.getAbsolutePath() ) )
185 {
186 getLog().warn( "Your POM <build><outputdirectory> must match your "
187 + "hosted webapp WEB-INF/classes folder for GWT Hosted browser to see your classes." );
188 }
189
190 File lib = new File( hostedWebapp, "WEB-INF/lib" );
191 getLog().info( "create exploded Jetty webapp in " + hostedWebapp );
192 lib.mkdirs();
193
194 Collection<Artifact> artifacts = getProject().getRuntimeArtifacts();
195 for ( Artifact artifact : artifacts )
196 {
197 if ( !artifact.getFile().isDirectory() )
198 {
199 FileUtils.copyFileToDirectory( artifact.getFile(), lib );
200 }
201 else
202 {
203
204 }
205 }
206 }
207 catch ( IOException ioe )
208 {
209 throw new MojoExecutionException( "Failed to create Jetty exploded webapp", ioe );
210 }
211 }
212
213
214
215
216
217
218
219 private void createLaunchConfigurationForHostedModeBrowser( String module )
220 throws MojoExecutionException
221 {
222 try
223 {
224 File launchFile = new File( getProject().getBasedir(), readModule( module ).getPath() + ".launch" );
225 if ( launchFile.exists() )
226 {
227 getLog().info( "launch file exists " + launchFile.getName() + " skip generation " );
228 return;
229 }
230
231 Configuration cfg = new Configuration();
232 cfg.setClassForTemplateLoading( EclipseMojo.class, "" );
233
234 Map<String, Object> context = new HashMap<String, Object>();
235
236 Collection<String> sources = new LinkedList<String>( executedProject.getCompileSourceRoots() );
237 List<Resource> resources = executedProject.getResources();
238 for ( Resource resource : resources )
239 {
240 sources.add( resource.getDirectory() );
241 }
242 context.put( "sources", sources );
243 context.put( "module", module );
244 context.put( "localRepository", localRepository.getBasedir() );
245 int idx = module.lastIndexOf( '.' );
246 String page = module.substring( idx + 1 ) + ".html";
247 if ( additionalPageParameters != null )
248 {
249 page += "?" + additionalPageParameters;
250 }
251
252 context.put( "modulePath", readModule( module ).getPath() );
253 context.put( "page", page );
254 int basedir = getProject().getBasedir().getAbsolutePath().length();
255 context.put( "out", getOutputDirectory().getAbsolutePath().substring( basedir + 1 ) );
256 context.put( "war", hostedWebapp.getAbsolutePath().substring( basedir + 1 ) );
257 String args = noserver ? "-noserver -port " + port : "";
258 if ( blacklist != null )
259 {
260 args += " -blacklist " + blacklist;
261 }
262 if ( whitelist != null )
263 {
264 args += " -whitelist " + whitelist;
265 }
266 if ( bindAddress != null )
267 {
268 args += " -bindAddress " + bindAddress;
269 }
270 context.put( "additionalArguments", args );
271 context.put( "extraJvmArgs", extraJvmArgs );
272 context.put( "project", eclipseUtil.getProjectName( getProject() ) );
273
274 Collection<String> gwtDevJarPath = new ArrayList<String>();
275 for (File f : getJarFiles(GWT_DEV, false))
276 {
277 gwtDevJarPath.add( f.getAbsolutePath().replace( '\\', '/' ) );
278 }
279 context.put( "gwtDevJarPath", gwtDevJarPath );
280
281 Writer configWriter = WriterFactory.newXmlWriter( launchFile );
282 String templateName = useGoogleEclipsePlugin ? "google.fm" : "launch.fm";
283 Template template = cfg.getTemplate( templateName, "UTF-8" );
284 template.process( context, configWriter );
285 configWriter.flush();
286 configWriter.close();
287 getLog().info( "Write launch configuration for GWT module : " + launchFile.getAbsolutePath() );
288 }
289 catch ( IOException ioe )
290 {
291 throw new MojoExecutionException( "Unable to write launch configuration", ioe );
292 }
293 catch ( TemplateException te )
294 {
295 throw new MojoExecutionException( "Unable to merge freemarker template", te );
296 }
297 catch ( GwtModuleReaderException e )
298 {
299 throw new MojoExecutionException( e.getMessage(), e );
300 }
301
302 }
303
304 }