我想使用groovy为我的域对象摆脱setter / getters的样板代码等等。 但是我在使用AST转换方面存在问题,特别是对于生成的构造函数。
这里有一些最低限度的复制代码:
package experiment.groovy;
public class App {
public static void main(String[] args) {
Example example = new Example("Alex");
System.out.println(example.getName());
}
}
package experiment.groovy
import groovy.transform.Canonical
@Canonical
class Example {
String name;
int id;
}
http://i60.tinypic.com/2eebx1e.gif
<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
所以问题是:我应该改变什么,或者,可能是,这是不可能的?
P.S。将来,如果它很重要,我会在Example类中添加javax.persistence.Entity
注释。
答案 0 :(得分:1)
我会在Groovy用户mailing list上询问此问题。我认为问题是生成的Java存根包含'id'和'name'的getter和setter,但不包括AST的构造函数。我不确定这是一个错误还是一个已知的限制。但我认为这样的事情应该有效,所以我更倾向于前者。
编辑:这显然不适用于Maven的Groovy-Eclipse插件,因为它不使用存根。我不太了解它可能会导致这种情况。
编辑2:我无法理解我的好奇心。我已经在邮件列表中继续asked了。
编辑3:见Guillaume的回答here。基本上,Java存根是在应用AST转换之前生成的,因此Java无法看到@Canonical添加的构造函数。您的解决方案有效,因为它将Java绑定到类文件,而不是存根。另一种方法是使用Groovy-Eclipse Compiler Plugin for Maven。
答案 1 :(得分:0)
我弄明白的唯一方法是:将groovy和java分成单独的maven模块,java模块将依赖于groovy模块。
项目的结构如下:
<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>
<modules>
<module>Groovy</module>
<module>Java</module>
</modules>
<packaging>pom</packaging>
<distributionManagement>
<snapshotRepository>
<id>repoId</id>
<url><!-- Url of your snapshot repository --></url>
</snapshotRepository>
</distributionManagement>
</project>
<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">
<parent>
<artifactId>groovy-ast</artifactId>
<groupId>experiment.groovy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>groovy-code</artifactId>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<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>
<!--
This plugin is important, because without
<extensions>true</extensions> no files will be compiled.
See http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven
-->
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
<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">
<parent>
<artifactId>groovy-ast</artifactId>
<groupId>experiment.groovy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>java-code</artifactId>
<dependencies>
<dependency>
<groupId>experiment.groovy</groupId>
<artifactId>groovy-code</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果你的groovy代码依赖于某些Java代码,那么为了避免循环依赖,你应该提取将在Java和Groovy代码中使用的接口来分离模块。
点击此处获取更多信息:http://groovy.codehaus.org/Mixed+Java+and+Groovy+Applications
<强>更新强> 要从单独的工件中轻松使用实体,您可以添加maven-dependency-plugin来解压缩特定的依赖项:http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html
答案 2 :(得分:-1)
我遇到了同样的问题,这是我的解决方案:
邮箱数据服务器(父项目)
后期服务器(子项目)
<properties>
<groovy-all.version>2.4.14</groovy-all.version>
...
<!--for <build>-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<groovy-eclipse-compiler.version>2.9.2-04</groovy-eclipse-compiler.version>
<groovy-eclipse-batch.version>2.4.14-01</groovy-eclipse-batch.version>
...
</properties>
请注意两个属性中的 2.4.14 ,它是Groovy版本,而 两者都必须相同!
<dependencyManagement>
部分:
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement>
<build>
部分:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<verbose>true</verbose>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy-eclipse-compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>${groovy-eclipse-batch.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
作为补充,如果您要使用最新版本的org.codehaus库,则必须在pom.xml中添加其存储库:
<pluginRepositories>
<pluginRepository>
<id>bintray</id>
<name>Groovy Bintray</name>
<url>https://dl.bintray.com/groovy/maven</url>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
部分:
<dependencies>
...
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
...
</dependencies>
<build>
部分:
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
</configuration>
</plugin>
...
</build>