2017-05-09 17 views
0

私はmaven javaプロジェクトを持っています。テストを実行するためにmavenテスト段階でmysqlドッカーイメージを実行する必要があり、その完了時にmysqlドッカーイメージを削除できます。dockerでmysqlを実行してmaven testを実行するには

+0

このリンクを参照してください。 https://hharnisc.github.io/2016/06/19/integration-testing-with-docker-compose.html 助けがあれば教えてください。 –

答えて

3

たとえば、Docker Mavenプラグイン(https://dmp.fabric8.io/)を使用するとします。ここでは、MySQLコンテナを起動するpomの例を示します.Maven Failsafe Pluginを使用して統合テストを行い、MySQLコンテナを停止します。また、プロパティーmysql.jdbc.urlをテストに渡すので、特定のDockerホスト上で実行されているMySQLコンテナへの正しいJDBC URL(Dockerの実行方法によって異なる可能性があります)が表示されます。

<?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>com.example</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <configuration> 
        <executable>true</executable> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>io.fabric8</groupId> 
       <artifactId>docker-maven-plugin</artifactId> 
       <version>0.20.1</version> 
       <extensions>true</extensions> 
       <configuration> 
        <images> 
         <image> 
          <alias>database</alias> 
          <name>mysql:5.7</name> 
          <run> 
           <wait> 
            <log>mysqld: ready for connections</log> 
            <time>20000</time> 
           </wait> 
           <env> 
            <MYSQL_ROOT_PASSWORD>abc123</MYSQL_ROOT_PASSWORD> 
            <MYSQL_DATABASE>testdb</MYSQL_DATABASE> 
            <MYSQL_USER>mysql</MYSQL_USER> 
            <MYSQL_PASSWORD>mysql</MYSQL_PASSWORD> 
           </env> 
           <ports> 
            <port>3306:3306</port> 
           </ports> 
          </run> 
         </image> 
         <image> 
          <name>mvndemo</name> 
          <build> 
           <from>java:8-jre</from> 
           <assembly> 
            <descriptorRef>artifact</descriptorRef> 
           </assembly> 
          </build> 
         </image> 
        </images> 
       </configuration> 
       <executions> 
        <execution> 
         <id>docker:start</id> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>start</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>docker:stop</id> 
         <phase>post-integration-test</phase> 
         <goals> 
          <goal>stop</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <artifactId>maven-failsafe-plugin</artifactId> 
       <version>2.17</version> 
       <executions> 
        <execution> 
         <id>integration-test</id> 
         <goals> 
          <goal>integration-test</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>verify</id> 
         <goals> 
          <goal>verify</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <systemPropertyVariables> 
         <mysql.jdbc.url>jdbc:mysql://${docker.host.address}/testdb</mysql.jdbc.url> 
        </systemPropertyVariables> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 
関連する問題