View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.writers;
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.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.eclipse.Messages;
32  import org.apache.maven.plugin.logging.Log;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
36  import org.codehaus.plexus.util.xml.XMLWriter;
37  
38  /**
39   * Base class for writing external launch configuration files.
40   * 
41   * @author <a href="mailto:kenneyw@neonics.com">Kenney Westerhof</a>
42   */
43  public abstract class EclipseLaunchConfigurationWriter
44      extends AbstractEclipseWriter
45  {
46      public static final String FILE_DOT_EXTERNAL_TOOL_BUILDERS = ".externalToolBuilders/";
47  
48      private String filename;
49  
50      private boolean initialized;
51  
52      /**
53       * Filename including .launch
54       * 
55       * @param filename
56       */
57      protected EclipseWriter init( Log log, EclipseWriterConfig config, String filename )
58      {
59          this.filename = filename;
60          initialized = true;
61          return super.init( log, config );
62      }
63  
64      public void write()
65          throws MojoExecutionException
66      {
67          if ( !initialized )
68          {
69              throw new MojoExecutionException( "Not initialized" );
70          }
71  
72          Writer w;
73  
74          try
75          {
76              File extToolsDir = new File( config.getEclipseProjectDirectory(), FILE_DOT_EXTERNAL_TOOL_BUILDERS );
77              if ( !extToolsDir.exists() && !extToolsDir.mkdir() )
78              {
79                  throw new MojoExecutionException( "Error creating directory " + extToolsDir );
80              }
81              w = new OutputStreamWriter( new FileOutputStream( new File( extToolsDir, filename ) ), "UTF-8" );
82          }
83          catch ( IOException ex )
84          {
85              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
86          }
87  
88          XMLWriter writer = new PrettyPrintXMLWriter( w );
89  
90          writer.startElement( "launchConfiguration" );
91          writer.addAttribute( "type", getLaunchConfigurationType() );
92  
93          writeAttribute( writer, "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", isLaunchInBackground() );
94  
95          writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS",
96                          StringUtils.join( getRunBuildKinds(), "," ) );
97  
98          // i think this one means if the ATTR_RUN_BUILD_KINDS is not default.
99          writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED", true );
100 
101         writeAttribute( writer, "org.eclipse.debug.core.appendEnvironmentVariables", isAppendEnvironmentVariables() );
102 
103         writeAttribute( writer, "org.eclipse.jdt.launching.PROJECT_ATTR", config.getEclipseProjectName() );
104 
105         writeAttribute( writer, "org.eclipse.jdt.launching.DEFAULT_CLASSPATH", true );
106 
107         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_LOCATION", getBuilderLocation() );
108 
109         if ( getWorkingDirectory() != null )
110         {
111             writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY", getWorkingDirectory() );
112         }
113 
114         if ( getRefreshScope() != null )
115         {
116             writeAttribute( writer, "org.eclipse.debug.core.ATTR_REFRESH_SCOPE", getRefreshScope() );
117         }
118 
119         writeAttribute( writer, "org.eclipse.debug.core.capture_output", isCaptureOutput() );
120 
121         String workingSet =
122             "<?xml version='1.0'?>"
123                 + "<launchConfigurationWorkingSet editPageId='org.eclipse.ui.resourceWorkingSetPage'"
124                 + " factoryID='org.eclipse.ui.internal.WorkingSetFactory'" + " label='workingSet'"
125                 + " name='workingSet'>";
126 
127         for ( Iterator it = getMonitoredResources().iterator(); it.hasNext(); )
128         {
129             MonitoredResource monitoredResource = (MonitoredResource) it.next();
130 
131             workingSet += monitoredResource.print();
132         }
133 
134         workingSet += "</launchConfigurationWorkingSet>";
135 
136         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE", "${working_set:" + workingSet + "}" );
137 
138         addAttributes( writer );
139 
140         writer.endElement();
141 
142         IOUtil.close( w );
143     }
144 
145     protected List getMonitoredResources()
146     {
147         return Collections.singletonList( new MonitoredResource( config.getEclipseProjectName(),
148                                                                  MonitoredResource.PROJECT ) );
149     }
150 
151     protected abstract void addAttributes( XMLWriter writer );
152 
153     /**
154      * Wheter to allocate a console.
155      */
156     private boolean isCaptureOutput()
157     {
158         return false;
159     }
160 
161     private String getWorkingDirectory()
162     {
163         return "${build_project}";
164     }
165 
166     protected String getRefreshScope()
167     {
168         return "${project}";
169     }
170 
171     protected abstract String getBuilderLocation();
172 
173     protected String[] getRunBuildKinds()
174     {
175         return new String[] { "full", "incremental", "auto", "clean" };
176     }
177 
178     protected boolean isAppendEnvironmentVariables()
179     {
180         return true;
181     }
182 
183     protected boolean isLaunchInBackground()
184     {
185         return false;
186     }
187 
188     protected abstract String getLaunchConfigurationType();
189 
190     protected static void writeAttribute( XMLWriter writer, String key, String value )
191     {
192         writer.startElement( "stringAttribute" );
193         writer.addAttribute( "key", key );
194         writer.addAttribute( "value", value );
195         writer.endElement();
196     }
197 
198     protected static void writeAttribute( XMLWriter writer, String key, boolean value )
199     {
200         writer.startElement( "booleanAttribute" );
201         writer.addAttribute( "key", key );
202         writer.addAttribute( "value", "" + value );
203         writer.endElement();
204     }
205 
206     protected static void writeAttribute( XMLWriter writer, String key, String[] values )
207     {
208         writer.startElement( "listAttribute" );
209         writer.addAttribute( "key", key );
210 
211         for ( int i = 0; i < values.length; i++ )
212         {
213             String value = values[i];
214             writer.startElement( "listEntry" );
215             writer.addAttribute( "value", value );
216             writer.endElement();
217         }
218 
219         writer.endElement();
220     }
221 
222 }