当我尝试使用以下类使用groovy-eclipse编译器编译project时:
import groovy.transform.builder.Builder
// @author: m on 10.05.16 22:15.
@Builder
class F {
int a
}
和
public class B {
int a;
public static void main(String[] args) {
F.FBuilder builder = F.builder();
F build = builder.a(1).build();
}
}
发生以下错误:
[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/m/git_repo/mix/src/main/java/B.java:[7,1] 1. ERROR in /Users/m/git_repo/mix/src/main/java/B.java (at line 7)
F.FBuilder builder = F.builder();
^^^^^^^^^^
F.FBuilder cannot be resolved to a type
[ERROR] /Users/m/git_repo/mix/src/main/java/B.java:[7,24] 2. ERROR in /Users/m/git_repo/mix/src/main/java/B.java (at line 7)
F.FBuilder builder = F.builder();
^^^^^^^
The method builder() is undefined for the type F
[ERROR] Found 2 errors and 0 warnings.
如何解决?请帮忙
答案 0 :(得分:0)
如果需要交叉编译,则需要将Java文件放在带有Groovy文件的groovy
目录下。
答案 1 :(得分:0)
尝试使用maven-antrun-plugin:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/src/main/groovy"/>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.compile.classpath"/>
</taskdef>
<mkdir dir="${project.build.outputDirectory}"/>
<groovyc destdir="${project.build.outputDirectory}"
srcdir="${basedir}/src/main/groovy/" listfiles="true">
<classpath refid="maven.compile.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/src/test/groovy"/>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.test.classpath"/>
</taskdef>
<mkdir dir="${project.build.testOutputDirectory}"/>
<groovyc destdir="${project.build.testOutputDirectory}"
srcdir="${basedir}/src/test/groovy/" listfiles="true">
<classpath refid="maven.test.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
答案 2 :(得分:0)
如果您不介意将Java和Groovy类拆分为不同的Maven模块,我强烈建议您这样做。
好处是您永远不会遇到联合编译问题,您将清楚地了解取决于什么(使用Java模块中的Groovy模块,向其添加Maven依赖项),Groovy AST将从Java类中可以看到,并且通过不在同一模块中混合语言来保持理智。
我在GitHub上创建了一个简单的java-groovy-maven-test项目,以展示如何做到这一点。
基本上,您创建了两个模块,一个只包含您的Java类(我的项目中为test-java
),另一个包含Groovy类(test-groovy
)。
两者都在同一个父Maven模块下。
这比尝试在同一模块中编译Groovy / Java要好得多。
有关如何构建和测试项目的说明,请参见自述文件页面。
答案 3 :(得分:0)
您可能只想使用def
关键字代替F.FBuilder
。当使用联合编译(java和groovy混合在同一个源文件夹中)时,groovy文件被映射到Eclipse / JDT的Java模型中。但是,这种映射在应用了许多AST转换之前发生,因此Java不会看到其他类型和方法,如@Builder
。