View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
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,
11   * software 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
14   * and limitations under the License.
15   */
16  package org.scala_tools.maven;
17  
18  import org.codehaus.plexus.util.StringUtils;
19  
20  
21  /**
22   * Run a Scala class using the Scala runtime
23   *
24   * @goal run
25   * @requiresDependencyResolution test
26   * @execute phase="test-compile"
27   */
28  public class ScalaRunMojo extends ScalaMojoSupport {
29  
30      /**
31       * @parameter expression="${launcher}"
32       */
33      protected String launcher;
34  
35      /**
36       * Additional parameter to use to call the main class
37       * Using this parameter only from command line ("-DaddArgs=arg1|arg2|arg3|..."), not from pom.xml.
38       * @parameter expression="${addArgs}"
39       */
40      protected String addArgs;
41  
42      /**
43       * A list of launcher definition (to avoid rewriting long command line or share way to call an application)
44       * launchers could be define by :
45       * <pre>
46       *   &lt;launchers>
47       *     &lt;launcher>
48       *       &lt;id>myLauncher&lt;/id>
49       *       &lt;mainClass>my.project.Main&lt;/mainClass>
50       *       &lt;args>
51       *         &lt;arg>arg1&lt;/arg>
52       *       &lt;/args>
53       *       &lt;jvmArgs>
54       *         &lt;jvmArg>-Xmx64m&lt;/jvmArg>
55       *       &lt;/jvmArgs>
56       *     &lt;/launcher>
57       *     &lt;launcher>
58       *       &lt;id>myLauncher2&lt;/id>
59       *       ...
60       *       &lt;>&lt;>
61       *     &lt;/launcher>
62       *   &lt;/launchers>
63       * </pre>
64       * @parameter
65       */
66      protected Launcher[] launchers;
67  
68      /**
69       * Main class to call, the call use the jvmArgs and args define in the pom.xml, and the addArgs define in the command line if define.
70       *
71       * Higher priority to launcher parameter)
72       * Using this parameter only from command line (-DmainClass=...), not from pom.xml.
73       * @parameter expression="${mainClass}"
74       */
75      protected String mainClass;
76  
77      @Override
78      @SuppressWarnings("unchecked")
79      protected void doExecute() throws Exception {
80          JavaCommand jcmd = null;
81          if (StringUtils.isNotEmpty(mainClass)) {
82              jcmd = new JavaCommand(this, mainClass, JavaCommand.toMultiPath(project.getTestClasspathElements()), jvmArgs, args);
83          } else if ((launchers != null) && (launchers.length > 0)) {
84              if (StringUtils.isNotEmpty(launcher)) {
85                  for(int i = 0; (i < launchers.length) && (jcmd == null); i++) {
86                      if (launcher.equals(launchers[i].id)) {
87                          getLog().info("launcher '"+ launchers[i].id + "' selected => "+ launchers[i].mainClass );
88                          jcmd = new JavaCommand(this, launchers[i].mainClass, JavaCommand.toMultiPath(project.getTestClasspathElements()), launchers[i].jvmArgs, launchers[i].args);
89                      }
90                  }
91              } else {
92                  getLog().info("launcher '"+ launchers[0].id + "' selected => "+ launchers[0].mainClass );
93                  jcmd = new JavaCommand(this, launchers[0].mainClass, JavaCommand.toMultiPath(project.getTestClasspathElements()), launchers[0].jvmArgs, launchers[0].args);
94              }
95          }
96          if (jcmd != null) {
97              if (StringUtils.isNotEmpty(addArgs)) {
98                  jcmd.addArgs(addArgs.split("|"));
99              }
100             jcmd.run(displayCmd);
101         } else {
102             getLog().warn("Not mainClass or valid launcher found/define");
103         }
104     }
105 }