Coverage Report - org.codehaus.mojo.nbm.RunNetBeansMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
RunNetBeansMojo
0%
0/76
0%
0/40
9.667
RunNetBeansMojo$1
0%
0/3
N/A
9.667
 
 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  0
 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  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  
         //http://www.netbeans.org/issues/show_bug.cgi?id=174819
 117  0
         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  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  
         // write XXX.conf file with cluster information...
 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  
                 // in 6.7 and onward, there's no nb.exe file.
 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  
                 //TODO --jdkhome
 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  
 }