groovy java gmaven 和特征

groovy java gmaven and traits

我正在尝试编写测试并使用 groovy 特征功能。

这是我的 gmaven 插件配置

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

这是我的特点:

trait UserTrait {

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

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

这是我的测试class:

class UserToCrossIdConnectionTest extends IntegrationBaseTest implements UserTrait{}

我正在尝试使用 Maven 编译这些东西,我得到:

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 

我检查了 classes。 特征变为:

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

和 class 实现了特征:

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

很公平,我无法实现 class。 我该如何解决?

GMaven 无法编译更新版本的 Groovy。我建议转到 GMavenPlus(我刚刚针对您的示例成功测试了它)。

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

由于我是 GMavenPlus 的作者(我也维护了 GMaven 一段时间),老实说,还有 Groovy-Eclipse Compiler Plugin for Maven. I tried to help people understand their options here.