/gradle-elasticsearch-plugin

an ElasticSearch gradle plugin for integration tests with ElasticSearch

Primary LanguageGroovyApache License 2.0Apache-2.0

gradle-elasticsearch-plugin

An ElasticSearch gradle plugin for starting an ElasticSearch within a gradle build.

Build Status Download

A big thx

A big thx to the author of the original plugin which can be found here.

Using

Plugin setup with gradle >= 2.1:

    plugins {
        id "de.cgoit.gradle.elasticsearch" version "0.2.3"
    }

Plugin setup with gradle < 2.1:

    buildscript {
        repositories {
            jcenter()
            maven { url "http://dl.bintray.com/cgoit/maven" }
        }
        dependencies {
            classpath("de.cgoit.gradle.elasticsearch:gradle-elasticsearch-plugin:0.2.3")
        }
    }

    apply plugin: 'de.cgoit.gradle.elasticsearch'

Starting and stopping ElasticSearch during the integration tests

    task integrationTests(type: Test) {
      reports {
          html.destination "$buildDir/reports/integration-tests"
      }

      include "**/*IT.*"

      doFirst {
        startElasticsearch {
          elasticsearchVersion = "7.8.0"
          httpScheme = "http"
          httpHost = "localhost"
          httpPort = 9200
          transportPort = 9300
          dataDir = file("$buildDir/elastic")
          logsDir = file("$buildDir/elastic/logs")
          forceShutdownBeforeStart = false
        }
      }
  
      doLast {
        stopElasticsearch {
          httpScheme = "http"
          httpHost = "localhost"
          httpPort = 9200
        }
      }
    }
    
    gradle.taskGraph.afterTask { Task task, TaskState taskState ->
      if (task.name == "integrationTests") {
        stopElasticsearch {
          httpScheme = "http"
          httpHost = "localhost"
          httpPort = 9200
        }
      }
    }

    test {
      include '**/*Test.*'
      exclude '**/*IT.*'
    }

The above example shows a task called integrationTests which runs all the tests in the project with the IT suffix. The reports for these tests are placed in the buildDir/reports/integration-tests directory - just to separate them from regular tests. But the important part here is in the doFirst and doLast.

In the doFirst ElasticSearch is started. All the values in the example above are the default values, so if these values work for you they can be ommitted:

    doFirst {
      startElastic {}
    }

In the doLast ElasticSearch is stopped. Note that ElasticSearch is also stopped in the gradle.taskGraph.afterTask section - this is to catch any crashes during the integration tests and make sure that ElasticSearch is stopped in the build clean-up phase.

Lastly the regular test task is configured to exclude the tests with the IT suffix - we only wanted to run these in the integration tests phase, not with the regular tests.

References