如果我 运行 来自根 pom 命令行的插件,它在遍历模块时会做什么?

If I run a plugin from the command line at the root pom, what does it do when traversing modules?

我正在尝试使用插件:http://gatling.io/docs/2.0.1/extensions/maven_plugin.html

我正在尝试 运行 在根 pom 中执行此命令:

mvn package gatling:execute

这个多模块项目只在一个子 pom 中定义了这个插件,如下所示:

    <plugins>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
                <args>
                    <arg>-deprecation</arg>
                    <arg>-feature</arg>
                    <arg>-language:postfixOps</arg>
                </args>
            </configuration>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

但是当我在根目录下运行这个命令时,它给出了这个错误:

[INFO] ------------------------------------------------------------------------
[INFO] Building root-pom 2.5.210-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- gatling-maven-plugin:2.1.7:execute (default-cli) @ root-pom ---
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at io.gatling.mojo.MainWithArgsInFile.runMain(MainWithArgsInFile.java:50)
    at io.gatling.mojo.MainWithArgsInFile.main(MainWithArgsInFile.java:33)
Caused by: java.lang.RuntimeException: Can't find the jar matching (.*scala-library.*\.jar)$
    at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching.apply(ZincCompiler.scala:88)
    at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching.apply(ZincCompiler.scala:88)
    at scala.Option.getOrElse(Option.scala:120)
    at io.gatling.compiler.ZincCompiler$.jarMatching(ZincCompiler.scala:88)
    at io.gatling.compiler.ZincCompiler$.setupZincCompiler(ZincCompiler.scala:91)
    at io.gatling.compiler.ZincCompiler$delayedInit$body.apply(ZincCompiler.scala:106)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main.apply(App.scala:71)
    at scala.App$$anonfun$main.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)
    at io.gatling.compiler.ZincCompiler$.main(ZincCompiler.scala:35)
    at io.gatling.compiler.ZincCompiler.main(ZincCompiler.scala)
    ... 6 more

这不是一个scala项目,所以这肯定与插件有关。 运行 mvn package 没有 gatling:execute 工作正常。

我对 maven 的了解还不够,无法解决这个问题,因为我真的不知道 maven 在这里试图做什么。 如果它试图 运行 根目录中的 gatling,为什么? 该插件未在任何地方的根 pom 中定义。我的问题是:

  1. 当你运行一个多模块项目根目录下的插件时,它如何遍历我的项目,寻找插件并运行它们?我抽象地问这个问题。您不必用加特林机来回答。我只是想更好地了解maven插件。

  2. 如何解决此问题?我想我可以将 scala 添加到根 pom,但由于它将是该级别的第一个依赖项,所以它似乎是错误的方法。

    我也可以 cd 进入一个有这个插件的子 pom 和 运行 那里的命令,但这意味着我只会 运行ning package 在一个项目上。理想情况下,在 运行 插件之前,我想 运行 package 整个项目。

运行 具有这样的插件目标的 Maven 将 运行 所有项目模块的目标,与 mvn install 将安装每个模块相同。您不必在任何地方定义插件 - 如果您在命令行上指定它,它是 运行.

您正确地将插件的执行绑定到一个或多个生命周期阶段,在本例中为 process-resourcesprocess-test-resources。如果你 运行 mvn compileprocess-resources 阶段将被执行,add-sourcescompile 目标将是 gatling 插件的 运行。同样,运行ning mvn test(当你 mvn install 时也是 运行)将 运行 加特林插件的 testCompile 目标。

您需要做的是同时绑定 execute 目标,如示例所示 在 page you linked 上。如果你想首先使用 packaged,将它绑定到 integration test phase:

<execution>
  <phase>integration-test</phase>
  <goals>
    <goal>execute</goal>
  </goals>
  <configuration>
     ....
  </configuration>
</execution>

注意integration-testpackage之后(在install之前的某处),所以要运行它,执行mvn integration-testmvn install.