---
title: "maven常用插件--groovy-maven-plugin"
author: "Roger111"
date: "2013-11-28T11:09:59Z"
tags: []
categories: ["研发管理"]
slug: "maven-groovy-maven-plugin"
draft: false
lastmod: "2013-12-02T15:34:38Z"
id: 1981127
url: "https://rogerming.iteye.com/blog/1981127"
---

这个插件在更新到2.0之后改了名字。之前为：

```
				<groupId>org.codehaus.gmaven</groupId>
				<artifactId>gmaven-plugin</artifactId>
				<version>1.5</version>
```

 现在为：

```
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.0</version>
```

 不过只是改名字而已，用法还是相同的。

 

这个插件的主要目的是能够在maven环境下执行groovy操作，和antrun差不多也分为两种，一种是直接在插件配置里下groovy代码片段，另一种是执行groovy脚本或类。

 

## 目标：

groovy:execute

 

## 使用

 

使用1，在source内些groovy脚本

```
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.0</version>
               <configuration>
                   <source>
                       println 'i can run groovy within maven!'
                   </source>
               </configuration>
            </plugin>
```

 使用2，执行hi.groovy

```
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.0</version>
               <configuration>
                   <source>${project.basedir}/src/main/groovy/hi.groovy</source>
               </configuration>
            </plugin>
```

 

## 内置变量

project 当前maven项目的对象，包含解析的properties。 pom 同project，相当于其别名 session MavenSession对象 settings 对应着settings.xml的对象。 log SLF4J的日志实例 ant antBuilder实例，方便处理ant任务。 fail（）

A helper to throw MojoExecutionException

           

 

使用举例：

可以直接在groovy脚本中使用

```
ant.echo('i am ant!')
println project.packaging
def servers = settings.servers
for(def server :servers){
    println server.id
}
```

 

## 自定义变量

可以通过properties和defaults来自定义变量给脚本用

```
<properties>
   <target>${project.build.directory}</target>
    <repo>${settings.localRepository}</repo>
     <project>this</project>
 </properties>
 <defaults>
        <name>Roger Wang</name>
</defaults>
```

 使用的方法都一样，如：

```
<source>
       println 'Hello ' + properties['name']
</source>
```

 

 

## 类的依赖

### java的依赖

gmaven可以通过classpathScope参数指定访问maven项目的classpath的范围。

```
<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <executions>
    <execution>
      <phase>process-classes</phase>
      <goals>
          <goal>execute</goal>
      </goals>
      <configuration>
        <classpathScope>runtime</classpathScope>
        <source>
          // this script has access to the compiled classes and all depencencies of scope=runtime.
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
```

 默认为none表示没有额外的classpath。

tips：

classpathScope选项：

```
compile 
none 
provided 
runtime 
test 
```

 可以通过依赖的方式加所需要的class添加到classpath

```
<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <dependencies>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
  </dependencies>
</plugin>
```

 

## groovy的依赖

gmaven通过参数scriptpath指定的路径去查找执行groovy文件。

例如你定义了一个名为Helper.groovy的文件在${project.basedir}/src/main/groovy目录下，你可以这样配置来使用它：

```
<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <executions>
    <execution>
      <phase>generate-resources</phase>
      <goals>
          <goal>execute</goal>
      </goals>
      <configuration>
        <scriptpath>
          <path>${project.basedir}/src/main/groovy</path>
        </scriptpath>
        <source>
          import Helper
          def h = new Helper()
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
```

 

ss