Executing Groovy script from Maven

positron picture positron · Aug 15, 2013 · Viewed 8.1k times · Source

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>

Answer

dmahapatro picture dmahapatro · Aug 15, 2013

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"