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.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
36
37
38
39
40 @Mojo(name="run-platform", requiresDependencyResolution= ResolutionScope.RUNTIME )
41 public class RunPlatformAppMojo
42 extends AbstractMojo
43 {
44
45
46
47
48 @Parameter(required=true, property="netbeans.branding.token")
49 protected String brandingToken;
50
51
52
53 @Parameter(required=true, defaultValue="${project.build.directory}")
54 private File outputDirectory;
55
56
57
58
59 @Parameter(required=true, defaultValue="${project.build.directory}/userdir", property="netbeans.userdir")
60 protected File netbeansUserdir;
61
62
63
64
65 @Parameter(property="netbeans.run.params")
66 protected String additionalArguments;
67
68
69
70
71
72
73
74
75 @Parameter(property="netbeans.run.params.debug")
76 protected String debugAdditionalArguments;
77
78
79
80
81
82 @Parameter(required=true, readonly=true, property="project")
83 private MavenProject project;
84
85
86
87
88
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 {
117 exec = new File( appbasedir, "bin\\" + brandingToken + ".exe" );
118
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
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 }