我正在尝试使用Gradle使JavaCC插件正常工作。该插件正确生成.java
个文件,但随后在compileJavaC
任务期间,它会因cannot find symbol
个错误而崩溃并烧毁。我的JavaCC .jj
文件包含引用其他未生成的java文件的代码,我猜测compileJava
任务尝试编译生成的代码而不提供对非生成代码的引用。
这是一个最小的破坏性例子。的build.gradle:
apply plugin: 'java'
//compile .jj file in src/main/javacc
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'ca.coglinc', name: 'javacc-gradle-plugin', version: '1.0.0'
}
}
apply plugin: 'javacc'
的src /主/ MyClass.java:
public class MyClass {
public MyClass(){
}
}
和src / main / javacc / MyParser.jj:
options
{}
PARSER_BEGIN(MyParser)
import java.util.*;
public class MyParser {
public static void main(String[] args) throws ParseException {
MyClass mc = new MyClass();
}
}
PARSER_END(MyParser)
SKIP:
{
" "
}
TOKEN:
{
<ANYTHING: ~[]>
}
void production():
{}
{
(<ANYTHING>)+
}
运行gradle build
会提供以下内容:
gradle build
:compileJavacc
Java Compiler Compiler Version 5.0 (Parser Generator)
(type "javacc" with no arguments for help)
Reading from file C:\Users\Nate Glenn\Desktop\java_workspace\test-gradle-javacc\
src\main\javacc\MyParser.jj . . .
File "TokenMgrError.java" does not exist. Will create one.
File "ParseException.java" does not exist. Will create one.
File "Token.java" does not exist. Will create one.
File "SimpleCharStream.java" does not exist. Will create one.
Parser generated successfully.
:compileJavaC:\Users\Nate Glenn\Desktop\java_workspace\test-gradle-javacc\build\
generated\javacc\MyParser.java:5: error: cannot find symbol
MyClass mc = new MyClass();
^
symbol: class MyClass
location: class MyParser
C:\Users\Nate Glenn\Desktop\java_workspace\test-gradle-javacc\build\generated\ja
vacc\MyParser.java:5: error: cannot find symbol
MyClass mc = new MyClass();
^
symbol: class MyClass
location: class MyParser
2 errors
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
BUILD FAILED
Total time: 9.002 secs
如何修复Gradle构建文件,以便在编译生成的文件时javaCompileC
正确包含未生成的文件?
答案 0 :(得分:3)
MyClass的源代码树不正确。它应该是src / main / java / MyClass.java。在gradle中,就像maven一样,约定是java文件默认位于src / main / java中,并且由于你没有在build.gradle文件中覆盖它,我认为这只是你的错误。该插件正确地将JavaCC输出路径添加到compileJava任务的类路径中,因此如果您在compileJava任务的正确路径中创建java类,一切都应该没问题:)
以这种方式试过你的例子并且它有效。
顺便说一下,感谢您对该插件的贡献。
答案 1 :(得分:1)
只是一些额外的FYI,如果您想覆盖编译路径,请按以下步骤操作:
HomePage