【问题标题】:Compile error combining groovy and java (using maven)结合groovy和java编译错误(使用maven)
【发布时间】: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注解。

【问题讨论】:

  • 蒂姆,我更新了帖子
  • 建议将javagroovy 源都放在src/main/groovy 下:/ 试过了吗?
  • 刚试过,不行。

标签: java maven groovy


【解决方案1】:

我会向 Groovy 用户 mailing list 询问这个问题。我认为问题在于生成的 Java 存根包含“id”和“name”的 getter 和 setter,但不包括来自 AST 的构造函数。我不确定这是错误还是已知限制。但我认为这样的事情应该有效,所以我更倾向于前者。

编辑:这显然不适用于 Maven 的 Groovy-Eclipse 插件,因为它不使用存根。我对此知之甚少,无法说出可能导致这种情况的原因。

编辑 2:我无法抑制我的好奇心。我已经继续,asked 在邮件列表中。

编辑 3:见纪尧姆的回答 here。基本上,Java 存根是在应用 AST 转换之前生成的,因此 Java 无法看到 @Canonical 添加的构造函数。您的解决方案有效,因为它将 Java 绑定到类文件,而不是存根。另一种方法是使用Groovy-Eclipse Compiler Plugin for Maven

【讨论】:

    【解决方案2】:

    我想出的唯一方法:将groovy和java分成单独的maven模块,java模块将依赖groovy模块。

    项目的结构将是这样的:

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

    Groovy 模块 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">
        <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>
    

    Java 模块 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">
        <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

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,这是我的解决方案:

      在图片上你可以看到我的项目结构:

      • post-box-data-server(父项目

      • 后服务器(子项目


      post-box-data-server 的 pom.xml:

      <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 版本,两者必须相同!

      &lt;dependencyManagement&gt; 部分:

      <dependencyManagement>
          <dependencies>
              ...
           <dependency>
              <groupId>org.codehaus.groovy</groupId>
              <artifactId>groovy-all</artifactId>
              <version>${groovy-all.version}</version>
           </dependency>
               ...
         </dependencies>
      </dependencyManagement>
      

      &lt;build&gt; 部分:

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

      post-server 的 pom.xml:

      &lt;dependencies&gt; 部分:

      <dependencies>
           ...
          <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
          </dependency>
          ...
      </dependencies>
      

      &lt;build&gt; 部分:

      <build>
         ...
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
              </configuration>
          </plugin>
         ...
      </build>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-13
        • 2018-07-11
        • 1970-01-01
        • 2010-11-04
        • 2015-08-29
        • 2015-01-28
        相关资源
        最近更新 更多