Elasticsearch Maven Plugin using gradle


(Prakhar Jain) #1

how to use this plugin in gradle project?

I know to add dependency
// https://mvnrepository.com/artifact/com.github.alexcojocaru/elasticsearch-maven-plugin
compile group: 'com.github.alexcojocaru', name: 'elasticsearch-maven-plugin', version: '6.1'

Can I change configurations and other such things that are mentioned in its read me using only gradle.


(David Pilato) #2

Better to use elasticsearch gradle plugin in that case.

I wrote an example there: Elasticsearch.esplugin: Replace/Change/Disable integTestCluster#wait Task

Might help...


(Prakhar Jain) #3

I am not able to understand it.
Is there any documentation fro elasticsearch gradle plugin?


(David Pilato) #4

I don't know. May be @rjernst or @nik9000 have a link?


(Ivan Brusic) #5

There is no documentation if you want to create your own plugin. David is
correct in that you should use the plugin gradle plugin and his example is
great.

I believe in learning with code and the plugins in the core repo are
difficult to learn by, ironically even the example one, because they depend
on the rest of the gradle build defined at a higher level. Here is a simple
example, with an additional fixture if you want to test against external
resources: https://github.com/brusic/plugin-with-fixture-skeleton


(Prakhar Jain) #6

Hi, I was trying to start elastic search instance from this sample library.

I have nothing in my repository except this gradle file

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath "org.elasticsearch.gradle:build-tools:6.1.1"
    }
}

apply plugin: 'java'
apply plugin: 'elasticsearch.esplugin'
apply plugin: 'idea'
apply plugin: 'maven'

// this is temporal and will be fixed in 6.0
ext.projectSubstitutions = [:]

// license of this project
licenseFile = rootProject.file('LICENSE.txt')
// copyright notices
noticeFile = rootProject.file('NOTICE.txt')

esplugin {
    // license of the plugin, may be different than the above license
    licenseFile rootProject.file('LICENSE.txt')
    // copyright notices, may be different than the above notice
    noticeFile rootProject.file('NOTICE.txt')
    name 'ESTest'
    description 'TestingESGradle'
    classname 'ESTest'
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

// In this section you declare the dependencies for your production and test code
// Elasticsearch dependency is included due to the build-tools, test-framework as well
dependencies {
    testCompile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.1.1'
}

integTestCluster {
    setting 'clusterName',     'GetProductTest'
    setting 'version', '6.1.1'
    setting 'httpPort', '9400'
}

artifacts {
    archives javadocJar, sourcesJar
}

I am getting this error when running the command gradle run.

[elasticsearch] [2018-01-17T02:19:04,728][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-
0] uncaught exception in thread [main]
[elasticsearch] org.elasticsearch.bootstrap.StartupException: ElasticsearchException[Could not find 
plugin class [ESTest]]; nested: ClassNotFoundException[ESTest];

[...]

> Task :run#start 
[ant:elasticsearch] Result: 1
[elasticsearch] Result: 1

> Task :run#wait FAILED

I want to start a new Elasticsearch instance when I run test in my java project.
So basically I have a gradle task for test, which should first spin up elasticsearch instance and then perform tests on it.

How I will do it?

Your help will be highly appreciated.

Thanks,
Prakhar


(Prakhar Jain) #7

I got it how to run the gradle task, but I dont understand how do I integrate it with my project which already has gradle run task,
I want tasks of these plugin to run in my main project test task.

Thanks,
Prakhar


(Ivan Brusic) #8

I joined the conversation late and did not fully read your original
intention. I simply thought you wanted to use the plugin plugin.

That plugin is meant to develop plugins, but it probably can be repurposed
to simply run integration tests. Your integration test gradle task would
need to depend on the integTestCluster task. I do not think you will be
able to use the clients though, so you will need to make sure that the test
cluster starts on a port you are expecting. Perhaps there is some
elasticsearch project that already has this behavior.


Need to start ES instance for test purpose
(Ryan Ernst) #9

@prakharjain17 If you only want an integration test cluster, then Ivan is correct: applying the elasticsearch.esplugin gradle plugin is incorrect. Instead, you will want to use the elasticsearch.rest-test plugin. Then, you can create a task of type RestIntegTestTask, which automatically creates the cluster configuration extension. You should look at the client smoke test as an example.

so you will need to make sure that the test
cluster starts on a port you are expecting. Perhaps there is some
elasticsearch project that already has this behavior.

@Ivan Actually, the port does not need to be hardcoded. The cluster that is configured by the RestIntegTestTask has a special flag passed to elasticsearch which instructs the node to write its http and transport bound ports to special log files. The gradle code then reads these files after the cluster is started, and passes them as a system property to the test runner. This allows us to use ephemeral ports for all our testing, and we can run with --parallel and not get any race conditions across integ test runs.


(Prakhar Jain) #10

Here is the gradle file I am trying

group 'testElasticsearch'
version '1.0-SNAPSHOT'

apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.rest-test'
apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}
buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath "org.elasticsearch.gradle:build-tools:6.1.2"
    }
}


dependencies {
    testCompile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-high-level-client', version: '6.1.2'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

integTestCluster {
    setting 'clusterName',     'GetProductTest'
    setting 'version', '6.1.2'
    setting 'httpPort', '9400'
}

I am getting this error

Cannot add a SourceSet with name 'test' as a SourceSet with that name already exists.

which is due to java plugin defines its own SourceSet with name 'test'.
and this plugin is interfering with this SourceSet.

I am very noob at this. Don't know how to fix it.

I need to use it to write integration test in java for my application.


(Ryan Ernst) #11

This line needs to be removed:
apply plugin: 'java'

The standalone-rest-test plugin creates a test sourceset. If you want to include your project's jars, do that with a dependency on your jar in this project (ie you will need multiple projects).


(system) #12

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.