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.ArrayList;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org_scala_tools_maven_executions.JavaMainCaller;
25  import org_scala_tools_maven_executions.JavaMainCallerInProcess;
26  import org_scala_tools_maven_executions.MainHelper;
27  
28  /**
29   * Run the Scala console with all the classes of the projects (dependencies and builded)
30   *
31   * @goal console
32   * @requiresDependencyResolution test
33   * @inheritByDefault false
34   * @requiresDirectInvocation true
35   * @executionStrategy once-per-session
36   */
37  public class ScalaConsoleMojo extends ScalaMojoSupport {
38  
39      /**
40       * The console to run.
41       *
42       * @parameter expression="${mainConsole}" default-value="scala.tools.nsc.MainGenericRunner"
43       * @required
44       */
45      protected String mainConsole;
46  
47      /**
48       * Add the test classpath (include classes from test directory), to the console's classpath ?
49       *
50       * @parameter expression="${maven.scala.console.useTestClasspath}" default-value="true"
51       * @required
52       */
53      protected boolean useTestClasspath;
54  
55      /**
56       * Add the runtime classpath, to the console's classpath ?
57       *
58       * @parameter expression="${maven.scala.console.useRuntimeClasspath}" default-value="true"
59       * @required
60       */
61      protected boolean useRuntimeClasspath;
62  
63      /**
64       * Path of the javaRebel jar. If this option is set then the console run
65       * with <a href="http://www.zeroturnaround.com/javarebel/">javarebel</a> enabled.
66       *
67       * @parameter expression="${javarebel.jar.path}"
68       */
69      protected File javaRebelPath;
70  
71      @Override
72      @SuppressWarnings("unchecked")
73      protected void doExecute() throws Exception {
74          //TODO - Many other paths uses the getScalaCommand()!!! We should try to use that as much as possibel to help maintainability.
75          String sv = findScalaVersion().toString();
76          Set<String> classpath = new HashSet<String>();
77          addToClasspath("org.scala-lang", "scala-compiler", sv, classpath);
78          addToClasspath("org.scala-lang", "scala-library", sv, classpath);
79          addToClasspath("jline", "jline", "0.9.94", classpath);
80          classpath.addAll(project.getCompileClasspathElements());
81          if (useTestClasspath) {
82              classpath.addAll(project.getTestClasspathElements());
83          }
84          if (useRuntimeClasspath) {
85              classpath.addAll(project.getRuntimeClasspathElements());
86          }
87          String classpathStr = MainHelper.toMultiPath(classpath.toArray(new String[classpath.size()]));
88          JavaMainCaller jcmd = null;
89          List<String> list = new ArrayList<String>(args != null ? args.length + 3 : 3);
90          if(args != null) {
91              for(String arg : args) {
92                  list.add(arg);
93              }
94          }
95          list.add("-cp");
96          list.add(classpathStr);
97  
98          if(fork) {
99              getLog().warn("maven-scala-plugin cannot fork scala console!!  Running in process");
100         }
101 
102         jcmd = new JavaMainCallerInProcess(this, mainConsole, classpathStr, jvmArgs, list.toArray(new String[list.size()]));
103         //We need to make sure compiler plugins are sent into the interpreter as well!
104         addCompilerPluginOptions(jcmd);
105         if (javaRebelPath != null) {
106             if (!javaRebelPath.exists()) {
107                 getLog().warn("javaRevelPath '"+javaRebelPath.getCanonicalPath()+"' not found");
108             } else {
109                 jcmd.addJvmArgs("-noverify", "-javaagent:" + javaRebelPath.getCanonicalPath());
110             }
111         }
112         jcmd.run(displayCmd);
113     }
114 }