Maven插件作为目标

Maven插件作为目标

本文介绍了无法执行Groovy Maven插件作为目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Apache Maven 3.3.9 with the Groovy Maven plugin. Here is the relevant section of the pom.xml (the inlined Groovy script is just fictional):

<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 调用插件的目标,则会收到以下错误消息:

If I am calling mvn install the inline Groovy script gets called by the plugin as part of the prepare-package phase and works just fine. But if I try to call the plugins' goal directly via mvn groovy:execute I get the following error message:

推荐答案

您遇到的错误已经指出了问题:该插件找不到 source 配置选项,因为确实如此仅在 myGroovyPlugin 执行范围内进行配置,也就是说,仅在 execution 范围内进行配置,而不是全局配置.

The error you are getting is already pointing at the issue: the plugin couldn't not find the source configuration option because indeed it is only configured within the myGroovyPlugin execution, that is, only in the execution scope and not as a global configuration.

这是 executions 之外(插件所有执行的全局配置(甚至从命令行)和 execution内的)的 configuration 元素之间的主要区别(配置仅适用于该特定目标执行).

This is the main difference between configuration element outside the executions (global configuration for all executions of the plugin (even from command line) and within an execution (configuration only applied to that particular goal execution).

要解决此问题,在这种情况下,应将 configuration 元素移至 executions 部分之外,因为该插件不是在默认绑定,就足够了,不会对您的应用产生影响构建:它将在 myGroovyPlugin 执行期间以及从命令行的显式执行中继续使用.

To fix the issue you should move the configuration element outside the executions section in this case, since the plugin is not a plugin invoked during default bindings by Maven, it would be enough and not have impact on your build: it will be still used during the myGroovyPlugin execution AND from explicit executions from command line.

Maven POM参考,其中的配置执行:


为清楚起见,应将其更改为以下内容:


To make it clear, you should change it to the following:

<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>

这样,配置将成为全局配置,并应用于命令行执行和声明的 execution .

As such the configuration will become a global configuration and applied to both command line executions and declared executions.

由于您使用的是Maven 3.3.9 ,因此您还可以使用稍微冗长的模式来直接调用执行的特定配置:

Since you are using Maven 3.3.9, you could also make use of a slightly more verbose pattern to invoke directly a specific configuration of an execution:

mvn groovy:execute@myGroovyPlugin

如果您确实不想使用全局配置,因为您不想影响某个插件的其他(通常是默认)执行,并且您确实想在一个插件中同时使用特定的隔离配置,则此模式很有用从命令行执行.

This pattern is useful in cases where you really don't want a global configuration because you don't want to impact other (often default) executions of a certain plugin and you really want to use a specific isolated configuration both in an execution and from command line.

这篇关于无法执行Groovy Maven插件作为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:25