Mihai8
Mihai8

Reputation: 3147

Simplistic using Groovy in Maven

In some maven project I'll try to figure out how can be used Groovy language. For instance, I try to create a directory in validation phase, by using something like that

<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">
...
<dependencies>
   ...
   <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.5.5</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>
   ...
 </dependencies>
 <build>    
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <id>Project</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <source>
                    final FileTreeBuilder treeBuilder = new FileTreeBuilder(new File('tree'))
                    def folder = new File( 'sample_dir' )
                    if(!folder.exists()){
                        treeBuilder.dir('sample_dir')
                    }
                </source>
            </configuration>
        </plugin>
        ...
     </plugins>
</build>

But if it using the command mvn compile will be present error

[ERROR] script1.groovy: 3: unable to resolve class FileTreeBuilder

What have I missed and what should be added to make this plugin functional?

Upvotes: 1

Views: 306

Answers (1)

daggett
daggett

Reputation: 28564

https://groovy.github.io/gmaven/groovy-maven-plugin/

Customizing Groovy Version

To customize the version of Groovy the plugin will use, override the org.codehaus.groovy:groovy-all dependency on the plugin definition in the project.

For example to use Groovy 2.0.6 instead of the default:

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.0.6</version>
    </dependency>
  </dependencies>
</plugin>

Upvotes: 1

Related Questions