1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.codehaus.mojo.nbm; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.FileOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.StringReader; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import org.apache.maven.plugin.AbstractMojo; |
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.plugin.MojoFailureException; |
28 | |
import org.apache.maven.plugins.annotations.Mojo; |
29 | |
import org.apache.maven.plugins.annotations.Parameter; |
30 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
31 | |
import org.codehaus.plexus.util.IOUtil; |
32 | |
import org.codehaus.plexus.util.Os; |
33 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
34 | |
import org.codehaus.plexus.util.cli.Commandline; |
35 | |
import org.codehaus.plexus.util.cli.StreamConsumer; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@Mojo(name="run-ide", aggregator=true, requiresDependencyResolution= ResolutionScope.RUNTIME ) |
45 | 0 | public class RunNetBeansMojo |
46 | |
extends AbstractMojo |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
@Parameter(required=true, defaultValue="${project.build.directory}/netbeans_clusters") |
54 | |
protected File clusterBuildDir; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
@Parameter(required=true, property="netbeans.installation") |
60 | |
protected File netbeansInstallation; |
61 | |
|
62 | |
|
63 | |
|
64 | |
@Parameter(required=true, defaultValue="${project.build.directory}/userdir", property="netbeans.userdir") |
65 | |
protected File netbeansUserdir; |
66 | |
|
67 | |
|
68 | |
|
69 | |
@Parameter(property="netbeans.run.params") |
70 | |
protected String additionalArguments; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
@Parameter(property="netbeans.run.params.debug") |
80 | |
protected String debugAdditionalArguments; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void execute() |
88 | |
throws MojoExecutionException, MojoFailureException |
89 | |
{ |
90 | 0 | netbeansUserdir.mkdirs(); |
91 | |
|
92 | 0 | List<File> clusters = new ArrayList<File>(); |
93 | 0 | if ( !clusterBuildDir.exists() || clusterBuildDir.listFiles() == null ) |
94 | |
{ |
95 | 0 | throw new MojoExecutionException( |
96 | |
"No clusters to include in execution found. Please run the nbm:cluster or nbm:cluster-app goals before this one." ); |
97 | |
} |
98 | 0 | File[] fls = clusterBuildDir.listFiles(); |
99 | 0 | for ( int i = 0; i < fls.length; i++ ) |
100 | |
{ |
101 | 0 | if ( fls[i].isDirectory() ) |
102 | |
{ |
103 | 0 | clusters.add( fls[i] ); |
104 | |
} |
105 | |
} |
106 | 0 | StringBuilder buff = new StringBuilder(); |
107 | 0 | for ( File cluster : clusters ) |
108 | |
{ |
109 | 0 | buff.append( cluster.getAbsolutePath() ); |
110 | 0 | buff.append( ":" ); |
111 | 0 | } |
112 | 0 | if ( buff.lastIndexOf( ":" ) > -1 ) |
113 | |
{ |
114 | 0 | buff.deleteCharAt( buff.lastIndexOf( ":" ) ); |
115 | |
} |
116 | |
|
117 | 0 | StringReader sr = |
118 | |
new StringReader( "netbeans_extraclusters=\"" + buff.toString() + "\"\n" + "extraclusters=\"" |
119 | |
+ buff.toString() + "\"\n" + "extra_clusters=\"" + buff.toString() + "\"" ); |
120 | |
|
121 | |
|
122 | 0 | File binDir = new File( netbeansInstallation, "bin" ); |
123 | 0 | File[] execs = binDir.listFiles(); |
124 | 0 | String clust = null; |
125 | 0 | if ( execs != null ) |
126 | |
{ |
127 | 0 | for ( File f : execs ) |
128 | |
{ |
129 | 0 | String name = f.getName(); |
130 | 0 | if ( name.contains( "_w.exe" ) ) |
131 | |
{ |
132 | 0 | continue; |
133 | |
} |
134 | 0 | name = name.replaceFirst( "(64)?([.]exe)?$", "" ); |
135 | 0 | if ( !name.contains( "." ) ) |
136 | |
{ |
137 | 0 | if ( clust == null ) |
138 | |
{ |
139 | 0 | clust = name; |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | if ( !clust.equals( name ) ) |
144 | |
{ |
145 | 0 | getLog().debug( "When examining executable names, found clashing results " + f.getName() |
146 | |
+ " " + clust ); |
147 | |
} |
148 | |
} |
149 | |
} |
150 | |
} |
151 | |
} |
152 | 0 | if ( clust == null ) |
153 | |
{ |
154 | 0 | clust = "netbeans"; |
155 | |
} |
156 | |
|
157 | |
|
158 | 0 | File etc = new File( netbeansUserdir, "etc" ); |
159 | 0 | etc.mkdirs(); |
160 | 0 | File confFile = new File( etc, clust + ".conf" ); |
161 | 0 | FileOutputStream conf = null; |
162 | |
try |
163 | |
{ |
164 | 0 | conf = new FileOutputStream( confFile ); |
165 | 0 | IOUtil.copy( sr, conf ); |
166 | |
} |
167 | 0 | catch ( IOException ex ) |
168 | |
{ |
169 | 0 | throw new MojoExecutionException( "Error writing " + confFile, ex ); |
170 | |
} |
171 | |
finally |
172 | |
{ |
173 | 0 | IOUtil.close( conf ); |
174 | 0 | } |
175 | |
|
176 | 0 | boolean windows = Os.isFamily( "windows" ); |
177 | 0 | Commandline cmdLine = new Commandline(); |
178 | |
File exec; |
179 | 0 | if ( windows ) |
180 | |
{ |
181 | 0 | exec = new File( netbeansInstallation, "bin\\nb.exe" ); |
182 | 0 | if ( !exec.exists() ) |
183 | |
{ |
184 | |
|
185 | 0 | exec = new File( netbeansInstallation, "bin\\" + clust + ".exe" ); |
186 | 0 | String jdkHome = System.getenv( "JAVA_HOME" ); |
187 | 0 | if ( jdkHome != null ) |
188 | |
{ |
189 | 0 | if ( new File( jdkHome, "jre\\lib\\amd64\\jvm.cfg" ).exists() ) |
190 | |
{ |
191 | 0 | File exec64 = new File( netbeansInstallation, "bin\\" + clust + "64.exe" ); |
192 | 0 | if ( exec64.isFile() ) |
193 | |
{ |
194 | 0 | exec = exec64; |
195 | |
} |
196 | |
} |
197 | |
} |
198 | 0 | cmdLine.addArguments( new String[] { "--console", "suppress" } ); |
199 | 0 | } |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | exec = new File( netbeansInstallation, "bin/" + clust ); |
204 | |
} |
205 | 0 | cmdLine.setExecutable( exec.getAbsolutePath() ); |
206 | |
|
207 | |
try |
208 | |
{ |
209 | 0 | String[] args = new String[] |
210 | |
{ |
211 | |
|
212 | |
"--userdir", |
213 | |
netbeansUserdir.getAbsolutePath(), |
214 | |
"-J-Dnetbeans.logger.console=true", |
215 | |
"-J-ea", |
216 | |
}; |
217 | 0 | cmdLine.addArguments( args ); |
218 | 0 | getLog().info( "Additional arguments=" + additionalArguments ); |
219 | 0 | cmdLine.addArguments( CommandLineUtils.translateCommandline( additionalArguments ) ); |
220 | 0 | cmdLine.addArguments( CommandLineUtils.translateCommandline( getDebugAdditionalArguments() ) ); |
221 | 0 | for ( int i = 0; i < cmdLine.getArguments().length; i++ ) |
222 | |
{ |
223 | 0 | getLog().info( " " + cmdLine.getArguments()[i] ); |
224 | |
} |
225 | 0 | getLog().info( "Executing: " + cmdLine.toString() ); |
226 | 0 | StreamConsumer out = new StreamConsumer() |
227 | 0 | { |
228 | |
|
229 | |
public void consumeLine( String line ) |
230 | |
{ |
231 | 0 | getLog().info( line ); |
232 | 0 | } |
233 | |
}; |
234 | 0 | CommandLineUtils.executeCommandLine( cmdLine, out, out ); |
235 | |
|
236 | |
} |
237 | 0 | catch ( Exception e ) |
238 | |
{ |
239 | 0 | throw new MojoExecutionException( "Failed executing NetBeans", e ); |
240 | 0 | } |
241 | 0 | } |
242 | |
|
243 | |
private String getDebugAdditionalArguments() |
244 | |
{ |
245 | 0 | if ( "true".equals( debugAdditionalArguments ) ) |
246 | |
{ |
247 | 0 | return "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"; |
248 | |
} |
249 | 0 | return debugAdditionalArguments; |
250 | |
} |
251 | |
} |