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; |
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 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 1 | 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 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
protected EclipseWriter init( Log log, EclipseWriterConfig config, String filename ) |
58 | |
{ |
59 | 1 | this.filename = filename; |
60 | 1 | initialized = true; |
61 | 1 | return super.init( log, config ); |
62 | |
} |
63 | |
|
64 | |
public void write() |
65 | |
throws MojoExecutionException |
66 | |
{ |
67 | 1 | if ( !initialized ) |
68 | |
{ |
69 | 0 | throw new MojoExecutionException( "Not initialized" ); |
70 | |
} |
71 | |
|
72 | |
Writer w; |
73 | |
|
74 | |
try |
75 | |
{ |
76 | 1 | File extToolsDir = new File( config.getEclipseProjectDirectory(), FILE_DOT_EXTERNAL_TOOL_BUILDERS ); |
77 | 1 | if ( !extToolsDir.exists() && !extToolsDir.mkdir() ) |
78 | |
{ |
79 | 0 | throw new MojoExecutionException( "Error creating directory " + extToolsDir ); |
80 | |
} |
81 | 1 | w = new OutputStreamWriter( new FileOutputStream( new File( extToolsDir, filename ) ), "UTF-8" ); |
82 | |
} |
83 | 0 | catch ( IOException ex ) |
84 | |
{ |
85 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); |
86 | 1 | } |
87 | |
|
88 | 1 | XMLWriter writer = new PrettyPrintXMLWriter( w ); |
89 | |
|
90 | 1 | writer.startElement( "launchConfiguration" ); |
91 | 1 | writer.addAttribute( "type", getLaunchConfigurationType() ); |
92 | |
|
93 | 1 | writeAttribute( writer, "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", isLaunchInBackground() ); |
94 | |
|
95 | 1 | writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS", |
96 | |
StringUtils.join( getRunBuildKinds(), "," ) ); |
97 | |
|
98 | |
|
99 | 1 | writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED", true ); |
100 | |
|
101 | 1 | writeAttribute( writer, "org.eclipse.debug.core.appendEnvironmentVariables", isAppendEnvironmentVariables() ); |
102 | |
|
103 | 1 | writeAttribute( writer, "org.eclipse.jdt.launching.PROJECT_ATTR", config.getEclipseProjectName() ); |
104 | |
|
105 | 1 | writeAttribute( writer, "org.eclipse.jdt.launching.DEFAULT_CLASSPATH", true ); |
106 | |
|
107 | 1 | writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_LOCATION", getBuilderLocation() ); |
108 | |
|
109 | 1 | if ( getWorkingDirectory() != null ) |
110 | |
{ |
111 | 1 | writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY", getWorkingDirectory() ); |
112 | |
} |
113 | |
|
114 | 1 | if ( getRefreshScope() != null ) |
115 | |
{ |
116 | 1 | writeAttribute( writer, "org.eclipse.debug.core.ATTR_REFRESH_SCOPE", getRefreshScope() ); |
117 | |
} |
118 | |
|
119 | 1 | writeAttribute( writer, "org.eclipse.debug.core.capture_output", isCaptureOutput() ); |
120 | |
|
121 | 1 | 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 | 1 | for ( Iterator it = getMonitoredResources().iterator(); it.hasNext(); ) |
128 | |
{ |
129 | 1 | MonitoredResource monitoredResource = (MonitoredResource) it.next(); |
130 | |
|
131 | 1 | workingSet += monitoredResource.print(); |
132 | 1 | } |
133 | |
|
134 | 1 | workingSet += "</launchConfigurationWorkingSet>"; |
135 | |
|
136 | 1 | writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE", "${working_set:" + workingSet + "}" ); |
137 | |
|
138 | 1 | addAttributes( writer ); |
139 | |
|
140 | 1 | writer.endElement(); |
141 | |
|
142 | 1 | IOUtil.close( w ); |
143 | 1 | } |
144 | |
|
145 | |
protected List getMonitoredResources() |
146 | |
{ |
147 | 1 | return Collections.singletonList( new MonitoredResource( config.getEclipseProjectName(), |
148 | |
MonitoredResource.PROJECT ) ); |
149 | |
} |
150 | |
|
151 | |
protected abstract void addAttributes( XMLWriter writer ); |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
private boolean isCaptureOutput() |
157 | |
{ |
158 | 1 | return false; |
159 | |
} |
160 | |
|
161 | |
private String getWorkingDirectory() |
162 | |
{ |
163 | 2 | return "${build_project}"; |
164 | |
} |
165 | |
|
166 | |
protected String getRefreshScope() |
167 | |
{ |
168 | 2 | return "${project}"; |
169 | |
} |
170 | |
|
171 | |
protected abstract String getBuilderLocation(); |
172 | |
|
173 | |
protected String[] getRunBuildKinds() |
174 | |
{ |
175 | 1 | return new String[] { "full", "incremental", "auto", "clean" }; |
176 | |
} |
177 | |
|
178 | |
protected boolean isAppendEnvironmentVariables() |
179 | |
{ |
180 | 1 | return true; |
181 | |
} |
182 | |
|
183 | |
protected boolean isLaunchInBackground() |
184 | |
{ |
185 | 1 | return false; |
186 | |
} |
187 | |
|
188 | |
protected abstract String getLaunchConfigurationType(); |
189 | |
|
190 | |
protected static void writeAttribute( XMLWriter writer, String key, String value ) |
191 | |
{ |
192 | 8 | writer.startElement( "stringAttribute" ); |
193 | 8 | writer.addAttribute( "key", key ); |
194 | 8 | writer.addAttribute( "value", value ); |
195 | 8 | writer.endElement(); |
196 | 8 | } |
197 | |
|
198 | |
protected static void writeAttribute( XMLWriter writer, String key, boolean value ) |
199 | |
{ |
200 | 8 | writer.startElement( "booleanAttribute" ); |
201 | 8 | writer.addAttribute( "key", key ); |
202 | 8 | writer.addAttribute( "value", "" + value ); |
203 | 8 | writer.endElement(); |
204 | 8 | } |
205 | |
|
206 | |
protected static void writeAttribute( XMLWriter writer, String key, String[] values ) |
207 | |
{ |
208 | 2 | writer.startElement( "listAttribute" ); |
209 | 2 | writer.addAttribute( "key", key ); |
210 | |
|
211 | 4 | for ( int i = 0; i < values.length; i++ ) |
212 | |
{ |
213 | 2 | String value = values[i]; |
214 | 2 | writer.startElement( "listEntry" ); |
215 | 2 | writer.addAttribute( "value", value ); |
216 | 2 | writer.endElement(); |
217 | |
} |
218 | |
|
219 | 2 | writer.endElement(); |
220 | 2 | } |
221 | |
|
222 | |
} |