无法将 Groovy Maven 插件作为目标执行

Cannot execute Groovy Maven Plugin as a goal

我正在使用带有 Groovy Maven 插件的 Apache Maven 3.3.9。这是 pom.xml 的相关部分(内联 Groovy 脚本只是虚构的):

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
      <execution>
        <id>myGroovyPlugin</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>
    log.info('Test message: {}', 'Hello, World!')
          </source>
        </configuration>
      </execution>
    </executions>
</plugin>

如果我调用 mvn install 内联 Groovy 脚本作为准备包阶段的一部分被插件调用并且工作正常。但是,如果我尝试通过 mvn groovy:execute 直接调用插件的目标,我会收到以下错误消息:

[ERROR] Failed to execute goal org.codehaus.gmaven:groovy-maven-plugin:2.0:execute (default-cli) on project exercise02: The parameters 'source' for goal org.codehaus.gmaven:groovy-maven-plugin:2.0:execute are missing or invalid -> [Help 1]

您得到的错误已经指出了问题所在:插件无法找到 source 配置选项,因为它确实只在 myGroovyPlugin 执行中配置,也就是说,仅在 execution 范围内而不作为全局配置。

这是 executions 之外的 configuration 元素(插件的所有执行的全局配置(甚至来自命令行)和 execution 内的元素(仅配置应用于该特定目标执行)。

要解决这个问题,在这种情况下,您应该将 configuration 元素移到 executions 部分之外,因为插件不是 Maven 在 default bindings 期间调用的插件,它会足够并且不会影响您的构建:它仍将在 myGroovyPlugin 执行期间以及从命令行显式执行时使用。

来自Maven POM referenceexecution中的configuration

confines the configuration to this specific list of goals, rather than all goals under the plugin.


为了说清楚,应该改成下面这样:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
      <execution>
        <id>myGroovyPlugin</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>execute</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <source>log.info('Test message: {}', 'Hello, World!')</source>
    </configuration>        
</plugin>

因此 configuration 将成为全局配置并应用于命令行执行和声明 executions


由于您使用的是 Maven 3.3.9,您还可以使用稍微更详细的模式 invoke directly a specific configuration of an execution:

mvn groovy:execute@myGroovyPlugin

这种模式在您真的不想要全局配置的情况下很有用,因为您不想影响某个插件的其他(通常是默认的)执行,并且您真的想同时使用特定的隔离配置在执行和命令行中。