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 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 netbeansUserdir.mkdirs();
91
92 List<File> clusters = new ArrayList<File>();
93 if ( !clusterBuildDir.exists() || clusterBuildDir.listFiles() == null )
94 {
95 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 File[] fls = clusterBuildDir.listFiles();
99 for ( int i = 0; i < fls.length; i++ )
100 {
101 if ( fls[i].isDirectory() )
102 {
103 clusters.add( fls[i] );
104 }
105 }
106 StringBuilder buff = new StringBuilder();
107 for ( File cluster : clusters )
108 {
109 buff.append( cluster.getAbsolutePath() );
110 buff.append( ":" );
111 }
112 if ( buff.lastIndexOf( ":" ) > -1 )
113 {
114 buff.deleteCharAt( buff.lastIndexOf( ":" ) );
115 }
116
117 StringReader sr =
118 new StringReader( "netbeans_extraclusters=\"" + buff.toString() + "\"\n" + "extraclusters=\""
119 + buff.toString() + "\"\n" + "extra_clusters=\"" + buff.toString() + "\"" );
120
121
122 File binDir = new File( netbeansInstallation, "bin" );
123 File[] execs = binDir.listFiles();
124 String clust = null;
125 if ( execs != null )
126 {
127 for ( File f : execs )
128 {
129 String name = f.getName();
130 if ( name.contains( "_w.exe" ) )
131 {
132 continue;
133 }
134 name = name.replaceFirst( "(64)?([.]exe)?$", "" );
135 if ( !name.contains( "." ) )
136 {
137 if ( clust == null )
138 {
139 clust = name;
140 }
141 else
142 {
143 if ( !clust.equals( name ) )
144 {
145 getLog().debug( "When examining executable names, found clashing results " + f.getName()
146 + " " + clust );
147 }
148 }
149 }
150 }
151 }
152 if ( clust == null )
153 {
154 clust = "netbeans";
155 }
156
157
158 File etc = new File( netbeansUserdir, "etc" );
159 etc.mkdirs();
160 File confFile = new File( etc, clust + ".conf" );
161 FileOutputStream conf = null;
162 try
163 {
164 conf = new FileOutputStream( confFile );
165 IOUtil.copy( sr, conf );
166 }
167 catch ( IOException ex )
168 {
169 throw new MojoExecutionException( "Error writing " + confFile, ex );
170 }
171 finally
172 {
173 IOUtil.close( conf );
174 }
175
176 boolean windows = Os.isFamily( "windows" );
177 Commandline cmdLine = new Commandline();
178 File exec;
179 if ( windows )
180 {
181 exec = new File( netbeansInstallation, "bin\\nb.exe" );
182 if ( !exec.exists() )
183 {
184
185 exec = new File( netbeansInstallation, "bin\\" + clust + ".exe" );
186 String jdkHome = System.getenv( "JAVA_HOME" );
187 if ( jdkHome != null )
188 {
189 if ( new File( jdkHome, "jre\\lib\\amd64\\jvm.cfg" ).exists() )
190 {
191 File exec64 = new File( netbeansInstallation, "bin\\" + clust + "64.exe" );
192 if ( exec64.isFile() )
193 {
194 exec = exec64;
195 }
196 }
197 }
198 cmdLine.addArguments( new String[] { "--console", "suppress" } );
199 }
200 }
201 else
202 {
203 exec = new File( netbeansInstallation, "bin/" + clust );
204 }
205 cmdLine.setExecutable( exec.getAbsolutePath() );
206
207 try
208 {
209 String[] args = new String[]
210 {
211
212 "--userdir",
213 netbeansUserdir.getAbsolutePath(),
214 "-J-Dnetbeans.logger.console=true",
215 "-J-ea",
216 };
217 cmdLine.addArguments( args );
218 getLog().info( "Additional arguments=" + additionalArguments );
219 cmdLine.addArguments( CommandLineUtils.translateCommandline( additionalArguments ) );
220 cmdLine.addArguments( CommandLineUtils.translateCommandline( getDebugAdditionalArguments() ) );
221 for ( int i = 0; i < cmdLine.getArguments().length; i++ )
222 {
223 getLog().info( " " + cmdLine.getArguments()[i] );
224 }
225 getLog().info( "Executing: " + cmdLine.toString() );
226 StreamConsumer out = new StreamConsumer()
227 {
228
229 public void consumeLine( String line )
230 {
231 getLog().info( line );
232 }
233 };
234 CommandLineUtils.executeCommandLine( cmdLine, out, out );
235
236 }
237 catch ( Exception e )
238 {
239 throw new MojoExecutionException( "Failed executing NetBeans", e );
240 }
241 }
242
243 private String getDebugAdditionalArguments()
244 {
245 if ( "true".equals( debugAdditionalArguments ) )
246 {
247 return "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005";
248 }
249 return debugAdditionalArguments;
250 }
251 }