View Javadoc

1   /* ==========================================================================
2    * Copyright 2003-2007 Mevenide Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Run NetBeans IDE with additional custom module clusters, 
39   * to be used in conjunction with nbm:cluster.
40   * Semi-deprecated; used only for standalone modules and "suites".
41   * @author <a href="mailto:mkleint@codehaus.org">Milos Kleint</a>
42   *
43   */
44  @Mojo(name="run-ide", aggregator=true, requiresDependencyResolution= ResolutionScope.RUNTIME )
45  public class RunNetBeansMojo
46          extends AbstractMojo
47  {
48  
49      /**
50       * directory where the module(s)' NetBeans cluster(s) are located.
51       * is related to nbm:cluster goal.
52       */
53      @Parameter(required=true, defaultValue="${project.build.directory}/netbeans_clusters")
54      protected File clusterBuildDir;
55      /**
56       * directory where the the NetBeans platform/IDE installation is,
57       * denotes the root directory of NetBeans installation.
58       */
59      @Parameter(required=true, property="netbeans.installation")
60      protected File netbeansInstallation;
61      /**
62       * NetBeans user directory for the executed instance.
63       */
64      @Parameter(required=true, defaultValue="${project.build.directory}/userdir", property="netbeans.userdir")
65      protected File netbeansUserdir;
66      /**
67       * additional command line arguments. 
68       */
69      @Parameter(property="netbeans.run.params")
70      protected String additionalArguments;
71      
72      /**
73       * Attach a debugger to the application JVM. If set to "true", the process will suspend and wait for a debugger to attach
74       * on port 5005. If set to some other string, that string will be appended to the <code>additionalArguments</code>, allowing you to configure
75       * arbitrary debug-ability options (without overwriting the other options specified through the <code>additionalArguments</code>
76       * parameter).
77       * @since 3.11.1
78       */
79      @Parameter(property="netbeans.run.params.debug")
80      protected String debugAdditionalArguments;    
81  
82      /**
83       * 
84       * @throws org.apache.maven.plugin.MojoExecutionException 
85       * @throws org.apache.maven.plugin.MojoFailureException 
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         //http://www.netbeans.org/issues/show_bug.cgi?id=174819
117         StringReader sr =
118             new StringReader( "netbeans_extraclusters=\"" + buff.toString() + "\"\n" + "extraclusters=\""
119                 + buff.toString() + "\"\n" + "extra_clusters=\"" + buff.toString() + "\"" );
120 
121         //now check what the exec names are to figure the right XXX.clusters name
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         // write XXX.conf file with cluster information...
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                 // in 6.7 and onward, there's no nb.exe file.
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                 //TODO --jdkhome
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 }