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