1

我正在尝试将mustache.scala移植到scala 2.10,并使用 maven 构建它antlr3-maven-plugin而不是 sbt。到目前为止,我得到了这个,并且代码编译成功。但是在运行时,当我尝试执行一些简单的操作时new Mustach("hello {{world}}!"),我得到一个java.lang.NoClassDefFoundError.

奇怪的是,起初,我得到了Node班级的 NoClassDefFoundError 。我不明白为什么 java 类不在编译的 jar 中,但我很想测试它是否有效。所以我只编译了两个java类javac,并调用了scala REPL,如下所示:

$scala -cp path/to/compiled/java/classes:path/to/jar-with-dependencies.jar
Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.monkey.mustache._
import org.monkey.mustache._

scala> new Mustache("hello {{world}}!")
java.lang.NoClassDefFoundError: org/monkey/mustache/MustacheLexer
        at org.monkey.mustache.Mustache.<init>(Mustache.scala:21)
        at org.monkey.mustache.Mustache.<init>(Mustache.scala:32)
        at .<init>(<console>:11)
        at .<clinit>(<console>)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        ...

所以现在我在 ANTLR 输出类上得到了 NoClassDefFoundError,所以我看到不仅 java 文件编译输出没有打包在 jar 中,还有 ANTLR 生成的文件。scala 代码依赖于 java 代码以及 ANTLR 生成的代码,因此如果编译成功,则一定意味着在某些时候 java & ANTLR 代码编译成功。

附上相关的Maven输出:

$ mvn clean install
  .
  . irrelevant output omitted...
  .
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mustache_2.10 1.0.5-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.3:clean (default-clean) @ mustache_2.10 ---
[INFO] Deleting file set: /path/to/mustache.scala/target (included: [**], excluded: [])
[INFO] 
[INFO] --- antlr3-maven-plugin:1.0:antlr (default) @ mustache_2.10 ---
[INFO] Processing grammar /path/to/mustache.scala/src/main/antlr/org/monkey/mustache/Mustache.g
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mustache_2.10 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /path/to/mustache.scala/src/main/resources
[INFO] 
[INFO] --- scala-maven-plugin:3.1.3:compile (project-resources-execution) @ mustache_2.10 ---
[INFO] /path/to/mustache.scala/src/main/java:-1: info: compiling
[INFO] /path/to/mustache.scala/target/generated-sources/antlr:-1: info: compiling
[INFO] /path/to/mustache.scala/src/main/scala:-1: info: compiling
[INFO] Compiling 8 source files to /path/to/mustache.scala/target/classes at 1371141939993
[INFO] prepare-compile in 0 s
[INFO] compile in 8 s
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mustache_2.10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default) @ mustache_2.10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- scala-maven-plugin:3.1.3:compile (compile) @ mustache_2.10 ---
[INFO] /path/to/mustache.scala/src/main/java:-1: info: compiling
[INFO] /path/to/mustache.scala/target/generated-sources/antlr:-1: info: compiling
[INFO] /path/to/mustache.scala/src/main/scala:-1: info: compiling
[INFO] Compiling 8 source files to /path/to/mustache.scala/target/classes at 1371141948773
[INFO] prepare-compile in 0 s
[INFO] compile in 8 s
[INFO] 
  .
  . irrelevant output omitted...
  .
[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ mustache_2.10 ---
[INFO] Building jar: /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT.jar
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 >>>
[INFO] 
[INFO] --- antlr3-maven-plugin:1.0:antlr (default) @ mustache_2.10 ---
[INFO] No grammars processed; generated files are up to date
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 ---
[INFO] Building jar: /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT-sources.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ mustache_2.10 ---
[INFO] Installing /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT.jar to /path/to/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT.jar
[INFO] Installing /path/to/mustache.scala/pom.xml to /path/to/.m2/repository/org/monkey/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT.pom
[INFO] Installing /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT-sources.jar to /path/to/.m2/repository/org/monkey/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.516s
[INFO] Finished at: Thu Jun 13 19:46:06 IDT 2013
[INFO] Final Memory: 16M/214M
[INFO] ------------------------------------------------------------------------

最重要的是,我如何从 java 文件中获取编译后的输出,并将其与编译后的 scala 代码一起打包到 jar 中?

4

1 回答 1

0

scala 编译器读取源 *.java (在您的日志中显式),但它不会编译它们。要编译您的 *.scala,它可以使用 *.java 或 *.class。

生成到 target/generated-sources/antlr 中的类不存在于 target/classes 中。编译不是由 maven-compiler-plugin 完成的。错误使用包括:它是一个过滤器而不是添加源目录的方法。尝试删除它:您将遇到编译错误,例如

.../MustacheLexer.java:[168,17] cannot find symbol [ERROR] symbol : variable type

抱歉,我不知道/搜索如何解决它。

于 2013-06-15T11:09:26.420 回答