2017-03-07 25 views
1

我正在嘗試爲集成測試提供並行maven測試執行。Maven surefire插件,在構建生命週期期間設置和獲取系統屬性

使用sql-maven-plugin在測試執行開始之前創建一個測試數據庫,並配合maven 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-行家-插件配置:

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

殼執行:

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提供未填充或語法問題。雖然它看起來很容易,但我不能搞定它,但我已經搞清楚了。

+0

對於集成測試,你應該看看Maven的故障安全插件。 –

+1

Surefire的'systemPropertyVariables'將在創建執行測試的JVM時設置系統屬性。顯然,當Maven試圖執行sql-maven-plugin時,該屬性不是爲Maven項目設置的屬性。 – Tome

+0

@Tome所以基本上,sql-maven-plugin和surefire插件在我想要的方式中不兼容? – Wrench

回答

1

您可能會混淆Maven的屬性,Java系統屬性和環境變量。您似乎希望使用的變量看起來像Maven屬性。

您可以在屬性元素中的POM中定義屬性。另外,您可以使用$ {env.PropertyName}表單引用環境變量,使用$ {java.PropertyName}表單引用Java系統屬性。

您可以參考本作的詳細信息:https://maven.apache.org/pom.html#Properties

+0

你是對的,我是。正如我在對我的問題的評論中提到的那樣,我已經開始了另一種方法,它提供了更多的實施工作,但與我的問題相比,理論上至少應該工作。感謝您的回答。 – Wrench

相關問題