如何在命令行中指定 Gatling 的模拟 class

How to specify simulation class of Gatling in command line

我有简单的 Gatling 项目和两个模拟 classes (SimulationForAzure.scala & OtherSimulation.scala)。 当我只想选择一个模拟并尝试执行命令时

mvn -Dgatling.simulationClass=org.example.SimulationForAzure gatling:test

mvn gatling:test -Dgatling.simulationClass=org.example.SimulationForAzure

我收到错误:

Unknown lifecycle phase ".simulationClass=org.example.SimulationForAzure". You must specify a valid lifecycle phase or a goal

怎么了? 以及如何在命令行中指定模拟加特林class?

我的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>untitled</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>

    <gatling.version>3.3.1</gatling.version>
    <gatling-maven-plugin.version>3.0.5</gatling-maven-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.gatling.highcharts</groupId>
      <artifactId>gatling-charts-highcharts</artifactId>
      <version>${gatling.version}</version>
    </dependency>
    <dependency>
      <groupId>io.gatling</groupId>
      <artifactId>gatling-app</artifactId>
      <version>${gatling.version}</version>
    </dependency>
    <dependency>
      <groupId>io.gatling</groupId>
      <artifactId>gatling-recorder</artifactId>
      <version>${gatling.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>io.gatling</groupId>
        <artifactId>gatling-maven-plugin</artifactId>
        <version>${gatling-maven-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

这是我的 classes: Simulations.

我自己找到了解决办法。 我更改了 pom.xml 文件。现在看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>untitled</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>

    <gatling.version>3.3.1</gatling.version>
    <gatling-maven-plugin.version>3.0.5</gatling-maven-plugin.version>

    <className></className>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.gatling.highcharts</groupId>
      <artifactId>gatling-charts-highcharts</artifactId>
      <version>${gatling.version}</version>
    </dependency>
    <dependency>
      <groupId>io.gatling</groupId>
      <artifactId>gatling-app</artifactId>
      <version>${gatling.version}</version>
    </dependency>
    <dependency>
      <groupId>io.gatling</groupId>
      <artifactId>gatling-recorder</artifactId>
      <version>${gatling.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>io.gatling</groupId>
        <artifactId>gatling-maven-plugin</artifactId>
        <version>${gatling-maven-plugin.version}</version>
        <configuration>
          <simulationClass>${className}</simulationClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

当我在 PowerShell 中执行时

mvn gatling:test '-Dclassname=org.example.SimulationForAzure'

mvn gatling:test '-Dclassname=org.example.OtherSimulation'

一切如预期。

如果模拟 类 在 src/test/scala gatling.simulationClass 属性 下工作正常:

src
   test
       scala
            com.myproject
                  simulation
                      Sim1.scala
                      Sim2.scala
                      ...

pom.xml中的插件可以保持原样:

            <plugin>
                <groupId>io.gatling</groupId>
                <artifactId>gatling-maven-plugin</artifactId>
                <version>${gatling-maven-plugin.version}</version>
            </plugin>

此命令有效:

mvn gatling:test -Dgatling.simulationClass=com.myproject.simulation.Sim1