Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

Executing Groovy script from Maven

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...