3

When I try to compile my project using groovy-eclipse compiler with following classes:

 import groovy.transform.builder.Builder

// @author: m on 10.05.16 22:15.
@Builder
class F {

   int a

}

and

public class B {

   int a;

public static void main(String[] args) {
    F.FBuilder builder = F.builder();

    F build = builder.a(1).build();
  }
}

The following error occurs:

[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.

How to fix it? Please help

RBT
  • 24,161
  • 21
  • 159
  • 240

4 Answers4

0

The Java files need to be under the groovy directory with the Groovy files if they need to be cross-compiled.

cjstehno
  • 13,468
  • 4
  • 44
  • 56
  • Moving java class to src/main/groovy and adding org.codehaus.groovy groovy-eclipse-compiler 2.9.1-01 true as described in https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin doesn't help - the same error. – Михайло Тітов May 11 '16 at 20:05
0

Try using the 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>
Renato
  • 12,940
  • 3
  • 54
  • 85
  • Thanks. Actualy I can use gmaven-plus but I want to have ability of compilation code with groovy-eclipse (or other supported) compiler specified in intellij idea. Is it possible? – Михайло Тітов May 11 '16 at 21:03
  • I use the above config in my projects and IntelliJ support is awesome. – Renato May 12 '16 at 10:15
  • Well, I mean that I want to have posibiliy of compiling class B with Build/Compile 'B.java' menu option when target folder is empty. Does it work in your case? – Михайло Тітов May 12 '16 at 17:58
  • IntelliJ can compile it if you use the SimpleStrategy in the builder (otherwise it can't see the FBuilder type)... but when building with Maven the Groovy class is not visible from Java, unfortunately... so if I were you I would stick with the groovy-eclipse compiler plugin. – Renato May 12 '16 at 20:16
  • I still can't compile @Builder(builderStrategy = SimpleStrategy) class F { int a int d } public class B { public static void main(String[] args) { F f = new F().setA(1).setD(2); } } Information:Using Groovy-Eclipse to compile Java & Groovy sources Information:12.05.16 22:35 - Compilation completed with 1 error and 0 warnings in 2s 636ms /Users/m/git_repo/mix/src/main/java/B.java Error:(7, -1) Groovy-Eclipse: Cannot invoke setD(int) on the primitive type void – Михайло Тітов May 12 '16 at 20:45
  • Add `prefix="with"` to the @Builder annotation and change your setters from `setA` to `withA`. – Renato May 12 '16 at 21:23
  • `@Builder(prefix = "with", builderStrategy = SimpleStrategy)class F { int a; int d}` it produce: `Information:Using Groovy-Eclipse to compile Java & Groovy sources Information:12.05.16 23:26 - Compilation completed with 1 error and 0 warnings in 1s 372ms /Users/m/git_repo/mix/src/main/java/B.java Error:(5, -1) Groovy-Eclipse: The method withA(int) is undefined for the type F` – Михайло Тітов May 12 '16 at 21:28
  • I added a new answer... see my project that shows an alternative way to solve this problem [here](https://github.com/renatoathaydes/java-groovy-maven-test). – Renato May 13 '16 at 06:53
0

If you do not mind splitting Java and Groovy classes into different Maven modules, I would strongly encourage you to do that.

The benefits are that you will never run into joint compilation issues, you will get a clear vision of what depends on what (to use a Groovy module from a Java module, you add a Maven dependency to it), Groovy AST will be visible from the Java classes, and you stay sane by not mixing languages within the same module.

I created a simple java-groovy-maven-test project on GitHub to show how you can do that.

Basically, you create two modules, one just containing your Java class (test-java in my project), the other containing the Groovy class (test-groovy).

Both are under the same parent Maven module.

This works much better than trying to compile both Groovy/Java within the same module.

Instruction on how to build and test the project are on the README page.

Renato
  • 12,940
  • 3
  • 54
  • 85
0

You may just want to use the def keyword in place of F.FBuilder. When using joint compilation (java and groovy mixed in the same source folder), the groovy files are mapped into the Java model of Eclipse/JDT. However this mapping happens before many AST transformations have been applied, so additional types and methods like @Builder adds are not seen by Java.

emilles
  • 1,104
  • 7
  • 14