Cannot instantiate interface 'ElasticsearchClient' - gradle

The official Elasticsearch docs tell to instantiate ElasticsearchClient like this:
ElasticsearchClient client = new ElasticsearchClient(transport);
Once I write this in my Grails 3 application with Gradle build management, I get the following compilation error: Cannot instantiate interface 'ElasticsearchClient'
The import statement is: import org.elasticsearch.client.ElasticsearchClient.
Indeed, ElasticsearchClient gets resolved to:
package org.elasticsearch.client;
// ...
public interface ElasticsearchClient {
The Gradle dependency is: compile 'org.elasticsearch:elasticsearch:7.17.2'
Why do they propose to instantiate an interface in their docs?
What can I do to make it compile and use the Elasticsearch client?

The reason for this was: ElasticsearchClient was mis-resolved to an old elasticsearch JAR file that had still been in my local user's .gradle directory. Removing the old JAR from .gradle enabled me to do the correct import.
The correct import is
import co.elastic.clients.elasticsearch.ElasticsearchClient
instead of
import org.elasticsearch.client.ElasticsearchClient

Related

Gradle shadowJar deletes required SQL driver

I'm using the shadowJar Gradle target provided by the com.github.johnrengelman.shadow Gradle plugin to build up an application, which requires an org.apache.hive.jdbc.HiveDriver to connect to Kudu using Impala.
The problem is that when I use standard approach to import the driver in Scala:
Class.forName("org.apache.hive.jdbc.HiveDriver")
the shadow plugin removes it from the resulting JAR, implying an runtime error of:
java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver.
My build.gradle contains:
dependencies {
implementation {
"org.apache.hive:hive-jdbc:1.2.1"
}
}
How do I instruct the shadow plugin not to delete the required dependency injected via a String?
I found out the solution is to include the static type not via Class.forName, but using code by importing it:
import java.sql.DriverManager
import org.apache.hive.jdbc.{HiveConnection, HiveDriver}
class Foo {
// Register Hive Driver this way to prevent shadowing to cut it off
new HiveDriver()
DriverManager.getConnection(connectionUrl, user, password) match {
case connection: HiveConnection =>
...
}
}
This way, the shadow plugin is officially informed about the fact that the Driver is actually required.

Upgraded to grails 3 and now get unable to resolve class org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

unable to resolve class org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
# line 4, column 1.
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
This happened when I upgraded grails from 2.2.4 to 3.1.3.
Is there some new place I need to import this too? Or is there a dependency I need to add to my gradle build file?
See step 5 on this page, https://docs.grails.org/3.0.x/guide/upgrading.html
Step 5 - Modify Package Imports In Grails 3.x all internal APIs can be
found in the org.grails package and public facing APIs in the grails
package. The org.codehaus.groovy.grails package no longer exists.
All package declaration in sources should be modified for the new
location of the respective classes. Example
org.codehaus.groovy.grails.commons.GrailsApplication is now
grails.core.GrailsApplication.

Spring Boot "unable to resolve class" with Groovy

I'm new to Groovy, Spring Boot, and Gradle (using gradle as well) and am trying to build a small test program.
I have a main class EvalMain and a InputObj class in a com.eval package.
#Controller
class EvalMain {
#RequestMapping("/")
#ResponseBody
public static String textTest() {
def iO = new InputObj("dsa", "dasdsa", "U1dBRw==");
return iO.xorString();
}
}
when running "spring run EvalMain.groovy" I get the following error:
startup failed:
file:<filepath>EvalMain.groovy: 14: unable to resolve class InputObj
# line 14, column 18.
def iO = new InputObj("dsa", "dasdsa", "U1dBRw==");
I tried throwing an import at the top but then spring boot complained about this too? What am I missing here? Any help would be much appreciated, thanks!
after adding the import:
import com.eval.InputObj;
I get this error:
file:/<filePath>/EvalMain.groovy: 2: unable to resolve class com.eval.InputObj
# line 2, column 1.
import com.eval.InputObj
^
1 error
Not sure if this is a good way to do it, but I was able to run it with "spring run .groovy .groovy" I wonder if there's the equivalent of a makefile?
Try to run spring run *.groovy.
Spring Boot CLI is for quick prototyping a single Groovy script. It tries to autoload all Spring Boot dependencies, but its your responsibility to include your additional dependencies
If you have just multiple source files you have to pass it to the CLI.
If it is an external dependency, include it with a #Grab, see here.
However, you have Gradle and more than one source file. Therefor it might be the time to switch from the Spring Boot CLI to a normal Gradle project.
Just go to the guides and choose "Build with Gradle". Then you will see an example Gradle build file. Also the Gradle Spring Boot plugin documentation provides samples.
You can start your application with gradlew bootRun.

Unable to import Spring Security #Secured Annotation into Grails 3

I am currently following the Spring Security 3.0.0.M1 plugin tutorial for Grails here and I appear to be stuck on Step 8. Using the statement import grails.plugin.springsecurity.annotation.Secured does not work because Grails cannot resolve the package name. I know that Spring Security for Grails 3 is in its infancy, but has anyone been able to get past this step yet? For reference, here is my SecureController class (with a another import that also does not work):
package ldaptest.controllers
import grails.plugin.springsecurity.annotation.Secured;
import org.springframework.security.access.annotation.Secured;
#Secured('ROLE_ADMIN')
class SecureController {
def index() {
render 'Secure access only'
}
}
I may found a solution:
Create a "lib" folder e.g. inside your "grails-app" directory.
Download the SpringSecurityCore JAR from here and move it into the lib directory
Add gradle dependency:
compile files('lib/spring-security-core-3.0.0.M1.jar')
Hope this helps.
Greetings
I had:3,1,1 the save problem with my application. I solved it by adding as a library to my project. However I had to change import package to make it work.
import org.springframework.security.access.annotation.Secured
I am using IntelliJ IDEA, I just has to search the maven repo for the spring-security-core:3.1.1.
In IntelliJ you do : File > Project Structure > Libraries > Add > From Maven Repository. Then do the search according to the version of "spring-security-core" you want to use.

Grails Spring Core Security Plugin - Unable to Resolve Classes

I'm using Grails 2.2.2 and I've already installed spring-security-core-2.0-RC2, by adding the below lines in my BuildConfig.groovy file:
Under the plugins:
compile ':spring-security-core:2.0-RC2'
Under Repositories:
mavenRepo 'http://repo.spring.io/milestone'
The plugin was installed successfully and I've created the security Domains and Controllers using s2-quickstart script.
Now, I've updated one of my controllers with the below code:
import grails.plugins.springsecurity.Secured
#Secured(['ROLE_ADMIN'])
However when I try to run the application I get the below error:
| Error Compilation error: startup failed: C:\workspace\SW
Development\Production\MyGrailsApp\safami\grails-app\controllers\safami\InfoPageAdminController.groovy:
4: unable to resolve class grails.plugins.springsecurity.Secured #
line 4, column 1. import grails.plugins.springsecurity.Secured
I'm new to Grails and I need your detailed solution.
The annotation was moved to a different package - change
import grails.plugins.springsecurity.Secured
to
import grails.plugin.springsecurity.annotation.Secured

Resources