【问题标题】:groovy java gmaven and traitsgroovy java gmaven和特征
【发布时间】:2015-07-23 04:46:30
【问题描述】:

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

这是我的 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 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 

我已经检查了课程。 特征变为:

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

以及实现 trait 的类:

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

那是公平的,我不能实现类。 我该如何解决?

【问题讨论】:

    标签: java maven groovy gmaven-plugin


    【解决方案1】:

    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。我试图帮助人们了解他们的选择here

    【讨论】:

    • 嗨,从其他角度遇到了特征问题。我有一个特点是具体和抽象的方法。还有两个类,它们实现和覆盖抽象方法。我收到编译错误:java 抱怨两个类没有覆盖特征中的具体方法...我应该开始新问题吗?
    • 是的,请开始一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多