簡體   English   中英

如何使用AST轉換編譯混合java groovy項目?

[英]How to compile mixed java groovy project with AST transformations?

當我嘗試使用groovy-eclipse編譯器使用以下類編譯我的項目時

 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.

怎么解決? 請幫忙

如果需要交叉編譯,那么Java文件需要在Groovy文件的groovy目錄下。

嘗試使用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>

如果您不介意將Java和Groovy類拆分為不同的Maven模塊,我強烈建議您這樣做。

好處是你永遠不會遇到聯合編譯問題,你將清楚地了解什么取決於什么(使用Java模塊中的Groovy模塊,你向它添加一個Maven依賴),Groovy AST將從Java類,你可以通過不在同一模塊中混合語言來保持理智。

我在GitHub上創建了一個簡單的java-groovy-maven-test項目,以展示如何做到這一點。

基本上,您創建了兩個模塊,一個只包含您的Java類(我的項目中的test-java ),另一個包含Groovy類( test-groovy )。

兩者都在同一個父Maven模塊下。

這比嘗試在同一模塊中編譯Groovy / Java要好得多。

有關如何構建和測試項目的說明,請參見README頁面。

您可能只想使用def關鍵字代替F.FBuilder 當使用聯合編譯(java和groovy混合在同一個源文件夾中)時,groovy文件被映射到Eclipse / JDT的Java模型中。 但是,這種映射在應用了許多AST轉換之前發生,因此Java不會看到像@Builder添加的其他類型和方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM