Search code examples
javamavengroovygmaven-pluginmaven-mojo

Call groovy 'main' method from Maven


I want to call the 'main' method in a groovy class in the 'package' phase from maven.

I tried the gmaven plugin

<plugin>
        <groupId>org.codehaus.groovy.maven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>execute</goal>
                </goals>
               <configuration>
          <source>src/main/groovy/CreateDeps.groovy</source>
        </configuration>
            </execution>
        </executions>

    </plugin>

But I'm getting a 'MissingPropertyException'

[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute 

(default) on project Versions: groovy.lang.MissingPropertyException: 

No such property: groovy for class: CreateDeps -> [Help 1]

Groovy file looks like

CreateDeps.groovy

class CreateDeps {

    static main(args) {
    println "**************************I'm in groovy";
    }

}

I'm able to call a java main method using mojo . I want to achieve the same result using groovy.

 <plugin>  
     <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
     <executions>  
      <execution>  
       <phase>package</phase>  
       <goals>  
        <goal>java</goal>  
       </goals>  
       <configuration>  
        <mainClass>CreateDeps</mainClass>  
        <arguments>  
         <argument>arg1</argument>  
          <argument>arg2</argument>  
        </arguments>  
       </configuration>  
      </execution>  
     </executions>  
    </plugin>   
<plugin>

Thanks in advance


Solution

  • The source configuration parameter for the gmaven plugin expects groovy source code, not a class name. If you want to execute a script, use the sourcepath configuration parameter with the path to the source, not the class name. i.e. <sourcepath>src/main/groovy/CreateDeps.groovy</sourcepath>