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.List;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.Messages;
31  import org.apache.maven.plugin.logging.Log;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.StringUtils;
34  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35  import org.codehaus.plexus.util.xml.XMLWriter;
36  
37  /**
38   * Base class for writing external launch configuration files.
39   * 
40   * @author <a href="mailto:kenneyw@neonics.com">Kenney Westerhof</a>
41   */
42  public abstract class EclipseLaunchConfigurationWriter
43      extends AbstractEclipseWriter
44  {
45      public static final String FILE_DOT_EXTERNAL_TOOL_BUILDERS = ".externalToolBuilders/";
46  
47      private String filename;
48  
49      private boolean initialized;
50  
51      /**
52       * Filename including .launch
53       * 
54       * @param filename
55       */
56      protected EclipseWriter init( Log log, EclipseWriterConfig config, String filename )
57      {
58          this.filename = filename;
59          initialized = true;
60          return super.init( log, config );
61      }
62  
63      public void write()
64          throws MojoExecutionException
65      {
66          if ( !initialized )
67          {
68              throw new MojoExecutionException( "Not initialized" );
69          }
70  
71          Writer w;
72  
73          try
74          {
75              File extToolsDir = new File( config.getEclipseProjectDirectory(), FILE_DOT_EXTERNAL_TOOL_BUILDERS );
76              if ( !extToolsDir.exists() && !extToolsDir.mkdir() )
77              {
78                  throw new MojoExecutionException( "Error creating directory " + extToolsDir );
79              }
80              w = new OutputStreamWriter( new FileOutputStream( new File( extToolsDir, filename ) ), "UTF-8" );
81          }
82          catch ( IOException ex )
83          {
84              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
85          }
86  
87          XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
88  
89          writer.startElement( "launchConfiguration" );
90          writer.addAttribute( "type", getLaunchConfigurationType() );
91  
92          writeAttribute( writer, "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", isLaunchInBackground() );
93  
94          writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS",
95                          StringUtils.join( getRunBuildKinds(), "," ) );
96  
97          // i think this one means if the ATTR_RUN_BUILD_KINDS is not default.
98          writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED", true );
99  
100         writeAttribute( writer, "org.eclipse.debug.core.appendEnvironmentVariables", isAppendEnvironmentVariables() );
101 
102         writeAttribute( writer, "org.eclipse.jdt.launching.PROJECT_ATTR", config.getEclipseProjectName() );
103 
104         writeAttribute( writer, "org.eclipse.jdt.launching.DEFAULT_CLASSPATH", true );
105 
106         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_LOCATION", getBuilderLocation() );
107 
108         if ( getWorkingDirectory() != null )
109         {
110             writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY", getWorkingDirectory() );
111         }
112 
113         if ( getRefreshScope() != null )
114         {
115             writeAttribute( writer, "org.eclipse.debug.core.ATTR_REFRESH_SCOPE", getRefreshScope() );
116         }
117 
118         writeAttribute( writer, "org.eclipse.debug.core.capture_output", isCaptureOutput() );
119 
120         String workingSet =
121             "<?xml version='1.0'?>"
122                 + "<launchConfigurationWorkingSet editPageId='org.eclipse.ui.resourceWorkingSetPage'"
123                 + " factoryID='org.eclipse.ui.internal.WorkingSetFactory'" + " label='workingSet'"
124                 + " name='workingSet'>";
125 
126         for (Object o : getMonitoredResources()) {
127             MonitoredResource monitoredResource = (MonitoredResource) o;
128 
129             workingSet += monitoredResource.print();
130         }
131 
132         workingSet += "</launchConfigurationWorkingSet>";
133 
134         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE", "${working_set:" + workingSet + "}" );
135 
136         addAttributes( writer );
137 
138         writer.endElement();
139 
140         IOUtil.close( w );
141     }
142 
143     protected List getMonitoredResources()
144     {
145         return Collections.singletonList( new MonitoredResource( config.getEclipseProjectName(),
146                                                                  MonitoredResource.PROJECT ) );
147     }
148 
149     protected abstract void addAttributes( XMLWriter writer );
150 
151     /**
152      * Wheter to allocate a console.
153      */
154     private boolean isCaptureOutput()
155     {
156         return false;
157     }
158 
159     private String getWorkingDirectory()
160     {
161         return "${build_project}";
162     }
163 
164     protected String getRefreshScope()
165     {
166         return "${project}";
167     }
168 
169     protected abstract String getBuilderLocation();
170 
171     protected String[] getRunBuildKinds()
172     {
173         return new String[] { "full", "incremental", "auto", "clean" };
174     }
175 
176     protected boolean isAppendEnvironmentVariables()
177     {
178         return true;
179     }
180 
181     protected boolean isLaunchInBackground()
182     {
183         return false;
184     }
185 
186     protected abstract String getLaunchConfigurationType();
187 
188     protected static void writeAttribute( XMLWriter writer, String key, String value )
189     {
190         writer.startElement( "stringAttribute" );
191         writer.addAttribute( "key", key );
192         writer.addAttribute( "value", value );
193         writer.endElement();
194     }
195 
196     protected static void writeAttribute( XMLWriter writer, String key, boolean value )
197     {
198         writer.startElement( "booleanAttribute" );
199         writer.addAttribute( "key", key );
200         writer.addAttribute( "value", "" + value );
201         writer.endElement();
202     }
203 
204     protected static void writeAttribute( XMLWriter writer, String key, String[] values )
205     {
206         writer.startElement( "listAttribute" );
207         writer.addAttribute( "key", key );
208 
209         for (String value : values) {
210             writer.startElement("listEntry");
211             writer.addAttribute("value", value);
212             writer.endElement();
213         }
214 
215         writer.endElement();
216     }
217 
218 }