0

When executing Groovy script from Maven I get the following error:

[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute (default) on project /path/to/project: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: project for class: /path/to/groovy/script/Example -> [Help 1]

I searched for possible cause and solution, but still don't understand what am I doing wrong and how to fix it. My script works fine when I execute it standalone or via ANT script.

Here is the script:

class Example {
   public static void main(String[] args){
       new Example().show();
   }

   def show() {
       println 'Hello World'
  }
}

And this is how I am calling it:

<dependencies>
    <dependency>
        <groupId>org.codehaus.gmaven.runtime</groupId>
        <artifactId>gmaven-runtime-1.7</artifactId>
        <version>1.3</version>
        <exclusions>
             <exclusion>
                 <groupId>org.codehaus.groovy</groupId>
                 <artifactId>groovy-all</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.7.6</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.groovy.maven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>${pom.basedir}/path/to/script/Test.groovy</source>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Community
  • 1
  • 1
positron
  • 3,643
  • 3
  • 22
  • 26

2 Answers2

5

Add your scripts directly to Example.groovy file, as long as you are able to access the default variables, instead of making it a POGO.

The script eventually compile down to a groovy class by itself with the same name as the file name (in this case Example). I am skeptical about the whole idea of class and psvm. :-)

//Example.groovy
println 'hello world'

println "$project"
println "$session"
println "$settings"
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • dmahapatro is right. `project` is available to a script via `Binding`. It can't be accessed from class – Ori Dar Aug 15 '13 at 18:45
  • Thanks. I changed my class to a script. My original class used groovy.io.FileType for example, so I had to add dependency on groovy-all, making my POM as http://pastebin.mozilla.org/2863147, otherwise I would get error about failure to located g.i.FileType. Now when I execute Maven I get another error - http://pastebin.mozilla.org/2863150. – positron Aug 15 '13 at 18:49
  • @AlexKravets Well that looks like a different issue altogether related to maven. You can find some valuable inputs from [this](http://stackoverflow.com/questions/14736283/maven-build-error-failed-to-execute-goal-missing-a-class) and [that](http://stackoverflow.com/questions/6044026/maven-archetype-problem) question. – dmahapatro Aug 15 '13 at 22:12
  • @dmahapatro Thanks for the links. Last question, when I run groovy class via call from ANT script everything works fine. So what is the difference between calling script through ANT and Maven? – positron Aug 16 '13 at 15:12
2

For whatever reason the gmaven-plugin does not appear to like a main

if you code it like

class Example {
   def show() {
       println 'Hello World'
   }
}
new Example().show();

it will work.