javamavengroovygmaven-plugin

groovy java gmaven and traits


I'm trying to write test and use groovy trait feature.

Here is my gmaven plugin configuration

<plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <debug>false</debug>
                    <verbose>true</verbose>
                    <stacktrace>true</stacktrace>
                    <providerSelection>2.0</providerSelection>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>generateStubs</goal>
                            <goal>testCompile</goal>
                            <goal>generateTestStubs</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.3.0</version>
                    </dependency>
                </dependencies>
            </plugin>

Here is my trait:

trait UserTrait {

    String generateCrossId(){
        System.currentTimeMillis().toString()
    }

    String generateOuterKey(){
        (System.currentTimeMillis() / new Random().nextInt(1000)) as String
    }
}

Here is my test class:

class UserToCrossIdConnectionTest extends IntegrationBaseTest implements UserTrait{}

I'm trying to compile this stuff using maven and i get:

INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 21 source files to /project/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /project/target/generated-sources/groovy-stubs/test/ru/mycode/UserControllerTest.java:[12,33] interface expected here
[ERROR] /project/target/generated-sources/groovy-stubs/test/ru/mycode/UserToCrossIdConnectionTest.java:[12,33] interface expected here
[INFO] 2 errors 

I've checked the classes. Trait became:

@groovy.transform.Trait() public class  UserTrait
  extends java.lang.Object  implements
    groovy.lang.GroovyObject {}

and class which implements trait:

public class  UserToCrossIdConnectionTest
  extends IntegrationBaseTest  implements
    ru.mycode.UserTrait {

That's fair, I can't implement class. How can i fix it?


Solution

  • GMaven isn't able to compile newer versions of Groovy. I suggest moving to GMavenPlus (I just successfully tested it against your example).

    <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <goals>
              <goal>addSources</goal>
              <goal>addTestSources</goal>
              <goal>generateStubs</goal>
              <goal>compile</goal>
              <goal>testGenerateStubs</goal>
              <goal>testCompile</goal>
              <goal>removeStubs</goal>
              <goal>removeTestStubs</goal>
            </goals>
          </execution>
        </executions>
    </plugin>
    

    Since I'm the author of GMavenPlus (I also maintained GMaven for a while), to be honest about all your options, there's also Groovy-Eclipse Compiler Plugin for Maven. I tried to help people understand their options here.