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 java.io.File;
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  /**
23   * Run the Scala console with all the classes of the projects (dependencies and builded)
24   *
25   * @goal console
26   * @requiresDependencyResolution test
27   */
28  public class ScalaConsoleMojo extends ScalaMojoSupport {
29  
30      /**
31       * The console to run.
32       *
33       * @parameter expression="${mainConsole}" default-value="scala.tools.nsc.MainGenericRunner"
34       * @required
35       */
36      protected String mainConsole;
37  
38      /**
39       * Add the test classpath (include classes from test directory), to the console's classpath ?
40       *
41       * @parameter expression="${maven.scala.console.useTestClasspath}" default-value="true"
42       * @required
43       */
44      protected boolean useTestClasspath;
45  
46      /**
47       * Add the runtime classpath, to the console's classpath ?
48       *
49       * @parameter expression="${maven.scala.console.useRuntimeClasspath}" default-value="true"
50       * @required
51       */
52      protected boolean useRuntimeClasspath;
53  
54      /**
55       * Path of the javaRebel jar. If this option is set then the console run
56       * with <a href="http://www.zeroturnaround.com/javarebel/">javarebel</a> enabled.
57       *
58       * @parameter expression="${javarebel.jar.path}"
59       */
60      protected File javaRebelPath;
61  
62      @Override
63      @SuppressWarnings("unchecked")
64      protected void doExecute() throws Exception {
65          Set<String> classpath = new HashSet<String>();
66          addToClasspath("org.scala-lang", "scala-compiler", scalaVersion, classpath);
67          addToClasspath("org.scala-lang", "scala-library", scalaVersion, classpath);
68          classpath.addAll(project.getCompileClasspathElements());
69          if (useTestClasspath) {
70              classpath.addAll(project.getTestClasspathElements());
71          }
72          if (useRuntimeClasspath) {
73              classpath.addAll(project.getRuntimeClasspathElements());
74          }
75          String classpathStr = JavaCommand.toMultiPath(classpath.toArray(new String[classpath.size()]));
76          JavaCommand jcmd = new JavaCommand(this, mainConsole, classpathStr, jvmArgs, args);
77          if (javaRebelPath != null) {
78              if (!javaRebelPath.exists()) {
79                  getLog().warn("javaRevelPath '"+javaRebelPath.getCanonicalPath()+"' not found");
80              } else {
81                  jcmd.addJvmArgs("-noverify", "-javaagent:" + javaRebelPath.getCanonicalPath());
82              }
83          }
84          jcmd.setLogOnly(false);
85          jcmd.run(displayCmd);
86      }
87  }