View Javadoc

1   /* ==========================================================================
2    * Copyright 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.util.List;
21  import java.util.ArrayList;
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.util.Os;
30  import org.codehaus.plexus.util.cli.CommandLineUtils;
31  import org.codehaus.plexus.util.cli.Commandline;
32  import org.codehaus.plexus.util.cli.StreamConsumer;
33  
34  /**
35   * Run a branded application on top of NetBeans Platform. To be used with projects
36   * with nbm-application packaging only and the project needs to be built first.
37   * @author <a href="mailto:mkleint@codehaus.org">Milos Kleint</a>
38   *
39   */
40  @Mojo(name="run-platform", requiresDependencyResolution= ResolutionScope.RUNTIME )
41  public class RunPlatformAppMojo
42          extends AbstractMojo
43  {
44  
45      /**
46       * The branding token for the application based on NetBeans platform.
47       */
48      @Parameter(required=true, property="netbeans.branding.token")
49      protected String brandingToken;
50      /**
51       * output directory where the the NetBeans application is created.
52       */
53      @Parameter(required=true, defaultValue="${project.build.directory}")
54      private File outputDirectory;
55  
56      /**
57       * NetBeans user directory for the executed instance.
58       */
59      @Parameter(required=true, defaultValue="${project.build.directory}/userdir", property="netbeans.userdir")
60      protected File netbeansUserdir;
61      /**
62       * additional command line arguments passed to the application.
63       * can be used to debug the IDE.
64       */
65      @Parameter(property="netbeans.run.params")
66      protected String additionalArguments;
67      
68      /**
69       * Attach a debugger to the application JVM. If set to "true", the process will suspend and wait for a debugger to attach
70       * on port 5005. If set to some other string, that string will be appended to the <code>additionalArguments</code>, allowing you to configure
71       * arbitrary debug-ability options (without overwriting the other options specified through the <code>additionalArguments</code>
72       * parameter).
73       * @since 3.11
74       */
75      @Parameter(property="netbeans.run.params.debug")
76      protected String debugAdditionalArguments;
77      
78      /**
79       * The Maven Project.
80       *
81       */
82      @Parameter(required=true, readonly=true, property="project")
83      private MavenProject project;
84  
85      /**
86       *
87       * @throws org.apache.maven.plugin.MojoExecutionException 
88       * @throws org.apache.maven.plugin.MojoFailureException 
89       */
90      public void execute()
91          throws MojoExecutionException, MojoFailureException
92      {
93          if ( !"nbm-application".equals( project.getPackaging() ) )
94          {
95              throw new MojoFailureException( "The nbm:run-platform goal shall be used within a NetBeans Application project only ('nbm-application' packaging)");
96          }
97  
98          netbeansUserdir.mkdirs();
99  
100         File appbasedir = new File( outputDirectory, brandingToken );
101 
102         if ( !appbasedir.exists() )
103         {
104             throw new MojoExecutionException( "The directory that shall contain built application, doesn't exist ("
105                 + appbasedir.getAbsolutePath() + ")\n Please invoke 'mvn install' on the project first" );
106         }
107 
108         boolean windows = Os.isFamily( "windows" );
109 
110         Commandline cmdLine = new Commandline();
111         File exec;
112         if ( windows )
113         {
114             exec = new File( appbasedir, "bin" + brandingToken + "_w.exe" );
115             if ( !exec.exists() )
116             { // Was removed as of nb 6.7
117                 exec = new File( appbasedir, "bin\\" + brandingToken + ".exe" );
118                 // if jdk is 32 or 64-bit
119                 String jdkHome = System.getenv( "JAVA_HOME" );
120                 if ( jdkHome != null )
121                 {
122                     if ( new File( jdkHome, "jre\\lib\\amd64\\jvm.cfg" ).exists() )
123                     {
124                         File exec64 = new File( appbasedir, "bin\\" + brandingToken + "64.exe" );
125                         if ( exec64.isFile() )
126                         {
127                             exec = exec64;
128                         }
129                     }
130                 }
131                 cmdLine.addArguments( new String[] { "--console", "suppress" } );
132             }
133         }
134         else
135         {
136             exec = new File( appbasedir, "bin/" + brandingToken );
137         }
138 
139         cmdLine.setExecutable( exec.getAbsolutePath() );
140 
141         try
142         {
143 
144             List<String> args = new ArrayList<String>();
145             args.add( "--userdir" );
146             args.add( netbeansUserdir.getAbsolutePath() );
147             args.add( "-J-Dnetbeans.logger.console=true" );
148             args.add( "-J-ea" );
149             args.add( "--branding" );
150             args.add( brandingToken );
151 
152             // use JAVA_HOME if set
153             if ( System.getenv( "JAVA_HOME" ) != null )
154             {
155                 args.add( "--jdkhome" );
156                 args.add( System.getenv( "JAVA_HOME" ) );
157             }
158 
159             cmdLine.addArguments( args.toArray( new String[0] ) );
160             cmdLine.addArguments( CommandLineUtils.translateCommandline( additionalArguments ) );
161             cmdLine.addArguments( CommandLineUtils.translateCommandline( getDebugAdditionalArguments() ) );
162             getLog().info( "Executing: " + cmdLine.toString() );
163             StreamConsumer out = new StreamConsumer()
164             {
165 
166                 public void consumeLine( String line )
167                 {
168                     getLog().info( line );
169                 }
170             };
171             CommandLineUtils.executeCommandLine( cmdLine, out, out );
172         }
173         catch ( Exception e )
174         {
175             throw new MojoExecutionException( "Failed executing NetBeans", e );
176         }
177     }
178 
179     private String getDebugAdditionalArguments()
180     {
181        if ( "true".equals( debugAdditionalArguments ) )
182         {
183             return "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005";
184         }
185         return debugAdditionalArguments;
186     }
187 }