Maven项目无法找到Groovy类

时间:2014-11-18 19:10:46

标签: java maven groovy

我有一个非常简单的maven项目 pom.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<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>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

我在src / main / java中有两个类:

MyClass.groovy:

class MyClass {
   public static String getAlex()
   {
      return "alex"
   }
}

和ParentClass.java:

public class ParentClass {
    public ParentClass()
    {

    }
   public String getStuff()
   {
      String alex = MyClass.getAlex();
      return "alex";
   }
}

这是我的专家细节:

Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T16:58:10-04:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.7.0_17, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"

当我去mvn clean install

我遇到了这个深奥的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project TestMaven: Compilation failure
[ERROR] /Users/afrieden/Projects/TestMaven/src/main/java/ParentClass.java:[15,23] cannot find symbol
[ERROR] symbol:   variable MyClass
[ERROR] location: class ParentClass
[ERROR] -> [Help 1]

这个错误是什么意思?为什么会这样?这应该是一个maven项目的问候世界。

1 个答案:

答案 0 :(得分:4)

你只添加了groovy作为依赖。它与添加任何其他jar无异,例如spring.jar。

你需要将groovy编译器作为maven插件添加到你的项目中,以便groovy编译器为你编译groovy类。

参见下面的示例,from mailing list.

<properties>
  <gmavenVersion>1.4</gmavenVersion>
  <gmavenProviderSelection>2.0</gmavenProviderSelection>
  <groovyVersion>2.0.0</groovyVersion>
</properties>
<dependencies>
  <dependency>
    groupId>org.codehaus.groovy</groupId>
    artifactId>groovy-all</artifactId>
    <version>${groovyVersion}</version>
  dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>${gmavenVersion}</version>
        <configuration>
          <providerSelection>${gmavenProviderSelection}</providerSelection>
          <sourceEncoding>UTF-8</sourceEncoding>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>generateStubs</goal>
              <goal>compile</goal>
              <goal>generateTestStubs</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
         <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-all</artifactId>
           <version>${groovyVersion}</version>
         </dependency>
       </dependencies>
    </plugin>
  </plugins>
</build>