View Javadoc
1   package org.codehaus.mojo.gwt.eclipse;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugin.MojoFailureException;
34  import org.apache.maven.plugins.annotations.Component;
35  import org.apache.maven.plugins.annotations.Execute;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.project.MavenProject;
40  import org.codehaus.mojo.gwt.shell.TestMojo;
41  import org.codehaus.mojo.gwt.test.TestTemplate;
42  import org.codehaus.plexus.util.WriterFactory;
43  
44  import freemarker.template.Configuration;
45  import freemarker.template.Template;
46  import freemarker.template.TemplateException;
47  
48  /**
49   * Goal which creates Eclipse lauch configurations for GWTTestCases.
50   *
51   * @version $Id$
52   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
53   * @deprecated use google eclipse plugin http://code.google.com/intl/fr-FR/eclipse/docs/users_guide.html
54   */
55  @Deprecated
56  @Mojo(name = "eclipseTest")
57  @Execute(phase = LifecyclePhase.GENERATE_TEST_RESOURCES)
58  public class EclipseTestMojo
59      extends TestMojo
60  {
61      @Component
62      private EclipseUtil eclipseUtil;
63  
64      /**
65       * The currently executed project (phase=generate-resources).
66       */
67      @Parameter(defaultValue = "${executedProject}", readonly = true)
68      private MavenProject executedProject;
69  
70      /**
71       * Location of the file.
72       */
73      @Parameter(defaultValue = "${project.build.directory}/www-test")
74      private File testOutputDirectory;
75  
76      /**
77       * {@inheritDoc}
78       *
79       * @see org.apache.maven.plugin.Mojo#execute()
80       */
81      @Override
82      public void doExecute()
83          throws MojoExecutionException, MojoFailureException
84      {
85  
86          new TestTemplate( getProject(), includes, excludes, new TestTemplate.CallBack()
87          {
88              public void doWithTest( File sourceDir, String test )
89                  throws MojoExecutionException
90              {
91                  createLaunchConfigurationForGwtTestCase( sourceDir, test );
92              }
93          } );
94      }
95  
96      /**
97       * Create an eclipse launch configuration file for the specified test
98       * @param test the GWTTestCase
99       * @param testSrc the source directory where the test lives
100      * @throws MojoExecutionException some error occured
101      */
102     private void createLaunchConfigurationForGwtTestCase( File testSrc, String test )
103         throws MojoExecutionException
104     {
105         File testFile = new File( testSrc, test );
106 
107         String fqcn = test.replace( File.separatorChar, '.' ).substring( 0, test.lastIndexOf( '.' ) );
108         File launchFile = new File( getProject().getBasedir(), fqcn + ".launch" );
109         if ( launchFile.exists() && launchFile.lastModified() > testFile.lastModified() )
110         {
111             return;
112         }
113 
114         Configuration cfg = new Configuration();
115         cfg.setClassForTemplateLoading( EclipseTestMojo.class, "" );
116 
117         Map < String, Object > context = new HashMap < String, Object > ();
118         List < String > sources = new LinkedList < String >();
119         sources.addAll( executedProject.getTestCompileSourceRoots() );
120         sources.addAll( executedProject.getCompileSourceRoots() );
121         context.put( "sources", sources );
122         context.put( "test", fqcn );
123         int basedir = getProject().getBasedir().getAbsolutePath().length();
124         context.put( "out", testOutputDirectory.getAbsolutePath().substring( basedir + 1 ) );
125         context.put( "extraJvmArgs", getExtraJvmArgs() );
126         context.put( "project", eclipseUtil.getProjectName( getProject() ) );
127         
128 
129         try
130         {
131             Collection<String> gwtDevJarPath = new ArrayList<String>();
132             for (File f : getJarFiles(GWT_DEV, false))
133             {
134                 gwtDevJarPath.add( f.getAbsolutePath().replace( '\\', '/' ) );
135             }
136             context.put( "gwtDevJarPath", gwtDevJarPath );
137 
138             Writer configWriter = WriterFactory.newXmlWriter( launchFile );
139             Template template = cfg.getTemplate( "test-launch.fm", "UTF-8" );
140             template.process( context, configWriter );
141             configWriter.flush();
142             configWriter.close();
143             getLog().info( "Write launch configuration for GWT test : " + launchFile.getAbsolutePath() );
144         }
145         catch ( IOException ioe )
146         {
147             throw new MojoExecutionException( "Unable to write launch configuration", ioe );
148         }
149         catch ( TemplateException te )
150         {
151             throw new MojoExecutionException( "Unable to merge freemarker template", te );
152         }
153     }
154 
155 }