# Elasticsearch Maven Plugin using gradle

**URL:** <https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557>\
**Category:** Elasticsearch\
**Created:** [January 15, 2018, 2:27pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557 "2018-01-15T14:27:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [January 15, 2018, 2:27pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/1 "2018-01-15T14:27:27Z")

</div>

how to use this plugin in gradle project?

> **[alexcojocaru/elasticsearch-maven-plugin](https://github.com/alexcojocaru/elasticsearch-maven-plugin)**
>
> elasticsearch-maven-plugin - A Maven plugin to run a single node Elasticsearch cluster during the integration test phase of a build

I know to add dependency  
// [https://mvnrepository.com/artifact/com.github.alexcojocaru/elasticsearch-maven-plugin](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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 15, 2018, 4:30pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/2 "2018-01-15T16:30:06Z")

</div>

Better to use elasticsearch gradle plugin in that case.

I wrote an example there: [Elasticsearch.esplugin: Replace/Change/Disable integTestCluster#wait Task](https://discuss.elastic.co/t/elasticsearch-esplugin-replace-change-disable-integtestcluster-wait-task/115006/2?u=dadoonet)

Might help...

---

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [January 15, 2018, 4:52pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/3 "2018-01-15T16:52:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 15, 2018, 5:33pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/4 "2018-01-15T17:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 15, 2018, 8:32pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/5 "2018-01-15T20:32:30Z")

</div>

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](https://github.com/brusic/plugin-with-fixture-skeleton)

---

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [January 16, 2018, 8:56pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/6 "2018-01-16T20:56:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [January 16, 2018, 9:22pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/7 "2018-01-16T21:22:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 17, 2018, 2:43am UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/8 "2018-01-17T02:43:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 24, 2018, 6:52am UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/9 "2018-01-24T06:52:56Z")

</div>

@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](https://github.com/elastic/elasticsearch/blob/master/qa/smoke-test-client/build.gradle) 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.

---

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [January 26, 2018, 8:16pm UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/10 "2018-01-26T20:16:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 28, 2018, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/11 "2018-01-28T05:46:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 25, 2018, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-maven-plugin-using-gradle/115557/12 "2018-02-25T05:46:47Z")

</div>

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