Badmiral
Badmiral

Reputation: 1589

Maven project fails to find Groovy class

I have a very simple maven project 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>

I have two classes in src/main/java:

MyClass.groovy:

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

And ParentClass.java:

public class ParentClass {
    public ParentClass()
    {

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

And here are my maven details:

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"

When I go to do mvn clean install

I get hit with this esoteric error:

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

What does this error mean? Why is this happening? This should be a hello world of maven projects.

Upvotes: 2

Views: 5637

Answers (1)

Atilla Ozgur
Atilla Ozgur

Reputation: 14701

You have only added groovy as dependency. It is no different from adding any other jar, like spring.jar.

You need to add groovy compiler as maven plugin to your project so that groovy compiler will compile groovy classes for you.

See example below, taken 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>

Upvotes: 5

Related Questions