【发布时间】:2014-05-22 11:30:12
【问题描述】:
我想为我的域对象使用 groovy 来摆脱 setter/getter 等样板代码。 但我在使用 AST 转换时遇到了问题,尤其是生成的构造函数。
这里有一些最小的复制代码:
App.java
package experiment.groovy;
public class App {
public static void main(String[] args) {
Example example = new Example("Alex");
System.out.println(example.getName());
}
}
Example.groovy
>package experiment.groovy
import groovy.transform.Canonical
@Canonical
class Example {
String name;
int id;
}
项目结构
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>experiment.groovy</groupId>
<artifactId>groovy-ast</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我也试过groovy-eclipse-compiler:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>1.7</source>
<target>1.7</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.8-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
而mvn compile 因错误而失败。
1. ERROR in C:\DEV\Groovy\src\main\java\experiment\groovy\ZApp.java (at line 5)
[ERROR] COMPILATION ERROR :
Example example = new Example("ToDelete");
[INFO] -------------------------------------------------------------
^^^^^^^^^^^^^^^^^^^^^^^
[ERROR] Found 1 error and 0 warnings.
The constructor Example(String) is undefined
所以问题是:我应该改变什么,或者,可能,不可能?
附:以后如果重要的话,我会在Example类中添加javax.persistence.Entity注解。
【问题讨论】:
-
蒂姆,我更新了帖子
-
建议将
java和groovy源都放在src/main/groovy下:/ 试过了吗? -
刚试过,不行。