我正在尝试为集成测试提出并行 Maven 测试执行。
使用sql-maven-plugin在测试执行开始之前创建一个测试数据库,结合 Maven Surefire 插件参数和并行执行,似乎是一个很好的开始方法。
我无法理解的是如何使用surefire插件设置的系统属性和sql-maven-plugin(或任何其他与此相关的插件)。我的设置失败了。
surefire插件配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>test_schema_${surefire.forkNumber}</databaseSchema>
</systemPropertyVariables>
</configuration>
</plugin>
sql-maven-plugin配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<username>user_with_create_privs</username>
<password>password</password>
<url>jdbc:mysql://localhost/dummy_schema</url>
</configuration>
<executions>
<execution>
<id>create-db</id>
<phase>process-test-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:mysql://localhost/dummy_schema</url>
<autocommit>true</autocommit>
<sqlCommand>create database ${databaseSchema}</sqlCommand>
</configuration>
</execution>
</executions>
</plugin>
shell 执行:
mvn test
结果输出:
[DEBUG] SQL: drop database ${databaseSchema}
[ERROR] Failed to execute: drop database ${databaseSchema}
[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute
(create-db) on project billing-core: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near '{databaseSchema}' at line 1 -> [Help 1]
所以,看起来 ${databaseSchema} 未填充,或者是语法问题。我已经摆弄过,但无法让它工作,尽管它看起来很简单并且应该。
最佳答案
您可能会混淆 Maven 的属性、Java 系统属性和环境变量。您似乎希望使用的变量看起来像 Maven 属性。
您可以在 POM 的属性元素中定义属性。此外,您还可以使用 ${env.PropertyName} 形式引用环境变量,并使用 ${java.PropertyName} 形式引用 Java 系统属性。
您可以引用此了解更多信息:https://maven.apache.org/pom.html#Properties
关于java - Maven Surefire 插件,在构建生命周期中设置和获取系统属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650354/