java - Maven Surefire 插件,在构建生命周期中设置和获取系统属性

标签 java maven maven-surefire-plugin sql-maven-plugin

我正在尝试为集成测试提出并行 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/

相关文章:

testing - 线程 Maven 使用顺序单元测试构建?

java - 如何使用 Java 8 增强 List 方法中的重复对象?列表中的对象是嵌套的,这就是使这个复杂的原因

java - 如何检查三个字符是否彼此相等以及它们是否相等?

maven - "tagNameFormat"属性未应用于 Maven Release 插件

java - 如何在 Jenkins 中为每项工作全局设置 JVM 选项?

java - 具有不同系统属性值的多个 Maven 插件执行

java - 用数字命名类的新实例

c# - 从 Java 应用程序调用 C# dll

Spring Boot 1.5.3 NoSuchMethodError : org. springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.getResponseStatus()

java - 如何在maven surefire运行测试之前执行一次方法